I have application that works using Perl’s CGI::Fast.
Basically mainloop of the code is:
while (my $cgi = CGI::Fast->new() ) { do_whatever( $cgi ); }
Now, I wanted to add ability to kill it, but without disrupting currently processed request. To do so, I added handling to SIGHUP. More or less along the lines:
my $sighupped = 0; $SIG{ 'HUP' } = sub { $sighupped = 1; }; while (my $cgi = CGI::Fast->new() ) { do_whatever( $cgi ); exit if $sighupped; }
Which works really great when it comes to ‘not disturbing process while it handles user request’. But, if the process is waiting for new request the sighup-based exit will not be executed until it will finally get some request and process it.
It there any workaround for this? What I would like to achieve it to make the script exit immediately if the HUP (or other signal, it can be changed) reaches it while waiting for request.
You could use the block version of the
evalfunction and thealarmfunction to add a timeout toFast::CGI. In the code below I have set a five second timeout. If it times out it will go back to the start of the loop which will check to see if$continuehas been set to zero yet. If it hasn’t then it we start a newCGI::Fastobject. This means that a maximum of five seconds will go by after you send aHUPbefore the code will start to stop if it was waiting for a newCGI::Fastobject (the timeout has no affect on the rest of the loop).