Skip to content

Commit

Permalink
Signal handling for Gaze scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew committed Feb 2, 2008
1 parent 664620b commit ac5c4d2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
15 changes: 13 additions & 2 deletions web/gaze-rest.cgi
Expand Up @@ -7,7 +7,7 @@
# Email: chris@mysociety.org; WWW: http://www.mysociety.org/
#

my $rcsid = ''; $rcsid .= '$Id: gaze-rest.cgi,v 1.23 2007-01-03 23:17:13 matthew Exp $';
my $rcsid = ''; $rcsid .= '$Id: gaze-rest.cgi,v 1.24 2008-02-02 12:30:50 matthew Exp $';

use strict;

Expand All @@ -33,6 +33,14 @@ binmode(STDOUT);

my $W = new mySociety::WatchUpdate();

# FastCGI signal handling
my $exit_requested = 0;
my $handling_request = 0;
$SIG{TERM} = $SIG{USR1} = sub {
$exit_requested = 1;
# exit(0) unless $handling_request;
};

# XXX do this in Gaze.pm?
my %countries = map { $_ => 1 } @{Gaze::get_find_places_countries()};
my $countries_last = time();
Expand Down Expand Up @@ -278,6 +286,7 @@ sub error ($%) {
}

while (my $q = new CGI::Fast()) {
$handling_request = 1;
my $f = $q->param('f');
if (!defined($f)) {
error($q, f => "missing (should specify function)");
Expand Down Expand Up @@ -397,5 +406,7 @@ while (my $q = new CGI::Fast()) {
), $r;
}
}
last if ($W->changed());
$W->exit_if_changed();
$handling_request = 0;
last if $exit_requested;
}
16 changes: 13 additions & 3 deletions web/gaze.cgi
Expand Up @@ -11,7 +11,7 @@
# Email: chris@mysociety.org; WWW: http://www.mysociety.org/
#

my $rcsid = ''; $rcsid .= '$Id: gaze.cgi,v 1.11 2006-12-01 16:28:25 matthew Exp $';
my $rcsid = ''; $rcsid .= '$Id: gaze.cgi,v 1.12 2008-02-02 12:30:50 matthew Exp $';

use strict;

Expand All @@ -33,9 +33,17 @@ use Gaze;
my $req = FCGI::Request();
my $W = new mySociety::WatchUpdate();

# FastCGI signal handling
my $exit_requested = 0;
my $handling_request = 0;
$SIG{TERM} = $SIG{USR1} = sub {
$exit_requested = 1;
# exit(0) unless $handling_request;
};

use constant cache_age => 86400;

while ($req->Accept() >= 0) {
while ($handling_request = ($req->Accept() >= 0)) {
RABX::Server::CGI::dispatch(
'Gaze.find_places' => [
sub { Gaze::find_places($_[0], $_[1], $_[2], $_[3], $_[4]); },
Expand Down Expand Up @@ -70,5 +78,7 @@ while ($req->Accept() >= 0) {
cache_age
]
);
last if ($W->changed());
$W->exit_if_changed();
$handling_request = 0;
last if $exit_requested;
}

0 comments on commit ac5c4d2

Please sign in to comment.