Skip to content

Commit

Permalink
Merge pull request #28 from limitusus/hotfix/spawn-interval
Browse files Browse the repository at this point in the history
Hotfix/spawn interval
  • Loading branch information
kazuho committed Feb 23, 2016
2 parents 1ef47c6 + a220d2f commit 962839a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/Plack/Handler/Starlet.pm
Expand Up @@ -75,7 +75,11 @@ sub run {
$self->accept_loop($app, $self->_calc_reqs_per_child());
$pm->finish;
}
while ($pm->wait_all_children(1)) {
my $timeout = 1;
if ($self->{spawn_interval}) {
$timeout = $self->{spawn_interval} * $self->{max_workers}
}
while ($pm->wait_all_children($timeout)) {
$pm->signal_all_children('TERM');
}
} else {
Expand Down
81 changes: 81 additions & 0 deletions t/13spawn_interval.t
@@ -0,0 +1,81 @@
use strict;
use warnings;

use File::Basename ();
use List::Util qw(first);
use Test::TCP ();
use File::Temp qw(tmpnam);

use Test::More;

BEGIN {
use_ok('Server::Starter');
};

sub findprog {
my $prog = shift;
first { -x $_ } map { "$_/$prog" } (
File::Basename::dirname($^X),
split /:/, $ENV{PATH},
);
}

sub read_status_file {
my $path = shift;
open my $fh, $path or die "failed to open status file $path";
my $contents = do { local $/; <$fh> };
close $fh;
my @pids = map { (split /:/, $_)[1] } split /\n/, $contents;
return @pids;
}

my $start_server = findprog('start_server');
my $plackup = findprog('plackup');

sub doit {
my $pkg = shift;
my $port = Test::TCP::empty_port();
my $status_file_path = tmpnam();
my $server_pid = fork();
die "fork failed:$!"
unless defined $server_pid;
if ($server_pid == 0) {
# child == server
exec(
$start_server,
"--port=$port",
"--status-file=$status_file_path",
"--signal-on-hup=USR1",
'--',
$plackup,
'--server',
$pkg,
'--max-workers=5', # just for finish test fast
'--spawn-interval=1',
't/00base-hello.psgi',
);
die "failed to launch server using start_server:$!";
}
# wait until all workers spawn
sleep 5;
my @pids = read_status_file($status_file_path);
my $sent_num_before_hup = kill 0, $pids[0];

kill 'HUP', $server_pid;

sleep 4;
my $sent_num_after_hup = kill 0, $pids[0];
is $sent_num_before_hup, 1, "process still alive before HUP signal";
is $sent_num_after_hup, 1, "old generation process still alive for a while after HUP signal";
sleep 1;
kill 'TERM', $server_pid;
while (wait == -1) {}
}

if ($start_server) {
doit('Starlet');
} else {
warn "could not find `start_server' next to $^X nor from \$PATH, skipping tests";
}

done_testing;

0 comments on commit 962839a

Please sign in to comment.