blib/lib/Server/Starter.pm

filter: Statementstmt 83.5% 219 / 262 Branchbran 61.1% 88 / 144 Conditioncond 57.4% 27 / 47 MC/DCmcdc 33.3% 10 / 30 Subroutinesub 90.0% 27 / 30 Podpod 66.6% 2 / 3 totaltotal 72.2% 373 / 516 CC 98 SCAR 38.4
CC
98
Cov
74%
restart_server
90.0
start_server
88.6
__ANON__
12.0
CRAP 46.5
↑ Summary
1 package Server::Starter;
2  
3 62 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 62 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 62 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 62 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 62 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 62 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 62 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 62 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 62 ▶use POSIX qw(:sys_wait_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 62 ▶use Scope::Guard;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 62 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 62 our $VERSION = '0.20_02';
18 62 our @EXPORT_OK = qw(start_server restart_server server_ports);
19  
20 62 my @signals_received;
21  
22 sub start_server {
23 33 ▶ my $opts = {
Subroutine start_server: called CC=54 SCAR=44.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 33 33
24 (@_ == 1 ? @$_[0] : @_),
25 };
26 $opts->{interval} = 1
27 33 ▶ if not defined $opts->{interval};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"interval"} 33 0 33
28 33 ▶ $opts->{signal_on_hup} ||= 'TERM';
Condition100%2/2
$opts->{"signal_on_hup"} ||= "TERM"
l!l
924
MC/DC100%1/1
$opts->{"signal_on_hup"} ||= "TERM"
$opts->{"signal_on_hup"}
Truth table100%2/2
A
A= $opts->{"signal_on_hup"}
Aresult
11
00
29 33 ▶ $opts->{signal_on_term} ||= 'TERM';
Condition50%1/2
$opts->{"signal_on_term"} ||= "TERM"
l!l
033
MC/DC0%0/1
$opts->{"signal_on_term"} ||= "TERM"
$opts->{"signal_on_term"}
Truth table50%1/2
A
A= $opts->{"signal_on_term"}
Aresult
11
00
30 33 ▶ $opts->{backlog} ||= Socket::SOMAXCONN();
Condition50%1/2
$opts->{"backlog"} ||= 4096
l!l
033
MC/DC0%0/1
$opts->{"backlog"} ||= 4096
$opts->{"backlog"}
Truth table50%1/2
A
A= $opts->{"backlog"}
Aresult
11
00
31 33 for ($opts->{signal_on_hup}, $opts->{signal_on_term}) {
32 # normalize to the one that can be passed to kill
33 66 tr/a-z/A-Z/;
34 66 s/^SIG//i;
35 }
36  
37 # prepare args
38 33 my $ports = $opts->{port};
39 33 my $paths = $opts->{path};
40 33 ▶ croak "either of ``port'' or ``path'' option is mandatory\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $ports or $paths 0 33 33
Condition66%2/3
$ports or $paths
l!l&&r!l&&!r
3120
MC/DC0%0/2
$ports or $paths
$ports $paths
Truth table66%2/3
A or B
A= $portsB= $paths
ABresult
1X1
011
000
41 unless $ports || $paths;
42 33 ▶ $ports = [ $ports ]
Branch: 1 branch
BranchTrueFalseTotal
if not ref $ports and defined $ports 31 2 33
Condition66%2/3
not ref $ports and defined $ports
!ll&&!rl&&r
0231
MC/DC50%1/2
not ref $ports and defined $ports
not ref $ports defined $ports
Truth table66%2/3
not(A) and B
A= not ref $portsB= defined $ports
ABresult
0X0
100
111
43 if ! ref $ports && defined $ports;
44 33 ▶ $paths = [ $paths ]
Branch: 1 branch
BranchTrueFalseTotal
if not ref $paths and defined $paths 2 31 33
Condition66%2/3
not ref $paths and defined $paths
!ll&&!rl&&r
0312
MC/DC50%1/2
not ref $paths and defined $paths
not ref $paths defined $paths
Truth table66%2/3
not(A) and B
A= not ref $pathsB= defined $paths
ABresult
0X0
100
111
45 if ! ref $paths && defined $paths;
46 croak "mandatory option ``exec'' is missing or is not an arrayref\n"
47 33 ▶ unless $opts->{exec} && ref $opts->{exec} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"exec"} and ref $opts->{"exec"} eq "ARRAY" 0 33 33
Condition33%1/3
$opts->{"exec"} and ref $opts->{"exec"} eq "ARRAY"
!ll&&!rl&&r
0033
MC/DC0%0/2
$opts->{"exec"} and ref $opts->{"exec"} eq "ARRAY"
$opts->{"exec"} ref $opts->{"exec"} eq "ARRAY"
Truth table33%1/3
A and B
A= $opts->{"exec"}B= ref $opts->{"exec"} eq "ARRAY"
ABresult
0X0
100
111
48  
49 # set envs
50 $ENV{ENVDIR} = $opts->{envdir}
51 33 ▶ if defined $opts->{envdir};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"envdir"} 4 29 33
52 $ENV{ENABLE_AUTO_RESTART} = $opts->{enable_auto_restart}
53 33 ▶ if defined $opts->{enable_auto_restart};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"enable_auto_restart"} 12 21 33
54 $ENV{KILL_OLD_DELAY} = $opts->{kill_old_delay}
55 33 ▶ if defined $opts->{kill_old_delay};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"kill_old_delay"} 12 21 33
56 $ENV{AUTO_RESTART_INTERVAL} = $opts->{auto_restart_interval}
57 33 ▶ if defined $opts->{auto_restart_interval};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"auto_restart_interval"} 6 27 33
58  
59 # open pid file
60 my $pid_file_guard = sub {
61 33 ▶ return unless $opts->{pid_file};
Subroutine __ANON__: called CC=3 SCAR=20.3
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"pid_file"} 33 0 33
62 open my $fh, '>', $opts->{pid_file}
63 0 ▶ or die "failed to open file:$opts->{pid_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $opts->{"pid_file"} or die qq[failed to open file:$opts->{"pid_file"}: $!] 0 0 0
64 0 print $fh "$$\n";
65 0 close $fh;
66 return Scope::Guard->new(
67 sub {
68 0 ▶ unlink $opts->{pid_file};
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 },
70 0 );
71 33 }->();
72
73 # open log file
74 33 ▶ if ($opts->{log_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"log_file"}) 0 33 33
75 open my $fh, '>>', $opts->{log_file}
76 0 ▶ or die "failed to open log file:$opts->{log_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $opts->{"log_file"} or die qq[failed to open log file:$opts->{"log_file"}: $!] 0 0 0
77 0 STDOUT->flush;
78 0 STDERR->flush;
79 0 ▶ open STDOUT, '>&', $fh
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $fh or die "failed to dup STDOUT to file: $!" 0 0 0
80 or die "failed to dup STDOUT to file: $!";
81 0 ▶ open STDERR, '>&', $fh
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $fh or die "failed to dup STDERR to file: $!" 0 0 0
82 or die "failed to dup STDERR to file: $!";
83 0 close $fh;
84 }
85
86 # create guard that removes the status file
87 my $status_file_guard = $opts->{status_file} && Scope::Guard->new(
88 sub {
89 7 ▶ unlink $opts->{status_file};
Subroutine __ANON__: called CC=1 SCAR=0.0
90 },
91 33 ▶ );
Condition66%2/3
$opts->{'status_file'} && 'Scope::Guard'->new(sub { unlink $opts->{'status_file'}; } )
!ll&&!rl&&r
11022
MC/DC50%1/2
$opts->{'status_file'} && 'Scope::Guard'->new(sub { unlink $opts->{'status_file'}; } )
$opts->{'status_file'} 'Scope::Guard'->new(sub { unlink $opts->{'status_file'}; } )
Truth table66%2/3
A && B
A= $opts->{'status_file'}B= 'Scope::Guard'->new(sub { unlink $opts->{'status_file'}; } )
ABresult
0X0
100
111
92
93 33 print STDERR "start_server (pid:$$) starting now...\n";
94
95 # start listening, setup envvar
96 33 my @sock;
97 my @sockenv;
98 33 for my $port (@$ports) {
99 31 my $sock;
100 31 ▶ if ($port =~ /^\s*(\d+)\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($port =~ /^\s*(\d+)\s*$/) { } 31 0 31
elsif ($port =~ /^\s*(.*)\s*:\s*(\d+)\s*$/) { } 0 0 0
101 $sock = IO::Socket::INET->new(
102 Listen => $opts->{backlog},
103 31 LocalPort => $port,
104 Proto => 'tcp',
105 ReuseAddr => 1,
106 );
107 } elsif ($port =~ /^\s*(.*)\s*:\s*(\d+)\s*$/) {
108 0 $port = "$1:$2";
109 $sock = IO::Socket::INET->new(
110 Listen => $opts->{backlog},
111 0 LocalAddr => $port,
112 Proto => 'tcp',
113 ReuseAddr => 1,
114 );
115 } else {
116 0 croak "invalid ``port'' value:$port\n"
117 }
118 31 ▶ die "failed to listen to $port:$!"
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 31 31
119 unless $sock;
120 31 ▶ fcntl($sock, F_SETFD, my $flags = '')
Branch: 1 branch
BranchTrueFalseTotal
fcntl $sock, 2, my $flags = "" or die "fcntl(F_SETFD, 0) failed:$!" 0 31 31
121 or die "fcntl(F_SETFD, 0) failed:$!";
122 31 push @sockenv, "$port=" . $sock->fileno;
123 31 push @sock, $sock;
124 }
125 my $path_remove_guard = Scope::Guard->new(
126 sub {
127 -S $_ and unlink $_
128 10 ▶ for @$paths;
Subroutine __ANON__: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
-S $_ and unlink $_ 1 0 1
129 },
130 33 );
131 33 for my $path (@$paths) {
132 2 ▶ if (-S $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-S $path) 0 2 2
133 0 warn "removing existing socket file:$path";
134 0 ▶ unlink $path
Branch: 1 branch
BranchTrueFalseTotal
unlink $path or die "failed to remove existing socket file:${path}:$!" 0 0 0
135 or die "failed to remove existing socket file:$path:$!";
136 }
137 2 unlink $path;
138 2 my $saved_umask = umask(0);
139 my $sock = IO::Socket::UNIX->new(
140 Listen => $opts->{backlog},
141 2 ▶ Local => $path,
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::UNIX"->new("Listen", $opts->{"backlog"}, "Local", $path) or die "failed to listen to file ${path}:$!" 0 2 2
142 ) or die "failed to listen to file $path:$!";
143 2 umask($saved_umask);
144 2 ▶ fcntl($sock, F_SETFD, my $flags = '')
Branch: 1 branch
BranchTrueFalseTotal
fcntl $sock, 2, my $flags = "" or die "fcntl(F_SETFD, 0) failed:$!" 0 2 2
145 or die "fcntl(F_SETFD, 0) failed:$!";
146 2 push @sockenv, "$path=" . $sock->fileno;
147 2 push @sock, $sock;
148 }
149 33 $ENV{SERVER_STARTER_PORT} = join ";", @sockenv;
150 33 $ENV{SERVER_STARTER_GENERATION} = 0;
151
152 # setup signal handlers
153 _set_sighandler($_, sub {
154 55 ▶ push @signals_received, $_[0];
Subroutine __ANON__: called CC=1 SCAR=0.0
155 33 }) for (qw/INT TERM HUP ALRM/);
156 33 $SIG{PIPE} = 'IGNORE';
157
158 # setup status monitor
159 33 my ($current_worker, %old_workers, $last_restart_time);
160 my $update_status = $opts->{status_file}
161 ? sub {
162 40 ▶ my $tmpfn = "$opts->{status_file}.$$";
Subroutine __ANON__: called CC=5 SCAR=16.3
163 40 ▶ open my $tmpfh, '>', $tmpfn
Branch: 1 branch
BranchTrueFalseTotal
open my $tmpfh, ">", $tmpfn or die "failed to create temporary file:${tmpfn}:$!" 0 40 40
164 or die "failed to create temporary file:$tmpfn:$!";
165 my %gen_pid = (
166 ($current_worker
167 ? ($ENV{SERVER_STARTER_GENERATION} => $current_worker)
168 : ()),
169 40 ▶ map { $old_workers{$_} => $_ } keys %old_workers,
Branch: 1 branch
BranchTrueFalseTotal
$current_worker ? : 33 7 40
170 );
171 print $tmpfh "$_:$gen_pid{$_}\n"
172 40 for sort keys %gen_pid;
173 40 close $tmpfh;
174 rename $tmpfn, $opts->{status_file}
175 40 ▶ or die "failed to rename $tmpfn to $opts->{status_file}:$!";
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpfn, $opts->{"status_file"} or die qq[failed to rename $tmpfn to $opts->{"status_file"}:$!] 0 40 40
176 ▶ } : sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
177 33 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'status_file'} ? : 22 11 33
178  
179 # setup the start_worker function
180 my $start_worker = sub {
181 66 ▶ my $pid;
Subroutine __ANON__: called CC=7 SCAR=19.9
182 66 while (1) {
183 66 $ENV{SERVER_STARTER_GENERATION}++;
184 66 $pid = fork;
185 66 ▶ die "fork(2) failed:$!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 66 66
186 unless defined $pid;
187 66 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 23 43 66
188 23 my @args = @{$opts->{exec}};
189 # child process
190 23 ▶ if (defined $opts->{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"dir"}) 2 21 23
191 2 ▶ chdir $opts->{dir} or die "failed to chdir:$!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $opts->{"dir"} or die "failed to chdir:$!" 0 2 2
192 }
193 23 { exec { $args[0] } @args };
194 0 print STDERR "failed to exec $args[0]$!";
195 0 exit(255);
196 }
197 43 print STDERR "starting new worker $pid\n";
198 43 sleep $opts->{interval};
199 43 ▶ if ((grep { $_ ne 'HUP' } @signals_received)
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ ne "HUP";} @signals_received or waitpid($pid, 1) <= 0) 43 0 43
Condition66%2/3
grep {$_ ne "HUP";} @signals_received or waitpid($pid, 1) <= 0
l!l&&r!l&&!r
1420
MC/DC0%0/2
grep {$_ ne "HUP";} @signals_received or waitpid($pid, 1) <= 0
grep {$_ ne "HUP";} @signals_received waitpid($pid, 1) <= 0
Truth table66%2/3
A or B
A= grep {$_ ne "HUP";} @signals_receivedB= waitpid($pid, 1) <= 0
ABresult
1X1
011
000
200 || waitpid($pid, WNOHANG) <= 0) {
201 43 last;
202 }
203 0 print STDERR "new worker $pid seems to have failed to start, exit status:$?\n";
204 }
205 # ready, update the environment
206 43 $current_worker = $pid;
207 43 $last_restart_time = time;
208 43 $update_status->();
209 33 };
210  
211 # setup the wait function
212 my $wait = sub {
213 78 ▶ my $block = @signals_received == 0;
Subroutine __ANON__: called CC=3 SCAR=11.0
214 78 my @r;
215 78 ▶ if ($block && $ENV{ENABLE_AUTO_RESTART}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block and $ENV{'ENABLE_AUTO_RESTART'}) { } 32 46 78
Condition66%2/3
$block and $ENV{"ENABLE_AUTO_RESTART"}
!ll&&!rl&&r
04632
MC/DC50%1/2
$block and $ENV{"ENABLE_AUTO_RESTART"}
$block $ENV{"ENABLE_AUTO_RESTART"}
Truth table66%2/3
A and B
A= $blockB= $ENV{"ENABLE_AUTO_RESTART"}
ABresult
0X0
100
111
216 32 alarm(1);
217 32 @r = _wait3($block);
218 32 alarm(0);
219 } else {
220 46 @r = _wait3($block);
221 }
222 78 return @r;
223 33 };
224  
225 # setup the cleanup function
226 my $cleanup = sub {
227 10 ▶ my $sig = shift;
Subroutine __ANON__: called CC=5 SCAR=16.2
228 10 ▶ my $term_signal = $sig eq 'TERM' ? $opts->{signal_on_term} : 'TERM';
Branch: 1 branch
BranchTrueFalseTotal
$sig eq 'TERM' ? : 10 0 10
229 10 $old_workers{$current_worker} = $ENV{SERVER_STARTER_GENERATION};
230 10 undef $current_worker;
231 10 print STDERR "received $sig, sending $term_signal to all workers:",
232 join(',', sort keys %old_workers), "\n";
233 kill $term_signal, $_
234 10 for sort keys %old_workers;
235 10 ▶ while (%old_workers) {
Branch: 1 branch
BranchTrueFalseTotal
if (%old_workers) 10 10 20
236 10 ▶ if (my @r = _wait3(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@r) = _wait3(1)) 10 0 10
237 10 my ($died_worker, $status) = @r;
238 10 print STDERR "worker $died_worker died, status:$status\n";
239 10 delete $old_workers{$died_worker};
240 10 $update_status->();
241 }
242 }
243 10 print STDERR "exiting\n";
244 33 };
245  
246 # the main loop
247 33 $start_worker->();
248 23 while (1) {
249 # wait for next signal (or when auto-restart becomes necessary)
250 78 my @r = $wait->();
251 # reload env if necessary
252 78 my %loaded_env = _reload_env();
253 78 my @loaded_env_keys = keys %loaded_env;
254 78 local @ENV{@loaded_env_keys} = map { $loaded_env{$_} } (@loaded_env_keys);
255 $ENV{AUTO_RESTART_INTERVAL} ||= 360
256 78 ▶ if $ENV{ENABLE_AUTO_RESTART};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"ENABLE_AUTO_RESTART"} 32 46 78
Condition50%1/2
$ENV{"AUTO_RESTART_INTERVAL"} ||= 360
l!l
320
MC/DC0%0/1
$ENV{"AUTO_RESTART_INTERVAL"} ||= 360
$ENV{"AUTO_RESTART_INTERVAL"}
Truth table50%1/2
A
A= $ENV{"AUTO_RESTART_INTERVAL"}
Aresult
11
00
257 # restart if worker died
258 78 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) 24 54 78
259 24 my ($died_worker, $status) = @r;
260 24 ▶ if ($died_worker == $current_worker) {
Branch: 1 branch
BranchTrueFalseTotal
if ($died_worker == $current_worker) { } 12 12 24
261 12 print STDERR "worker $died_worker died unexpectedly with status:$status, restarting\n";
262 12 $start_worker->();
263 } else {
264 12 print STDERR "old worker $died_worker died, status:$status\n";
265 12 delete $old_workers{$died_worker};
266 12 $update_status->();
267 }
268 }
269 # handle signals
270 74 my $restart;
271 74 ▶ while (@signals_received) {
Branch: 1 branch
BranchTrueFalseTotal
if (@signals_received) 55 47 102
272 55 my $sig = shift @signals_received;
273 55 ▶ if ($sig eq 'HUP') {
Branch: 2 branches
BranchTrueFalseTotal
if ($sig eq 'HUP') { } 17 38 55
elsif ($sig eq 'ALRM') { } 28 10 38
274 17 print STDERR "received HUP, spawning a new worker\n";
275 17 $restart = 1;
276 17 last;
277 } elsif ($sig eq 'ALRM') {
278 # skip
279 } else {
280 10 return $cleanup->($sig);
281 }
282 }
283 64 ▶ if (! $restart && $ENV{ENABLE_AUTO_RESTART}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $restart and $ENV{"ENABLE_AUTO_RESTART"}) 30 34 64
Condition100%3/3
not $restart and $ENV{"ENABLE_AUTO_RESTART"}
!ll&&!rl&&r
171730
MC/DC100%2/2
not $restart and $ENV{"ENABLE_AUTO_RESTART"}
not $restart $ENV{"ENABLE_AUTO_RESTART"}
Truth table100%3/3
not(A) and B
A= not $restartB= $ENV{"ENABLE_AUTO_RESTART"}
ABresult
0X0
100
111
284 30 my $auto_restart_interval = $ENV{AUTO_RESTART_INTERVAL};
285 30 my $elapsed_since_restart = time - $last_restart_time;
286 30 ▶ if ($elapsed_since_restart >= $auto_restart_interval && ! %old_workers) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elapsed_since_restart >= $auto_restart_interval and not %old_workers) { } 4 26 30
elsif ($elapsed_since_restart >= $auto_restart_interval * 2) { } 0 26 26
Condition66%2/3
$elapsed_since_restart >= $auto_restart_interval and not %old_workers
!ll&&!rl&&r
2604
MC/DC50%1/2
$elapsed_since_restart >= $auto_restart_interval and not %old_workers
$elapsed_since_restart >= $auto_restart_interval not %old_workers
Truth table66%2/3
A and not(B)
A= $elapsed_since_restart >= $auto_restart_intervalB= not %old_workers
ABresult
0X0
100
111
287 4 print STDERR "autorestart triggered (interval=$auto_restart_interval)\n";
288 4 $restart = 1;
289 } elsif ($elapsed_since_restart >= $auto_restart_interval * 2) {
290 0 print STDERR "autorestart triggered (forced, interval=$auto_restart_interval)\n";
291 0 $restart = 1;
292 }
293 }
294 # restart if requested
295 64 ▶ if ($restart) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restart) 21 43 64
296 21 $old_workers{$current_worker} = $ENV{SERVER_STARTER_GENERATION};
297 21 $start_worker->();
298 12 print STDERR "new worker is now running, sending $opts->{signal_on_hup} to old workers:";
299 12 ▶ if (%old_workers) {
Branch: 1 branch
BranchTrueFalseTotal
if (%old_workers) { } 12 0 12
300 12 print STDERR join(',', sort keys %old_workers), "\n";
301 } else {
302 0 print STDERR "none\n";
303 }
304 12 ▶ my $kill_old_delay = defined $ENV{KILL_OLD_DELAY} ? $ENV{KILL_OLD_DELAY} : $ENV{ENABLE_AUTO_RESTART} ? 5 : 0;
Branch: 2 branches
BranchTrueFalseTotal
defined $ENV{'KILL_OLD_DELAY'} ? : 4 8 12
$ENV{'ENABLE_AUTO_RESTART'} ? : 0 8 8
305 12 ▶ if ($kill_old_delay != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kill_old_delay != 0) 4 8 12
306 4 print STDERR "sleeping $kill_old_delay secs before killing old workers\n";
307 4 sleep $kill_old_delay;
308 }
309 12 print STDERR "killing old workers\n";
310 kill $opts->{signal_on_hup}, $_
311 12 for sort keys %old_workers;
312 }
313 }
314  
315 0 die "unreachable";
316 }
317  
318 sub restart_server {
319 0 ▶ my $opts = {
Subroutine restart_server: not called CC=9 SCAR=45.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
320 (@_ == 1 ? @$_[0] : @_),
321 };
322 die "--restart option requires --pid-file and --status-file to be set as well\n"
323 0 ▶ unless $opts->{pid_file} && $opts->{status_file};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"pid_file"} and $opts->{"status_file"} 0 0 0
Condition0%0/3
$opts->{"pid_file"} and $opts->{"status_file"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"pid_file"} and $opts->{"status_file"}
$opts->{"pid_file"} $opts->{"status_file"}
Truth table0%0/3
A and B
A= $opts->{"pid_file"}B= $opts->{"status_file"}
ABresult
0X0
100
111
324
325 # get pid
326 0 my $pid = do {
327 open my $fh, '<', $opts->{pid_file}
328 0 ▶ or die "failed to open file:$opts->{pid_file}:$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $opts->{"pid_file"} or die qq[failed to open file:$opts->{"pid_file"}:$!] 0 0 0
329 0 my $line = <$fh>;
330 0 chomp $line;
331 0 $line;
332 };
333
334 # function that returns a list of active generations in sorted order
335 my $get_generations = sub {
336 open my $fh, '<', $opts->{status_file}
337 0 ▶ or die "failed to open file:$opts->{status_file}:$!";
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $opts->{"status_file"} or die qq[failed to open file:$opts->{"status_file"}:$!] 0 0 0
338 0 ▶ uniq sort { $a <=> $b } map { /^(\d+):/ ? ($1) : () } <$fh>;
Branch: 1 branch
BranchTrueFalseTotal
/^(\d+):/ ? : 0 0 0
339 0 };
340
341 # wait for this generation
342 0 my $wait_for = do {
343 0 ▶ my @gens = $get_generations->()
Branch: 1 branch
BranchTrueFalseTotal
my(@gens) = &$get_generations() or die "no active process found in the status file" 0 0 0
344 or die "no active process found in the status file";
345 0 pop(@gens) + 1;
346 };
347
348 # send HUP
349 0 ▶ kill 'HUP', $pid
Branch: 1 branch
BranchTrueFalseTotal
kill "HUP", $pid or die "failed to send SIGHUP to the server process:$!" 0 0 0
350 or die "failed to send SIGHUP to the server process:$!";
351
352 # wait for the generation
353 0 while (1) {
354 0 my @gens = $get_generations->();
355 0 ▶ last if scalar(@gens) == 1 && $gens[0] == $wait_for;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @gens == 1 and $gens[0] == $wait_for 0 0 0
Condition0%0/3
scalar @gens == 1 and $gens[0] == $wait_for
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @gens == 1 and $gens[0] == $wait_for
scalar @gens == 1 $gens[0] == $wait_for
Truth table0%0/3
A and B
A= scalar @gens == 1B= $gens[0] == $wait_for
ABresult
0X0
100
111
356 0 sleep 1;
357 }
358 }
359  
360 sub server_ports {
361 die "no environment variable SERVER_STARTER_PORT. Did you start the process using server_starter?",
362 17 ▶ unless $ENV{SERVER_STARTER_PORT};
Subroutine server_ports: called CC=2 SCAR=7.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"SERVER_STARTER_PORT"} 0 17 17
363 my %ports = map {
364 17 +(split /=/, $_, 2)
365 17 } split /;/, $ENV{SERVER_STARTER_PORT};
366 17 \%ports;
367 }
368  
369 sub _reload_env {
370 78 ▶ my $dn = $ENV{ENVDIR};
Subroutine _reload_env: called CC=8 SCAR=21.1
371 78 ▶ return if !defined $dn or !-d $dn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dn and -d $dn 69 9 78
Condition66%2/3
defined $dn and -d $dn
!ll&&!rl&&r
6909
MC/DC50%1/2
defined $dn and -d $dn
defined $dn -d $dn
Truth table66%2/3
A and B
A= defined $dnB= -d $dn
ABresult
0X0
100
111
372 9 my $d;
373 9 ▶ opendir($d, $dn) or return;
Branch: 1 branch
BranchTrueFalseTotal
opendir $d, $dn or return 0 9 9
374 9 my %env;
375 9 ▶ while (my $n = readdir($d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n = readdir $d)) 23 9 32
376 23 ▶ next if $n =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $n =~ /^\./ 18 5 23
377 5 ▶ open my $fh, '<', "$dn/$n" or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "$dn/$n" or next 0 5 5
378 5 chomp(my $v = <$fh>);
379 5 ▶ $env{$n} = $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 5 0 5
380 }
381 9 return %env;
382 }
383  
384 62 our $sighandler_should_die;
385 62 my $sighandler_got_sig;
386  
387 sub _set_sighandler {
388 133 ▶ my ($sig, $proc) = @_;
Subroutine _set_sighandler: called CC=1 SCAR=0.0
389 $SIG{$sig} = sub {
390 57 ▶ $proc->(@_);
Subroutine __ANON__: called CC=2 SCAR=6.9
391 57 $sighandler_got_sig = 1;
392 57 ▶ die "got signal"
Branch: 1 branch
BranchTrueFalseTotal
if $Server::Starter::sighandler_should_die 56 1 57
393 if $sighandler_should_die;
394 133 };
395 }
396  
397 sub _wait3 {
398 92 ▶ my $block = shift;
Subroutine _wait3: called CC=6 SCAR=18.0
399 92 my $pid = -1;
400 92 ▶ if ($block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block) { } 91 1 92
401 91 local $@;
402 91 eval {
403 91 $sighandler_got_sig = 0;
404 91 local $sighandler_should_die = 1;
405 91 ▶ die "exit from eval"
Branch: 1 branch
BranchTrueFalseTotal
if $sighandler_got_sig 0 91 91
406 if $sighandler_got_sig;
407 91 $pid = wait();
408 };
409 91 ▶ if ($pid == -1 && $@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == -1 and $@) 56 35 91
Condition66%2/3
$pid == -1 and $@
!ll&&!rl&&r
35056
MC/DC50%1/2
$pid == -1 and $@
$pid == -1 $@
Truth table66%2/3
A and B
A= $pid == -1B= $@
ABresult
0X0
100
111
410 56 $! = Errno::EINTR;
411 }
412 } else {
413 1 my $pid = waitpid(-1, WNOHANG);
414 }
415 92 ▶ return $pid > 0 ? ($pid, $?) : ();
Branch: 1 branch
BranchTrueFalseTotal
$pid > 0 ? : 35 57 92
416 }
417  
418 62 1;
419 __END__
↑ Summary