Supervisor/Handle/Proc.pm

filter: Statementstmt 94.4% 119 / 126 Branchbran 80.0% 32 / 40 Conditioncond 41.0% 16 / 39 MC/DCmcdc 14.2% 3 / 21 Subroutinesub 86.9% 20 / 23 Podpod 50.0% 2 / 4 totaltotal 75.8% 192 / 253 SCAR 42.1
CC
51
Cov
81%
__ANON__
12.0
__ANON__
11.9
_stop_child
11.9
CRAP 67.6
↑ Summary Supervisor/Handle/Session.pm »
1 #!/usr/bin/perl
2  
3 package POE::Component::Supervisor::Handle::Proc;
4 22 ▶use MooseX::POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 22 ▶use POSIX qw(WIFSIGNALED WIFEXITED WEXITSTATUS WTERMSIG);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 22 ▶use POE::Wheel::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 22 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 22 with qw(
13 POE::Component::Supervisor::Handle
14 POE::Component::Supervisor::LogDispatch
15 );
16  
17 22 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 22 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 has wheel_parameters => (
22 isa => "HashRef",
23 is => "ro",
24 auto_deref => 1,
25 382 ▶ default => sub { +{ } },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 22 );
27  
28 22 has enable_nested_poe => (
29 isa => "Bool",
30 is => "ro",
31 default => 1,
32 );
33  
34 22 has start_nested_poe => (
35 isa => "Bool",
36 is => "ro",
37 default => 1,
38 );
39  
40 22 has [map { "std${_}_callback" } qw(out err in)] => (
41 isa => "CodeRef",
42 is => "rw",
43 required => 0,
44 );
45  
46 22 has program => (
47 isa => 'ArrayRef|CodeRef',
48 is => "ro",
49 required => 1,
50 );
51  
52 22 has until_term => (
53 isa => "Num|Undef",
54 is => "ro",
55 default => 0.1,
56 );
57  
58 22 has until_kill => (
59 isa => "Num|Undef",
60 is => "ro",
61 default => 10,
62 );
63  
64 has wait_for => (
65 isa => "Num|Undef",
66 is => "ro",
67 lazy => 1,
68 predicate => "has_wait_for",
69 default => sub {
70 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
71 0 ▶ 5 + ( $self->until_kill || $self->until_term || 0 );
Condition0%0/5
$self->until_kill || $self->until_term || 0
l!l
00
$self->until_kill or $self->until_term
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->until_kill || $self->until_term || 0
$self->until_kill $self->until_term
Truth table0%0/3
A or B
A= $self->until_killB= $self->until_term
ABresult
1X1
011
000
72 },
73 22 );
74  
75 22 has _wheel => (
76 isa => "POE::Wheel::Run",
77 is => "rw",
78 init_arg => undef,
79 clearer => "_clear_wheel",
80 );
81  
82 22 has pid => (
83 isa => "Int",
84 is => "ro",
85 init_arg => undef,
86 writer => "_pid",
87 );
88  
89 22 has exited => (
90 isa => "Int",
91 is => "rw",
92 init_arg => undef,
93 required => 0,
94 predicate => "has_exited",
95 writer => "_exited",
96 );
97  
98 22 has exite_code => (
99 isa => "Int",
100 is => "rw",
101 init_arg => undef,
102 required => 0,
103 predicate => "has_exit_code",
104 writer => "_exit_code",
105 );
106  
107 22 has exit_signal => (
108 isa => "Int",
109 is => "rw",
110 init_arg => undef,
111 required => 0,
112 predicate => "has_exit_signal",
113 writer => "_exit_signal",
114 );
115  
116 sub STOP {
117 680 ▶ $_[OBJECT]->logger->debug("stopping child handle session $_[SESSION]");
Subroutine STOP: called CC=1 SCAR=0.0
Pod undocumented
118 }
119  
120 sub START {
121 639 ▶ my ( $self, $kernel ) = @_[OBJECT, KERNEL];
Subroutine START: called CC=1 SCAR=0.0
Pod undocumented
122  
123 639 $kernel->refcount_increment( $self->get_session_id, __PACKAGE__ );
124  
125 639 my $program = $self->_wrapped_program;
126  
127 639 my $wheel = POE::Wheel::Run->new(
128 StderrEvent => "stderr",
129 StdoutEvent => "stdout",
130 StdinEvent => "stdin",
131 $self->wheel_parameters,
132 Program => $program,
133 );
134
135 621 my $pid = $wheel->PID;
136  
137 621 $self->_wheel($wheel);
138 621 $self->_pid($pid);
139  
140 621 $self->notify_spawn( pid => $pid );
141  
142 621 $kernel->sig_child( $wheel->PID, "child_exit" );
143 }
144  
145 sub _wrapped_program {
146 639 ▶ my ( $self, $program ) = @_;
Subroutine _wrapped_program: called CC=4 SCAR=16.1
147  
148 639 ▶ $program ||= $self->program;
Condition33%1/3
$program ||= $self->program
l!l&&r!l&&!r
06390
MC/DC0%0/2
$program ||= $self->program
$program $self->program
Truth table33%1/3
A ||= B
A= $programB= $self->program
ABresult
1X1
011
000
149  
150 639 ▶ if ( ref($program) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $program eq "CODE") 639 0 639
151 639 ▶ if ( $self->enable_nested_poe ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->enable_nested_poe) 639 0 639
152 639 my $also_start = $self->start_nested_poe;
153 return sub {
154 18 ▶ my @args = @_;
Subroutine __ANON__: called CC=2 SCAR=9.2
155  
156 18 $poe_kernel->stop;
157  
158 18 $program->(@args);
159  
160 0 ▶ $poe_kernel->run if $also_start;
Branch: 1 branch
BranchTrueFalseTotal
if $also_start 0 0 0
161 },
162 639 }
163 }
164  
165 0 return $program;
166 }
167  
168 22 foreach my $event (qw(stdout stderr stdin)) {
169 66 my $cb_name = "${event}_callback";
170 event $event => sub {
171 176 ▶ if ( my $cb = $_[OBJECT]->$cb_name ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Subroutine stdin: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
if (my $cb = $_[0]->$cb_name) 168 8 176
if (my $cb = $_[0]->$cb_name) 895 6 901
172 168 $cb->(@_);
173 }
174 66 };
175 }
176  
177 event child_exit => sub {
178 100 ▶ my ( $self, $exit ) = @_[OBJECT, ARG2];
Subroutine __ANON__: called CC=6 SCAR=17.9
Subroutine child_exit: called CC=6 SCAR=17.9
179  
180 100 ▶ my $exit_code = WIFEXITED($exit) ? WEXITSTATUS($exit) : undef;
Branch: 2 branches
BranchTrueFalseTotal
WIFEXITED($exit) ? : 19 81 100
WIFEXITED($exit) ? : 201 416 617
181 100 ▶ my $exit_signal = WIFSIGNALED($exit) ? WTERMSIG($exit) : undef;
Branch: 2 branches
BranchTrueFalseTotal
WIFSIGNALED($exit) ? : 81 19 100
WIFSIGNALED($exit) ? : 416 201 617
182  
183 100 $self->_exited($exit);
184 100 ▶ $self->_exit_code($exit_code) if defined $exit_code;
Branch: 2 branches
BranchTrueFalseTotal
if defined $exit_code 19 81 100
if defined $exit_code 62 416 478
185 100 ▶ $self->_exit_signal($exit_signal) if defined($exit_signal);
Branch: 2 branches
BranchTrueFalseTotal
if defined $exit_signal 81 19 100
if defined $exit_signal 416 62 478
186  
187 100 ▶ $self->logger->info("child exited with status " . ($exit_code || "undef") . " ($exit), notifying supervisor");
Condition100%5/5
$exit_code || 'undef'
l!l
1288
$exit_code || 'undef'
l!l
56422139
MC/DC100%1/1
$exit_code || 'undef'
$exit_code
MC/DC100%1/1
$exit_code || 'undef'
$exit_code
Truth table100%2/2
A
A= $exit_code
Aresult
11
00
Truth table100%2/2
A
A= $exit_code
Aresult
11
00
188  
189 100 $self->notify_stop(
190 pid => $self->pid,
191 exit => $exit,
192 exit_code => $exit_code,
193 exit_signal => $exit_signal,
194 );
195  
196 100 $self->call("_cleanup");
197  
198 22 };
199  
200 event _cleanup => sub {
201 249 ▶ my ( $self, $kernel ) = @_[OBJECT, KERNEL];
Subroutine _cleanup: called CC=2 SCAR=7.0
202  
203 249 ▶ if ( my $wheel = $self->_wheel ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $wheel = $self->_wheel) 249 0 249
204 249 $wheel->shutdown_stdin;
205 249 $self->_clear_wheel;
206 }
207
208 249 $kernel->alarm_remove_all();
209  
210 249 $kernel->refcount_decrement( $self->get_session_id, __PACKAGE__ );
211 22 };
212  
213 sub stop {
214 503 ▶ my $self = shift;
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
215  
216 503 $self->call("_stop_child");
217 }
218  
219 event _stop_child => sub {
220 364 ▶ my ( $self, $kernel, $heap ) = @_[OBJECT, KERNEL, HEAP];
Subroutine _stop_child: called CC=8 SCAR=24.7
221  
222 364 $self->call("_close_stdin");
223  
224 364 my $now = time;
225  
226 364 my ( $until_term, $until_kill ) = ( $self->until_term, $self->until_kill );
227  
228 364 ▶ my $start_term = defined($until_term) && $now + $until_term;
Condition33%1/3
defined $until_term && $now + $until_term
!ll&&!rl&&r
00364
MC/DC0%0/2
defined $until_term && $now + $until_term
defined $until_term $now + $until_term
Truth table33%1/3
A && B
A= defined $until_termB= $now + $until_term
ABresult
0X0
100
111
229 364 ▶ my $start_kill = defined($until_kill) && $now + $until_kill;
Condition33%1/3
defined $until_kill && $now + $until_kill
!ll&&!rl&&r
00364
MC/DC0%0/2
defined $until_kill && $now + $until_kill
defined $until_kill $now + $until_kill
Truth table33%1/3
A && B
A= defined $until_killB= $now + $until_kill
ABresult
0X0
100
111
230  
231 364 ▶ my $give_up = $self->has_wait_for && $now + $self->wait_for;
Condition33%1/3
$self->has_wait_for && $now + $self->wait_for
!ll&&!rl&&r
36400
MC/DC0%0/2
$self->has_wait_for && $now + $self->wait_for
$self->has_wait_for $now + $self->wait_for
Truth table33%1/3
A && B
A= $self->has_wait_forB= $now + $self->wait_for
ABresult
0X0
100
111
232  
233 364 ▶ $kernel->alarm_set( _term_loop => $start_term, $start_kill || $give_up ) if $start_term;
Branch: 1 branch
BranchTrueFalseTotal
if $start_term 364 0 364
Condition33%1/3
$start_kill || $give_up
l!l&&r!l&&!r
36400
MC/DC0%0/2
$start_kill || $give_up
$start_kill $give_up
Truth table33%1/3
A || B
A= $start_killB= $give_up
ABresult
1X1
011
000
234  
235 364 ▶ $kernel->alarm_set( _kill_loop => $start_kill, $give_up ) if $start_kill;
Branch: 1 branch
BranchTrueFalseTotal
if $start_kill 364 0 364
236  
237 364 ▶ $kernel->alarm_set( _couldnt_kill => $give_up ) if $give_up;
Branch: 1 branch
BranchTrueFalseTotal
if $give_up 0 364 364
238 22 };
239  
240 event _close_stdin => sub {
241 503 ▶ my ( $self, $kernel ) = @_[OBJECT, KERNEL];
Subroutine __ANON__: called CC=2 SCAR=6.9
242  
243 503 $self->logger->info("closing child stdin");
244  
245 503 ▶ if ( my $wheel = $self->_wheel ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $wheel = $self->_wheel) 486 17 503
246 486 $wheel->shutdown_stdin;
247 }
248 22 };
249  
250 22 foreach my $sig (qw(term kill)) {
251 44 my $SIG = uc($sig);
252  
253 44 my $event = "_${sig}_loop";
254  
255 event $event => sub {
256 498 ▶ my ( $self, $kernel, $until, $iter ) = @_[OBJECT, KERNEL, ARG0 .. $#_];
Subroutine _kill_loop: called CC=5 SCAR=17.3
257  
258 498 ▶ $iter ||= 0;
Condition37%3/8
$iter ||= 0
l!l
0498
$iter ||= 0
l!l
00139
$iter ||= 0
l!l
13900
MC/DC0%0/1
$iter ||= 0
$iter
MC/DC0%0/1
$iter ||= 0
$iter
MC/DC0%0/1
$iter ||= 0
$iter
Truth table50%1/2
A
A= $iter
Aresult
11
00
Truth table50%1/2
A
A= $iter
Aresult
11
00
Truth table50%1/2
A
A= $iter
Aresult
11
00
259 498 my $delay = 2 ** $iter / 10; # exponential back off
260 498 my $next_attempt = time() + $delay;
261  
262 498 ▶ if ( !defined($until) or $next_attempt < $until ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $until or $next_attempt < $until) { } 496 2 498
Condition50%3/6
not defined $until or $next_attempt < $until
l!l&&r!l&&!r
04962
not defined $until or $next_attempt < $until
l!l&&r!l&&!r
13900
MC/DC50%1/2
not defined $until or $next_attempt < $until
not defined $until $next_attempt < $until
MC/DC0%0/2
not defined $until or $next_attempt < $until
not defined $until $next_attempt < $until
Truth table66%2/3
not(A) or B
A= not defined $untilB= $next_attempt < $until
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not defined $untilB= $next_attempt < $until
ABresult
1X1
011
000
263 496 $kernel->alarm_set( $event, $next_attempt, $until, $iter + 1 );
264 } else {
265 2 undef $delay;
266 }
267  
268 498 ▶ $self->logger->info("sending SIG$SIG, attempt #" . ( $iter + 1) . ( $delay ? ", next attempt in $delay" : " (last attempt)" ));
Branch: 1 branch
BranchTrueFalseTotal
$delay ? : 496 2 498
269  
270 498 $self->_wheel->kill($SIG);
271 44 };
272 }
273  
274 event _couldnt_kill => sub {
275 0 ▶ die "couldn't kill child";
Subroutine _couldnt_kill: not called CC=1 SCAR=6.9
276 22 };
277  
278 sub is_running {
279 0 ▶ my $self = shift;
Subroutine is_running: not called CC=1 SCAR=6.9
Pod documented
280 0 not $self->has_exited;
281 }
282  
283 22 __PACKAGE__
284  
285 __END__
286  
287 =pod
288  
289 =head1 NAME
290  
291 POE::Component::Supervisor::Handle::Proc - A supervisor child handle for a POSIXish process.
292  
293 =head1 SYNOPSIS
294  
295 # created by POE::Component::Supervisor::Supervised::Proc
296  
297 =head1 DESCRIPTION
298  
299 These objects manage a real UNIX process (signalling, monitoring) within a
300 L<POE::Component::Supervisor>.
301  
302 =head1 SIGNALLING
303  
304 In order to kill a child process first the child's standard input is closed,
305 then the C<TERM> signal is sent, and after a wait period the C<KILL> signal is
306 sent.
307  
308 If the child has not died by the time the C<KILL> loop times out then an error
309 is thrown (this happens under weird OS scenarios and shouldn't happen
310 normally).
311  
312 The attributes C<until_term>, C<until_kill> and C<wait_for> determine the
313 durations of these loops.
314  
315 Initially inputs will be closed. Then, after C<until_term> seconds have passed
316 the C<TERM> sending loop will start, sending the C<TERM> signal with an
317 exponential backoff.
318  
319 When C<until_kill> seconds have passed, from the time of the C<stop> method
320 being called, the C<TERM> loop will be stopped, and instead the C<KILL> signal
321 will be sent, also with an exponential backoff.
322  
323 From the time of the C<stop> method being called the handle will wait for a
324 maximum of C<wait_for> seconds before giving up on the child process.
325  
326 ANy of these attributes may be set to C<undef> to disable their corresponding
327 behaviors (suppress sending of a certain signal, or wait indefinitely).
328  
329 =head1 ATTRIBUTES
330  
331 B<NOTE>: All the attributes are generally passsed in by
332 L<POE::Component::Supervisor::Supervised::Proc>, the factory for this class.
333  
334 They are documented here because that is where their behavior is defined.
335  
336 L<POE::Component::Supervisor::Supervised::Proc> will borrow all the attributes
337 from this class that have an C<init_arg>, and as such they should be passed to
338 L<POE::Component::Supervisor::Supervised::Proc/new>, while this class is never
339 instantiated directly..
340  
341 =over 4
342  
343 =item until_term
344  
345 The time to wait after closing inputs, and before sending the C<TERM> signal.
346 Defaults to one tenth of a second.
347  
348 Set to C<undef> to disable sending the C<TERM> signal.
349  
350 =item until_kill
351  
352 The time to wait after closing inputs, and before sending the C<KILL> signal.
353 Defaults to 10 seconds.
354  
355 Set to C<undef> to disable sending the C<KILL> signal.
356  
357 =item wait_for
358  
359 How long to keep sending exit signals for.
360  
361 Defaults to
362  
363 5 + ( $self->until_kill || $self->until_term || 0 )
364  
365 =item enable_nested_poe
366  
367 Whether or not to call L<POE::Kernel/stop> in the child program, before the
368 callback. Only applies to code references.
369  
370 This allows a nested POE kernel to be started in the forked environment without
371 needing to C<exec> a new program.
372  
373 Defaults to true.
374  
375 =item start_nested_poe
376  
377 Whether or not to call L<POE::Kernel/run> in the child program, after the callback. Only applies to
378 code references.
379  
380 Defaults to true.
381  
382 =item program
383  
384 A coderef or an array ref. Passed as the C<Program> parameter to the wheel, but
385 may be wrapped depending on the values of C<enable_nested_poe> and
386 C<start_nested_poe> if it's a code ref.
387  
388 Required.
389  
390 =item wheel_parameters
391  
392 Additional parameters to pass to L<POE::Wheel::Run/new>.
393  
394 =item stdin_callback
395  
396 =item stdout_callback
397  
398 =item stderr_callback
399  
400 Callbacks to be fired when the corresponding L<POE::Wheel::Run> events are
401 handled.
402  
403 This only affects the default event handlers, if you ovverride those by passing
404 your own C<wheel_parameters> these callbacks will never take effect.
405  
406 The arguments are passed through as is, see L<POE::Wheel::Run> for the details.
407  
408 Not required.
409  
410 =item pid
411  
412 Read only attribute containing the process ID.
413  
414 =item exited
415  
416 =item exit_code
417  
418 =item exit_signal
419  
420 After the process has exited these read only attributes are filled in with the exit information.
421  
422 C<exited> is the raw value of C<$?>, and C<exit_code> and C<exit_signal> are
423 the values of applying C<WEXITSTATUS> and C<WTERMSIG> to that value.
424  
425 See L<POSIX> for details.
426  
427 =item use_logger_singleton
428  
429 Changes the default value of the original L<MooseX::LogDispatch> attribute to
430 true.
431  
432 =back
433  
434 =head1 METHODS
435  
436 =over 4
437  
438 =item new
439  
440 Never called directly, but called by L<POE::Component::Supervisor::Supervised::Proc>.
441  
442 =item stop
443  
444 Stop the running process
445  
446 =item is_running
447  
448 Check whether or not the process is still running.
449  
450 =back
451  
452 =head1 EVENTS
453  
454 All L<POE> events supported by this object are currently internal, and as such
455 the session corresponding to this object provides no useful L<POE> interface.
456  
457 =cut
458  
459  
↑ Summary Supervisor/Handle/Session.pm »