idadan
asked on
setting a timeout
Here is a copy of a admin at my hosting copany "I am thinking because it involves uploading the program is just not
executing but yet hanging around and keeping httpd childs open until we kill
them off. Could you set a timeout or somethiong on this script?" How would I do this? Here is the script. Thanks for any suggestions/help.
#!/usr/local/bin/perl
use CGI;
require '/home/dank/public_html/cg i-local/ch at/locksub s.pl';
$cgi = new CGI;
print "Content-type: text/html\n\n";
if ($cgi->param('file') eq '') {
&no_pic;
}
$username = $cgi->param('USERNAME');
$pics_dir = '/home/dank/public_html/cg i-local/pi cs';
$filename = "/home/dank/public_html/cg i-local/pi cs/$userna me.gif";
open(OUT,"> $filename");
while($bytesread = read($cgi->param('file'), $buffer, 1024)) {
print OUT $buffer;
}
close(OUT);
&lock("waiting-count.db"," $pics_dir" ,'3');
open(COUNT,"<$pics_dir/wai ting-count .db");
$count = <COUNT>;
close(COUNT);
$count++;
open(COUNT,">$pics_dir/wai ting-count .db");
print COUNT "$count";
close(COUNT);
&unlock("waiting-count.db" ,"$pics_di r");
&lock("waiting.db","$pics_ dir",'3');
open(WAIT,">>$pics_dir/wai ting.db");
print WAIT "$username--$count\n";
close(WAIT);
&unlock("waiting.db","$pic s_dir");
print "done. username = $username\n";
sub no_pic {
print "sorry please give a pic to process.\n";
exit;
}
executing but yet hanging around and keeping httpd childs open until we kill
them off. Could you set a timeout or somethiong on this script?" How would I do this? Here is the script. Thanks for any suggestions/help.
#!/usr/local/bin/perl
use CGI;
require '/home/dank/public_html/cg
$cgi = new CGI;
print "Content-type: text/html\n\n";
if ($cgi->param('file') eq '') {
&no_pic;
}
$username = $cgi->param('USERNAME');
$pics_dir = '/home/dank/public_html/cg
$filename = "/home/dank/public_html/cg
open(OUT,"> $filename");
while($bytesread = read($cgi->param('file'), $buffer, 1024)) {
print OUT $buffer;
}
close(OUT);
&lock("waiting-count.db","
open(COUNT,"<$pics_dir/wai
$count = <COUNT>;
close(COUNT);
$count++;
open(COUNT,">$pics_dir/wai
print COUNT "$count";
close(COUNT);
&unlock("waiting-count.db"
&lock("waiting.db","$pics_
open(WAIT,">>$pics_dir/wai
print WAIT "$username--$count\n";
close(WAIT);
&unlock("waiting.db","$pic
print "done. username = $username\n";
sub no_pic {
print "sorry please give a pic to process.\n";
exit;
}
What is &lock
ASKER
A file locking routine, from the cgi-perl cookbook (matt wright)...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok... in this line
$SIG{'ALRM'} = 'Wait_For_Timeout'; # A subroutine
is that legal? It looks funny to me.. and also shouldn't you define $go_on as something before calling it or won't it just read that as 0? I need this code to work as is, if there is anything wrong, could you just look it over and check it? thanks.
$SIG{'ALRM'} = 'Wait_For_Timeout'; # A subroutine
is that legal? It looks funny to me.. and also shouldn't you define $go_on as something before calling it or won't it just read that as 0? I need this code to work as is, if there is anything wrong, could you just look it over and check it? thanks.
Yes this is legal. If you have the Programming Perl v2 book look at p340-1 for an example. The only difference is that they use
$SIG{'x'} = \&foo, but its the same thing in the end.
The $go_on variable was defined a couple hundred lines previous in the script. It was declared as a 'my' and was visible to all code in this file.
You could keep your code pretty much the same. Write a new subroutine to cleanup the locks on a timeout and print an error message. Then wrap the area of the code you wish to guard with the timer stuff. e.g
$SIG{'ALRM'} = \&My_Timeout;
alarm(60);
&lock("waiting.db","$pics_ dir",'3');
open(WAIT,">>$pics_dir/wai ting.db");
print WAIT "$username--$count\n";
close(WAIT);
&unlock("waiting.db","$pic s_dir");
## We completed this part in under 60 seconds, cancel the timer
alarm(0);
$SIG{'ALRM'} = 'DEFAULT'; # Return to normal response
$SIG{'x'} = \&foo, but its the same thing in the end.
The $go_on variable was defined a couple hundred lines previous in the script. It was declared as a 'my' and was visible to all code in this file.
You could keep your code pretty much the same. Write a new subroutine to cleanup the locks on a timeout and print an error message. Then wrap the area of the code you wish to guard with the timer stuff. e.g
$SIG{'ALRM'} = \&My_Timeout;
alarm(60);
&lock("waiting.db","$pics_
open(WAIT,">>$pics_dir/wai
print WAIT "$username--$count\n";
close(WAIT);
&unlock("waiting.db","$pic
## We completed this part in under 60 seconds, cancel the timer
alarm(0);
$SIG{'ALRM'} = 'DEFAULT'; # Return to normal response
ASKER
Thanks for the help.