Supervisor/Handle/Proc.pm

filter: Statementstmt 93.8% 123 / 131 Branchbran 71.0% 27 / 38 Conditioncond 39.3% 13 / 33 MC/DCmcdc 5.0% 1 / 20 Subroutinesub 87.5% 21 / 24 Podpod 50.0% 2 / 4 totaltotal 74.8% 187 / 250 CC 51 SCAR 16.6
CC
51
Cov
81%
__ANON__
12.0
__ANON__
10.4
_stop_child
10.4
CRAP 5.3
↑ Summary Supervisor/Handle/Session.pm »
1 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package POE::Component::Supervisor::Handle::Proc;
4  
5 25 our $VERSION = '0.09';
6  
7 25 ▶use MooseX::POE 0.210;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use POSIX qw(WIFSIGNALED WIFEXITED WEXITSTATUS WTERMSIG);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 ▶use POE::Wheel::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 25 with qw(
15 POE::Component::Supervisor::Handle
16 POE::Component::Supervisor::LogDispatch
17 );
18  
19 has wheel_parameters => (
20 isa => "HashRef",
21 is => "ro",
22 auto_deref => 1,
23 642 ▶ default => sub { +{ } },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 25 );
25  
26 25 has enable_nested_poe => (
27 isa => "Bool",
28 is => "ro",
29 default => 1,
30 );
31  
32 25 has start_nested_poe => (
33 isa => "Bool",
34 is => "ro",
35 default => 1,
36 );
37  
38 25 has [map { "std${_}_callback" } qw(out err in)] => (
39 isa => "CodeRef",
40 is => "rw",
41 required => 0,
42 );
43  
44 25 has program => (
45 isa => 'ArrayRef|CodeRef',
46 is => "ro",
47 required => 1,
48 );
49  
50 25 has until_term => (
51 isa => "Num|Undef",
52 is => "ro",
53 default => 0.1,
54 );
55  
56 25 has until_kill => (
57 isa => "Num|Undef",
58 is => "ro",
59 default => 10,
60 );
61  
62 has wait_for => (
63 isa => "Num|Undef",
64 is => "ro",
65 lazy => 1,
66 predicate => "has_wait_for",
67 0 ▶ default => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
68 0 my $self = shift;
69 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
70 },
71 25 );
72  
73 25 has _wheel => (
74 isa => "POE::Wheel::Run",
75 is => "rw",
76 init_arg => undef,
77 clearer => "_clear_wheel",
78 );
79  
80 25 has pid => (
81 isa => "Int",
82 is => "ro",
83 init_arg => undef,
84 writer => "_pid",
85 );
86  
87 25 has exited => (
88 isa => "Int",
89 is => "rw",
90 init_arg => undef,
91 required => 0,
92 predicate => "has_exited",
93 writer => "_exited",
94 );
95  
96 25 has exite_code => (
97 isa => "Int",
98 is => "rw",
99 init_arg => undef,
100 required => 0,
101 predicate => "has_exit_code",
102 writer => "_exit_code",
103 );
104  
105 25 has exit_signal => (
106 isa => "Int",
107 is => "rw",
108 init_arg => undef,
109 required => 0,
110 predicate => "has_exit_signal",
111 writer => "_exit_signal",
112 );
113  
114 1200 ▶sub STOP {
Subroutine STOP: called CC=1 SCAR=0.0
Pod undocumented
115 1200 $_[OBJECT]->logger->debug("stopping child handle session $_[SESSION]");
116 }
117  
118 1142 ▶sub START {
Subroutine START: called CC=1 SCAR=0.0
Pod undocumented
119 1142 my ( $self, $kernel ) = @_[OBJECT, KERNEL];
120  
121 1142 $kernel->refcount_increment( $self->get_session_id, __PACKAGE__ );
122  
123 1142 my $program = $self->_wrapped_program;
124  
125 1142 my $wheel = POE::Wheel::Run->new(
126 StderrEvent => "stderr",
127 StdoutEvent => "stdout",
128 StdinEvent => "stdin",
129 $self->wheel_parameters,
130 Program => $program,
131 );
132  
133 1121 my $pid = $wheel->PID;
134  
135 1121 $self->_wheel($wheel);
136 1121 $self->_pid($pid);
137  
138 1121 $self->notify_spawn( pid => $pid );
139  
140 1121 $kernel->sig_child( $wheel->PID, "child_exit" );
141 }
142  
143 1142 ▶sub _wrapped_program {
Subroutine _wrapped_program: called CC=4 SCAR=16.1
144 1142 my ( $self, $program ) = @_;
145  
146 1142 ▶ $program ||= $self->program;
Condition33%1/3
$program ||= $self->program
l!l&&r!l&&!r
011420
MC/DC0%0/2
$program ||= $self->program
$program $self->program
Truth table33%1/3
A ||= B
A= $programB= $self->program
ABresult
1X1
011
000
147  
148 1142 ▶ if ( ref($program) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $program eq "CODE") 1142 0 1142
149 1142 ▶ if ( $self->enable_nested_poe ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->enable_nested_poe) 1142 0 1142
150 1142 my $also_start = $self->start_nested_poe;
151 21 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
152 21 my @args = @_;
153  
154 21 $poe_kernel->stop;
155  
156 21 $program->(@args);
157  
158 0 ▶ $poe_kernel->run if $also_start;
Branch: 1 branch
BranchTrueFalseTotal
if $also_start 0 0 0
159 },
160 1142 }
161 }
162  
163 0 return $program;
164 }
165  
166 25 foreach my $event (qw(stdout stderr stdin)) {
167 75 my $cb_name = "${event}_callback";
168 498 ▶ event $event => sub {
Subroutine stdin: called CC=2 SCAR=7.0
Subroutine stdout: called CC=2 SCAR=7.0
169 498 ▶ if ( my $cb = $_[OBJECT]->$cb_name ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $cb = $_[0]->$cb_name) 498 0 498
if (my $cb = $_[0]->$cb_name) 940 590 1530
170 498 $cb->(@_);
171 }
172 75 };
173 }
174  
175 398 ▶event child_exit => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
176 398 my ( $self, $exit ) = @_[OBJECT, ARG2];
177  
178 398 ▶ my $exit_code = WIFEXITED($exit) ? WEXITSTATUS($exit) : undef;
Branch: 1 branch
BranchTrueFalseTotal
WIFEXITED($exit) ? : 42 356 398
179 398 ▶ my $exit_signal = WIFSIGNALED($exit) ? WTERMSIG($exit) : undef;
Branch: 1 branch
BranchTrueFalseTotal
WIFSIGNALED($exit) ? : 356 42 398
180  
181 398 $self->_exited($exit);
182 398 ▶ $self->_exit_code($exit_code) if defined $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if defined $exit_code 42 356 398
183 398 ▶ $self->_exit_signal($exit_signal) if defined($exit_signal);
Branch: 1 branch
BranchTrueFalseTotal
if defined $exit_signal 356 42 398
184  
185 398 ▶ $self->logger->info("child exited with status " . ($exit_code || "undef") . " ($exit), notifying supervisor");
Condition100%2/2
$exit_code || 'undef'
l!l
41357
MC/DC100%1/1
$exit_code || 'undef'
$exit_code
Truth table100%2/2
A
A= $exit_code
Aresult
11
00
186  
187 398 $self->notify_stop(
188 pid => $self->pid,
189 exit => $exit,
190 exit_code => $exit_code,
191 exit_signal => $exit_signal,
192 );
193  
194 398 $self->call("_cleanup");
195  
196 25 };
197  
198 1067 ▶event _cleanup => sub {
Subroutine _cleanup: called CC=2 SCAR=6.9
199 1067 my ( $self, $kernel ) = @_[OBJECT, KERNEL];
200  
201 1067 ▶ if ( my $wheel = $self->_wheel ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $wheel = $self->_wheel) 1067 0 1067
if (my $wheel = $self->_wheel) 879 83 962
202 1067 $wheel->shutdown_stdin;
203 1067 $self->_clear_wheel;
204 }
205  
206 1067 $kernel->alarm_remove_all();
207  
208 1067 $kernel->refcount_decrement( $self->get_session_id, __PACKAGE__ );
209 25 };
210  
211 950 ▶sub stop {
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
212 950 my $self = shift;
213  
214 950 $self->call("_stop_child");
215 }
216  
217 950 ▶event _stop_child => sub {
Subroutine __ANON__: called CC=8 SCAR=23.4
218 950 my ( $self, $kernel, $heap ) = @_[OBJECT, KERNEL, HEAP];
219  
220 950 $self->call("_close_stdin");
221  
222 950 my $now = time;
223  
224 950 my ( $until_term, $until_kill ) = ( $self->until_term, $self->until_kill );
225  
226 950 ▶ my $start_term = defined($until_term) && $now + $until_term;
Condition50%3/6
defined $until_term && $now + $until_term
!ll&&!rl&&r
00950
defined $until_term && $now + $until_term
!ll&&!rl&&r
708920
MC/DC0%0/2
defined $until_term && $now + $until_term
defined $until_term $now + $until_term
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
Truth table0%0/3
A && B
A= defined $until_termB= $now + $until_term
ABresult
0X0
100
111
227 950 ▶ my $start_kill = defined($until_kill) && $now + $until_kill;
Condition50%3/6
defined $until_kill && $now + $until_kill
!ll&&!rl&&r
00950
defined $until_kill && $now + $until_kill
!ll&&!rl&&r
02912
MC/DC0%0/2
defined $until_kill && $now + $until_kill
defined $until_kill $now + $until_kill
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
Truth table0%0/3
A && B
A= defined $until_killB= $now + $until_kill
ABresult
0X0
100
111
228  
229 950 ▶ 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
95000
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
230  
231 950 ▶ $kernel->alarm_set( _term_loop => $start_term, $start_kill || $give_up ) if $start_term;
Branch: 2 branches
BranchTrueFalseTotal
if $start_term 950 0 950
if $start_term 81 588 669
Condition33%1/3
$start_kill || $give_up
l!l&&r!l&&!r
95000
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
232  
233 950 ▶ $kernel->alarm_set( _kill_loop => $start_kill, $give_up ) if $start_kill;
Branch: 2 branches
BranchTrueFalseTotal
if $start_kill 950 0 950
if $start_kill 588 81 669
234  
235 950 ▶ $kernel->alarm_set( _couldnt_kill => $give_up ) if $give_up;
Branch: 1 branch
BranchTrueFalseTotal
if $give_up 0 950 950
236 25 };
237  
238 950 ▶event _close_stdin => sub {
Subroutine _close_stdin: called CC=2 SCAR=6.9
239 950 my ( $self, $kernel ) = @_[OBJECT, KERNEL];
240  
241 950 $self->logger->info("closing child stdin");
242  
243 950 ▶ if ( my $wheel = $self->_wheel ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $wheel = $self->_wheel) 883 67 950
244 883 $wheel->shutdown_stdin;
245 }
246 25 };
247  
248 25 foreach my $sig (qw(term kill)) {
249 50 my $SIG = uc($sig);
250  
251 50 my $event = "_${sig}_loop";
252  
253 652 ▶ event $event => sub {
Subroutine _term_loop: called CC=5 SCAR=17.8
254 652 my ( $self, $kernel, $until, $iter ) = @_[OBJECT, KERNEL, ARG0 .. $#_];
255  
256 652 ▶ $iter ||= 0;
Condition50%1/2
$iter ||= 0
l!l
0652
MC/DC0%0/1
$iter ||= 0
$iter
Truth table50%1/2
A
A= $iter
Aresult
11
00
257 652 my $delay = 2 ** $iter / 10; # exponential back off
258 652 my $next_attempt = time() + $delay;
259  
260 652 ▶ if ( !defined($until) or $next_attempt < $until ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $until or $next_attempt < $until) { } 652 0 652
Condition33%1/3
not defined $until or $next_attempt < $until
l!l&&r!l&&!r
06520
MC/DC0%0/2
not defined $until or $next_attempt < $until
not defined $until $next_attempt < $until
Truth table33%1/3
not(A) or B
A= not defined $untilB= $next_attempt < $until
ABresult
1X1
011
000
261 652 $kernel->alarm_set( $event, $next_attempt, $until, $iter + 1 );
262 } else {
263 0 undef $delay;
264 }
265  
266 652 ▶ $self->logger->info("sending SIG$SIG, attempt #" . ( $iter + 1) . ( $delay ? ", next attempt in $delay" : " (last attempt)" ));
Branch: 1 branch
BranchTrueFalseTotal
$delay ? : 652 0 652
267  
268 652 $self->_wheel->kill($SIG);
269 50 };
270 }
271  
272 0 ▶event _couldnt_kill => sub {
Subroutine _couldnt_kill: not called CC=1 SCAR=6.9
273 0 die "couldn't kill child";
274 25 };
275  
276 0 ▶sub is_running {
Subroutine is_running: not called CC=1 SCAR=6.9
Pod documented
277 0 my $self = shift;
278 0 not $self->has_exited;
279 }
280  
281 25 __PACKAGE__->meta->make_immutable;
282  
283 __END__
↑ Summary Supervisor/Handle/Session.pm »