FCGI/Engine/ProcManager.pm

filter: Statementstmt 87.5% 133 / 152 Branchbran 48.5% 34 / 70 Conditioncond 20.0% 2 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 89.1% 33 / 37 Podpod 0.0% 0 / 20 totaltotal 68.4% 202 / 295 CC 40 SCAR 18.8
CC
40
Cov
73%
post_dispatch
42.0
manager_sig_handler
8.3
manage
7.1
CRAP 6.6
« FCGI/Engine/PSGI.pm ↑ Summary FCGI/Engine/Manager/Server.pm »
1 package FCGI::Engine::ProcManager;
2 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 10 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use POSIX qw(SA_RESTART SIGTERM SIGHUP);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use FCGI::Engine::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use MooseX::Daemonize::Pid::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 our $VERSION = '0.22';
12 10 our $AUTHORITY = 'cpan:STEVAN';
13  
14 has 'role' => (
15 is => 'rw',
16 isa => 'FCGI::Engine::ProcManager::Role',
17 4 ▶ default => sub { 'manager' }
Subroutine __ANON__: called CC=1 SCAR=0.0
18 10 );
19  
20 has 'start_delay' => (
21 is => 'rw',
22 isa => 'Int',
23 4 ▶ default => sub { 0 }
Subroutine __ANON__: called CC=1 SCAR=0.0
24 10 );
25  
26 has 'die_timeout' => (
27 is => 'rw',
28 isa => 'Int',
29 4 ▶ default => sub { 60 }
Subroutine __ANON__: called CC=1 SCAR=0.0
30 10 );
31  
32 has 'n_processes' => (
33 is => 'rw',
34 isa => 'Int',
35 1 ▶ default => sub { 0 }
Subroutine __ANON__: called CC=1 SCAR=0.0
36 10 );
37  
38 10 has 'pidfile' => (
39 is => 'rw',
40 isa => 'MooseX::Daemonize::Pid::File',
41 # coerce => 1,
42 );
43  
44 has 'no_signals' => (
45 is => 'rw',
46 isa => 'Bool',
47 4 ▶ default => sub { 0 }
Subroutine __ANON__: called CC=1 SCAR=0.0
48 10 );
49  
50 10 has 'sigaction_no_sa_restart' => (is => 'rw', isa => 'POSIX::SigAction');
51 10 has 'sigaction_sa_restart' => (is => 'rw', isa => 'POSIX::SigAction');
52  
53 has 'signals_received' => (
54 is => 'rw',
55 isa => 'HashRef',
56 4 ▶ default => sub { +{} }
Subroutine __ANON__: called CC=1 SCAR=0.0
57 10 );
58  
59 10 has 'manager_pid' => (
60 is => 'rw',
61 isa => 'Int',
62 );
63  
64 has 'server_pids' => (
65 traits => [ 'Hash' ],
66 is => 'rw',
67 isa => 'HashRef',
68 clearer => 'forget_all_pids',
69 4 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
70 10 handles => {
71 '_add_pid' => 'set',
72 'get_all_pids' => 'keys',
73 'remove_pid' => 'delete',
74 'has_pids' => 'count',
75 'pid_count' => 'count',
76 }
77 );
78  
79 1 ▶sub add_pid { (shift)->_add_pid( @_, 1 ) }
Subroutine add_pid: called CC=1 SCAR=0.0
Pod undocumented
80  
81 4 ▶has 'process_name' => (is => 'ro', isa => 'Str', default => sub { 'perl-fcgi' });
Subroutine __ANON__: called CC=1 SCAR=0.0
82 4 ▶has 'manager_process_name' => (is => 'ro', isa => 'Str', default => sub { 'perl-fcgi-pm' });
Subroutine __ANON__: called CC=1 SCAR=0.0
83  
84 ## methods ...
85  
86 4 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.0
Pod undocumented
87 4 my $self = shift;
88 4 ▶ unless ($self->no_signals()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->no_signals) 4 0 4
89 4 $self->sigaction_no_sa_restart(
90 POSIX::SigAction->new(
91 'FCGI::Engine::ProcManager::sig_sub'
92 )
93 );
94 4 $self->sigaction_sa_restart(
95 POSIX::SigAction->new(
96 'FCGI::Engine::ProcManager::sig_sub',
97 undef,
98 POSIX::SA_RESTART
99 )
100 );
101 }
102 }
103  
104 # this is the signal handler ...
105 {
106 10 my $SIG_CODEREF;
107  
108 2 ▶ sub sig_sub { $SIG_CODEREF->(@_) if ref $SIG_CODEREF }
Subroutine sig_sub: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if ref $SIG_CODEREF 2 0 2
109  
110 1 ▶ sub clear_signal_handler { undef $SIG_CODEREF }
Subroutine clear_signal_handler: called CC=1 SCAR=0.0
Pod undocumented
111  
112 3 ▶ sub setup_signal_handler {
Subroutine setup_signal_handler: called CC=2 SCAR=7.1
Pod undocumented
113 3 my $self = shift;
114 $SIG_CODEREF = $self->role eq 'manager'
115 1 ▶ ? sub { defined $self && $self->manager_sig_handler(@_) }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $self and $self->manager_sig_handler(@_) 1 0 1
116 3 ▶ : sub { defined $self && $self->server_sig_handler(@_) };
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
$self->role eq 'manager' ? : 2 1 3
defined $self and $self->server_sig_handler(@_) 1 0 1
117 }
118 }
119  
120 ## main loop ...
121  
122 10 ▶sub manage {
Subroutine manage: called CC=7 SCAR=19.6
Pod undocumented
123 3 my $self = shift;
124  
125 # skip to handling now if we won't be managing any processes.
126 3 ▶ $self->n_processes or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->n_processes or return 2 1 3
127  
128 # call the (possibly overloaded) management initialization hook.
129 2 $self->role("manager");
130 2 $self->manager_init;
131 2 $self->notify("initialized");
132  
133 2 my $manager_pid = $$;
134  
135 2 MANAGING_LOOP: while (1) {
136  
137 # FIXME
138 # we should tell the process that it is being
139 # run under some kind of daemon, which will mean
140 # that getppid will usually then return 1
141 # - SL
142 #getppid() == 1 and
143 # return $self->die("calling process has died");
144  
145 2 ▶ $self->n_processes > 0 or
Branch: 1 branch
BranchTrueFalseTotal
$self->n_processes > 0 or return $self->die 2 0 2
146 return $self->die;
147  
148 # while we have fewer servers than we want.
149 2 ▶ PIDS: while ($self->pid_count < $self->n_processes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid_count < $self->n_processes) 2 1 3
150  
151 2 ▶ if (my $pid = fork) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $pid = fork) { } 1 1 2
elsif (not defined $pid) { } 0 1 1
152 # the manager remembers the server.
153 1 $self->add_pid($pid);
154 1 $self->notify("server (pid $pid) started");
155  
156 }
157 elsif (! defined $pid) {
158 0 return $self->abort("fork: $!");
159 }
160 else {
161 1 $self->manager_pid($manager_pid);
162 # the server exits the managing loop.
163 1 last MANAGING_LOOP;
164 }
165  
166 1 ▶ for (my $s = $self->start_delay; $s; $s = sleep $s) {};
Branch: 1 branch
BranchTrueFalseTotal
if ($s) 0 1 1
167 }
168  
169 # this should block until the next server dies.
170 1 $self->wait;
171  
172 }# while 1
173  
174 SERVER:
175  
176 # forget any children we had been collecting.
177 1 $self->forget_all_pids;
178  
179 # call the (possibly overloaded) handling init hook
180 1 $self->role("server");
181 1 $self->server_init;
182 1 $self->notify("initialized");
183  
184 # server returns
185 1 return 1;
186 }
187  
188 ## initializers ...
189  
190 2 ▶sub manager_init {
Subroutine manager_init: called CC=3 SCAR=11.1
Pod undocumented
191 2 my $self = shift;
192  
193 2 ▶ unless ($self->no_signals) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->no_signals) 2 0 2
194 2 $self->setup_signal_actions(with_sa_restart => 0);
195 2 $self->setup_signal_handler;
196 }
197  
198 2 $self->change_process_name;
199  
200 2 eval { $self->pidfile->write };
201 2 ▶ $self->notify("Could not write the PID file because: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
202  
203 2 inner();
204 }
205  
206 1 ▶sub server_init {
Subroutine server_init: called CC=2 SCAR=7.0
Pod undocumented
207 1 my $self = shift;
208  
209 1 ▶ unless ($self->no_signals) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->no_signals) 1 0 1
210 1 $self->setup_signal_actions(with_sa_restart => 0);
211 1 $self->setup_signal_handler;
212 }
213  
214 1 $self->change_process_name;
215  
216 1 inner();
217 }
218  
219  
220 ## hooks ...
221  
222 0 ▶sub pre_dispatch {
Subroutine pre_dispatch: not called CC=2 SCAR=17.9
Pod undocumented
223 0 my $self = shift;
224  
225 0 ▶ $self->setup_signal_actions(with_sa_restart => 1)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->no_signals 0 0 0
226 unless $self->no_signals;
227  
228 0 inner();
229 }
230  
231 0 ▶sub post_dispatch {
Subroutine post_dispatch: not called CC=6 SCAR=37.4
Pod undocumented
232 0 my $self = shift;
233  
234 0 ▶ $self->exit("safe exit after SIGTERM")
Branch: 1 branch
BranchTrueFalseTotal
if $self->received_signal("TERM") 0 0 0
235 if $self->received_signal("TERM");
236  
237 0 ▶ $self->exit("safe exit after SIGHUP")
Branch: 1 branch
BranchTrueFalseTotal
if $self->received_signal("HUP") 0 0 0
238 if $self->received_signal("HUP");
239  
240 0 ▶ if ($self->manager_pid and getppid() != $self->manager_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->manager_pid and getppid != $self->manager_pid) 0 0 0
Condition0%0/3
$self->manager_pid and getppid != $self->manager_pid
!ll&&!rl&&r
000
MC/DC0%0/2
$self->manager_pid and getppid != $self->manager_pid
$self->manager_pid getppid != $self->manager_pid
Truth table0%0/3
A and B
A= $self->manager_pidB= getppid != $self->manager_pid
ABresult
0X0
100
111
241 0 $self->exit("safe exit: manager has died");
242 }
243  
244 0 ▶ $self->setup_signal_actions(with_sa_restart => 0)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->no_signals 0 0 0
245 unless $self->no_signals;
246  
247 0 inner();
248 }
249  
250 ## utils ...
251  
252 # sig-handlers
253  
254 1 ▶sub manager_sig_handler {
Subroutine manager_sig_handler: called CC=4 SCAR=21.1
Pod undocumented
255 1 my ($self, $name) = @_;
256 1 ▶ if ($name eq "TERM") {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'TERM') { } 1 0 1
elsif ($name eq 'HUP') { } 0 0 0
257 1 $self->notify("received signal $name");
258 1 $self->die("safe exit from signal $name");
259 }
260 elsif ($name eq "HUP") {
261 # send a TERM to each of the servers,
262 # and pretend like nothing happened..
263 0 ▶ if (my @pids = $self->get_all_pids) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@pids) = $self->get_all_pids) 0 0 0
264 0 $self->notify("sending TERM to PIDs, @pids");
265 0 kill TERM => @pids;
266 }
267 }
268 else {
269 0 $self->notify("ignoring signal $name");
270 }
271 }
272  
273 1 ▶sub server_sig_handler {
Subroutine server_sig_handler: called CC=1 SCAR=0.0
Pod undocumented
274 1 my ($self, $name) = @_;
275 1 $self->received_signal($name, 1);
276 }
277  
278 1 ▶sub received_signal {
Subroutine received_signal: called CC=3 SCAR=11.4
Pod undocumented
279 1 my ($self, $sig, $received) = @_;
280 1 ▶ return $self->signals_received unless $sig;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig 0 1 1
281 1 ▶ $self->signals_received->{$sig}++ if $received;
Branch: 1 branch
BranchTrueFalseTotal
if $received 1 0 1
282 1 return $self->signals_received->{$sig};
283 }
284  
285 3 ▶sub change_process_name {
Subroutine change_process_name: called CC=2 SCAR=6.9
Pod undocumented
286 3 my $self = shift;
287 3 ▶ $0 = ($self->role eq 'manager' ? $self->manager_process_name : $self->process_name);
Branch: 1 branch
BranchTrueFalseTotal
$self->role eq 'manager' ? : 2 1 3
288 }
289  
290 2 ▶sub wait : method {
Subroutine wait: called CC=3 SCAR=11.4
Pod undocumented
291 2 my $self = shift;
292  
293 # wait for the next server to die.
294 2 ▶ return if (my $pid = CORE::wait()) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = CORE::wait) < 0 0 1 1
295  
296 # notify when one of our servers have died.
297 1 ▶ $self->remove_pid($pid)
Branch: 1 branch
BranchTrueFalseTotal
$self->remove_pid($pid) and $self->notify("server (pid $pid) exited with status $?") 1 0 1
298 and $self->notify("server (pid $pid) exited with status $?");
299  
300 1 return $pid;
301 }
302  
303 ## signal handling stuff ...
304  
305 3 ▶sub setup_signal_actions {
Subroutine setup_signal_actions: called CC=5 SCAR=18.1
Pod undocumented
306 3 my $self = shift;
307 3 my %args = @_;
308  
309 my $sig_action = (exists $args{with_sa_restart} && $args{with_sa_restart})
310 3 ▶ ? $self->sigaction_sa_restart
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'with_sa_restart'} && $args{'with_sa_restart'} ? : 0 3 3
Condition33%1/3
exists $args{"with_sa_restart"} and $args{"with_sa_restart"}
!ll&&!rl&&r
030
MC/DC0%0/2
exists $args{"with_sa_restart"} and $args{"with_sa_restart"}
exists $args{"with_sa_restart"} $args{"with_sa_restart"}
Truth table33%1/3
A and B
A= exists $args{"with_sa_restart"}B= $args{"with_sa_restart"}
ABresult
0X0
100
111
311 : $self->sigaction_no_sa_restart;
312  
313 3 ▶ POSIX::sigaction(POSIX::SIGTERM, $sig_action)
Branch: 1 branch
BranchTrueFalseTotal
POSIX::sigaction(15, $sig_action) or $self->notify("sigaction: SIGTERM: $!") 3 0 3
314 || $self->notify("sigaction: SIGTERM: $!");
315 3 ▶ POSIX::sigaction(POSIX::SIGHUP, $sig_action)
Branch: 1 branch
BranchTrueFalseTotal
POSIX::sigaction(1, $sig_action) or $self->notify("sigaction: SIGHUP: $!") 3 0 3
316 || $self->notify("sigaction: SIGHUP: $!");
317 }
318  
319 ## notification ...
320  
321 8 ▶sub notify {
Subroutine notify: called CC=1 SCAR=0.0
Pod undocumented
322 8 my ($self, $msg) = @_;
323 8 $msg =~ s/\s*$/\n/;
324 8 print STDERR "FastCGI: " . $self->role() . " (pid $$): " . $msg;
325 }
326  
327 ## error/exit handlers ...
328  
329 1 ▶sub die : method {
Subroutine die: called CC=5 SCAR=16.4
Pod undocumented
330 1 my ($self, $msg, $n) = @_;
331  
332 # stop handling signals.
333 1 $self->clear_signal_handler;
334 1 $SIG{HUP} = 'DEFAULT';
335 1 $SIG{TERM} = 'DEFAULT';
336  
337 1 ▶ $self->pidfile->remove
Branch: 1 branch
BranchTrueFalseTotal
$self->pidfile->remove or $self->notify("Could not remove PID file: $!") 1 0 1
338 || $self->notify("Could not remove PID file: $!");
339  
340 # prepare to die no matter what.
341 1 ▶ if (defined $self->die_timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->die_timeout) 1 0 1
342 0 ▶ $SIG{ALRM} = sub { $self->abort("wait timeout") };
Subroutine __ANON__: not called CC=1 SCAR=1.2
343 1 alarm $self->die_timeout;
344 }
345  
346 # send a TERM to each of the servers.
347 1 ▶ if (my @pids = $self->get_all_pids) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@pids) = $self->get_all_pids) 1 0 1
348 1 $self->notify("sending TERM to PIDs, @pids");
349 1 kill TERM => @pids;
350 }
351  
352 # wait for the servers to die.
353 1 ▶ while ($self->has_pids) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_pids) 1 1 2
354 1 $self->wait;
355 }
356  
357 # die already.
358 1 $self->exit("dying: $msg", $n);
359 }
360  
361 0 ▶sub abort {
Subroutine abort: not called CC=2 SCAR=17.9
Pod undocumented
362 0 my ($self, $msg, $n) = @_;
363 0 ▶ $n ||= 1;
Condition0%0/2
$n ||= 1
l!l
00
MC/DC0%0/1
$n ||= 1
$n
Truth table0%0/2
A
A= $n
Aresult
11
00
364 0 $self->exit($msg, 1);
365 }
366  
367 1 ▶sub exit : method {
Subroutine exit: called CC=3 SCAR=11.2
Pod undocumented
368 1 my ($self, $msg, $n) = @_;
369 1 ▶ $n ||= 0;
Condition50%1/2
$n ||= 0
l!l
01
MC/DC0%0/1
$n ||= 0
$n
Truth table50%1/2
A
A= $n
Aresult
11
00
370  
371 # if we still have children at this point,
372 # something went wrong. SIGKILL them now.
373 1 ▶ kill KILL => $self->get_all_pids
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_pids 0 1 1
374 if $self->has_pids;
375  
376 1 $self->notify($msg);
377 1 $@ = $msg;
378 1 CORE::exit $n;
379 }
380  
381 10 1;
382  
383 __END__
« FCGI/Engine/PSGI.pm ↑ Summary FCGI/Engine/Manager/Server.pm »