Permalink
Browse files

Super hack to handle QUIT to do graceful shutdown.

This allows Server::Starter to do the true graceful restart with:

    start_server --port 8080 --signal-on-hup=QUIT -- starman --preload-app ...
  • Loading branch information...
1 parent bec22af commit 109ed98eeac84224db9b09777cff66cb68f3d007 @miyagawa committed Dec 2, 2011
Showing with 24 additions and 1 deletion.
  1. +24 −1 lib/Starman/Server.pm
View
@@ -79,7 +79,7 @@ sub run {
user => $options->{user} || $>,
group => $options->{group} || $),
listen => $options->{backlog} || 1024,
- leave_children_open_on_hup => 1, # XXX conigurable?
+ check_for_waiting => 1,
no_client_stdout => 1,
%extra
);
@@ -101,12 +101,35 @@ sub pre_loop_hook {
register_sig(
TTIN => sub { $self->{server}->{$_}++ for qw( min_servers max_servers ) },
TTOU => sub { $self->{server}->{$_}-- for qw( min_servers max_servers ) },
+ QUIT => sub { $self->server_close(1) },
);
}
+sub server_close {
+ my($self, $quit) = @_;
+
+ if ($quit) {
+ $self->{server}->{$_} = 0 for qw( min_servers max_servers );
+ $self->hup_children;
+ while (1) {
+ Net::Server::SIG::check_sigs();
+ $self->coordinate_children;
+ last if !keys %{$self->{server}{children}};
+ sleep 1;
+ }
+ }
+
+ $self->SUPER::server_close();
+}
+
sub run_parent {
my $self = shift;
$0 = "starman master " . join(" ", @{$self->{options}{argv} || []});
+ local *Net::Server::PreFork::register_sig = sub {
+ my %args = @_;
+ delete $args{QUIT};
+ Net::Server::SIG::register_sig(%args);
+ };
$self->SUPER::run_parent(@_);
}

0 comments on commit 109ed98

Please sign in to comment.