Supervisor.pm

filter: Statementstmt 95.5% 192 / 201 Branchbran 73.5% 25 / 34 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 93.6% 44 / 47 Podpod 40.0% 4 / 10 totaltotal 89.5% 266 / 297 CC 32 SCAR 7.4
CC
32
Cov
92%
stop
5.1
__ANON__
3.0
stopped_unexpectedly
3.0
CRAP 2.1
« Supervisor/Supervised.pm ↑ Summary Supervisor/Handle.pm »
1 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package POE::Component::Supervisor; # git description: 0.08-14-g6e2e02b
4  
5 26 our $VERSION = '0.09';
6  
7 26 ▶use MooseX::POE 0.210;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 26 ▶use POE::Component::Supervisor::Supervised;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 26 ▶use POE::Component::Supervisor::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 ▶use Devel::PartialDump;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use Hash::Util::FieldHash::Compat qw(idhash);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 26 with qw(
16 POE::Component::Supervisor::Interface
17 MooseX::POE::Aliased
18 POE::Component::Supervisor::LogDispatch
19 );
20  
21 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
22 0 my $self = shift->new(@_);
23 0 $poe_kernel->run;
24 }
25  
26 # by default when all the children die we exit as well
27 134 ▶sub _build_alias { undef }
Subroutine _build_alias: called CC=1 SCAR=0.0
28  
29 26 has restart_policy => (
30 isa => enum(__PACKAGE__ . "::RestartPolicy" => [qw(one all rest)]),
31 is => "rw",
32 default => "one",
33 );
34  
35 has children => (
36 isa => "ArrayRef",
37 init_arg => undef,
38 is => "ro",
39 auto_deref => 1,
40 134 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 26 );
42  
43 26 has _children_tmp => (
44 isa => "ArrayRef",
45 is => "rw",
46 init_arg => undef,
47 clearer => "_clear_children_tmp",
48 );
49  
50 26 has _last_child_id => (
51 isa => "Int",
52 is => "rw",
53 default => 0,
54 );
55  
56 1058 ▶sub _next_child_id {
Subroutine _next_child_id: called CC=1 SCAR=0.0
57 1058 my $self = shift;
58 1058 $self->_last_child_id( $self->_last_child_id + 1 );
59 }
60  
61 has _children_hash => (
62 isa => "HashRef",
63 is => "ro",
64 init_arg => undef,
65 134 ▶ default => sub { idhash my %h },
Subroutine __ANON__: called CC=1 SCAR=0.0
66 26 );
67  
68 7207 ▶sub _child_id {
Subroutine _child_id: called CC=2 SCAR=7.6
69 7207 my ( $self, $child ) = @_;
70  
71 7207 ▶ if ( defined ( my $id = $self->_children_hash->{$child}{id} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $id = $self->_children_hash->{$child}{'id'})) { } 7207 0 7207
72 7207 return $id;
73 } else {
74 0 confess "unknown child $child";
75 }
76 }
77  
78 968 ▶sub _child_handle {
Subroutine _child_handle: called CC=1 SCAR=0.0
79 968 my ( $self, $child ) = @_;
80 968 $self->_children_hash->{$child}{handle};
81 }
82  
83 # used to track which children are currently being stopped for the purpose of
84 # restarting, because we first have to stop everything and then we start them again
85 has _stopping_for_restart => (
86 isa => "HashRef",
87 is => "ro",
88 init_arg => undef,
89 134 ▶ default => sub { idhash my %h },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 26 );
91  
92 # when children that are being restarted have stopped they are tracked here
93 # when the last child is stopped this collection of children will be started based on the order of 'children'
94 has _pending_restart => (
95 isa => "HashRef",
96 is => "ro",
97 init_arg => undef,
98 134 ▶ default => sub { idhash my %h },
Subroutine __ANON__: called CC=1 SCAR=0.0
99 26 );
100  
101 134 ▶sub START {
Subroutine START: called CC=2 SCAR=7.0
Pod undocumented
102 134 my ( $self, $kernel ) = @_[OBJECT, KERNEL];
103  
104 134 $kernel->sig( DIE => "exception" );
105  
106 134 $self->logger->info("starting supervisor $self in process $$");
107  
108 134 ▶ if ( my $children = $self->_children_tmp ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $children = $self->_children_tmp) 134 0 134
109 134 $self->_clear_children_tmp;
110 134 $self->start(@$children);
111 }
112 }
113  
114 134 ▶sub STOP {
Subroutine STOP: called CC=1 SCAR=0.0
Pod undocumented
115 134 my $self = $_[OBJECT];
116  
117 134 $self->logger->info("stopping supervisor $self in process $$");
118 }
119  
120 93 ▶event exception => sub {
Subroutine exception: called CC=1 SCAR=0.0
121 93 my ( $self, $error_info ) = @_[OBJECT, ARG1];
122  
123 93 $self->logger->error("Error in supervisor child session, event $error_info->{event} of $error_info->{dest_session}: $error_info->{error_str}, sent from $error_info->{source_session} state $error_info->{from_state} at $error_info->{file} line $error_info->{line}");
124 26 };
125  
126 1058 ▶sub _register_child {
Subroutine _register_child: called CC=2 SCAR=7.1
127 1058 my ( $self, $new_child ) = @_;
128  
129 1058 $self->logger->debug("registering child $new_child");
130  
131 1058 ▶ $self->_children_hash->{$new_child} ||= do {
Condition33%1/3
$self->_children_hash->{$new_child} ||= do { push @{$self->children;}, $new_child; $self->_new_child_registration($new_child) }
l!l&&r!l&&!r
010580
MC/DC0%0/2
$self->_children_hash->{$new_child} ||= do { push @{$self->children;}, $new_child; $self->_new_child_registration($new_child) }
$self->_children_hash->{$new_child} do { push @{$self->children;}, $new_child; $self->_new_child_registration($new_child) }
Truth table33%1/3
A ||= B
A= $self->_children_hash->{$new_child}B= do { push @{$self->children;}, $new_child; $self->_new_child_registration($new_child) }
ABresult
1X1
011
000
132 1058 push @{ $self->children }, $new_child;
133 1058 $self->_new_child_registration($new_child);
134 }
135 }
136  
137 1058 ▶sub _new_child_registration {
Subroutine _new_child_registration: called CC=1 SCAR=0.0
138 1058 my ( $self, $new_child ) = @_;
139 1058 return { id => $self->_next_child_id };
140 }
141  
142 891 ▶sub _unregister_child {
Subroutine _unregister_child: called CC=2 SCAR=7.0
143 891 my ( $self, $child ) = @_;
144  
145 891 $self->logger->debug("unregistering child $child");
146  
147 891 ▶ if ( delete $self->_children_hash->{$child} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $self->_children_hash->{$child}) 891 0 891
148 891 @{ $self->children } = grep { $_ != $child } @{ $self->children };
149 }
150  
151 }
152  
153 134 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.1
Pod undocumented
154 134 my ( $self, $params ) = @_;
155  
156 134 ▶ if ( my $children = $params->{children} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $children = $params->{"children"}) 134 0 134
157 134 $self->_children_tmp($children);
158 }
159 }
160  
161 134 ▶sub start {
Subroutine start: called CC=3 SCAR=11.1
Pod documented
162 134 my ( $self, @children ) = @_;
163  
164 134 foreach my $child ( @children ) {
165 1058 ▶ next if $self->_children_hash->{$child};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_children_hash->{$child} 0 1058 1058
166 1058 $self->_register_child($child);
167 1058 $self->yield( spawn => $child );
168 }
169 }
170  
171 458 ▶sub stop {
Subroutine stop: called CC=5 SCAR=16.3
Pod documented
172 458 my ( $self, @children ) = @_;
173  
174 458 ▶ @children = reverse $self->children unless @children;
Branch: 1 branch
BranchTrueFalseTotal
unless @children 436 22 458
175  
176 458 foreach my $child ( @children ) {
177 882 ▶ if ( my $entry = $self->_children_hash->{$child} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->_children_hash->{$child}) 882 0 882
178 882 ▶ if ( my $handle = $entry->{handle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handle = $entry->{'handle'}) { } 882 0 882
179 882 $entry->{stopping} = 1;
180 882 $entry->{handle}->stop;
181  
182 # remove it from the children list, so that it isn't restarted due to a
183 # 'rest' or 'all' policy because of some other childs' exit
184 # _unregister_child will eventually try to do this too, but that's OK
185 # because it *should* do it if the child has been stopped unexpectedly
186 # and is temporary/transient
187 882 @{ $self->children } = grep { $_ != $child } @{ $self->children };
188 } else {
189 # it's already dead, just delete it
190 0 $self->_unregister_child($child);
191 }
192 }
193 }
194 }
195  
196 1933 ▶sub notify_spawned {
Subroutine notify_spawned: called CC=1 SCAR=0.0
Pod undocumented
197 1933 my ( $self, @args ) = @_;
198 1933 $self->yield( spawned => @args );
199 }
200  
201 1879 ▶sub notify_stopped {
Subroutine notify_stopped: called CC=1 SCAR=0.0
Pod undocumented
202 1879 my ( $self, @args ) = @_;
203 1879 $self->yield( stopped => @args );
204 }
205  
206 1266 ▶event spawned => sub {
Subroutine spawned: called CC=1 SCAR=0.0
207 1266 my ( $self, $kernel, $child, @args ) = @_[OBJECT, KERNEL, ARG0 .. $#_];
208  
209 1266 $kernel->refcount_increment( $self->get_session_id(), "handles" );
210  
211 1266 $self->logger->info("child " . $self->_child_id($child) . " spawned " . Devel::PartialDump::dump(@args));
212 26 };
213  
214 734 ▶event spawn => sub {
Subroutine spawn: called CC=1 SCAR=0.0
215 734 my ( $self, $child ) = @_[OBJECT, ARG0 ];
216  
217 734 $self->logger->debug("instructing child " . $self->_child_id($child) . " to spawn");
218  
219 734 $self->_children_hash->{$child}{handle} = $child->spawn( supervisor => $self );
220 26 };
221  
222 307 ▶event respawn => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
223 307 my ( $self, $child ) = @_[OBJECT, ARG0];
224  
225 307 $self->_pending_restart->{$child} = $child;
226  
227 307 ▶ if ( scalar keys %{ $self->_stopping_for_restart } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->_stopping_for_restart;}) { } 139 168 307
228 # if we're waiting on more children to exit, just mark this child as ready to restart
229 139 $self->logger->debug("child " . $self->_child_id($child) . " respawn postponed, other children still not stopped");
230 } else {
231 # otherwise we can now restart all the children which are ready to be restarted
232 168 $self->logger->debug("no more unstopped children, ready to respawn");
233 168 my @children_to_restart = grep { defined } delete @{ $self->_pending_restart }{ $self->children };
234  
235 168 foreach my $child ( @children_to_restart ) {
236 307 $self->yield( _respawn => $child );
237 }
238 }
239 26 };
240  
241 678 ▶event _respawn => sub {
Subroutine _respawn: called CC=1 SCAR=0.0
242 678 my ( $self, $child ) = @_[OBJECT, ARG0];
243  
244 678 $self->logger->info("respawning child " . $self->_child_id($child));
245 678 $self->_children_hash->{$child}{handle} = $child->respawn( supervisor => $self );
246 26 };
247  
248 1859 ▶event stopped => sub {
Subroutine stopped: called CC=3 SCAR=11.0
249 1859 my ( $self, $kernel, $child, @args ) = @_[OBJECT, KERNEL, ARG0 .. $#_];
250  
251 1859 $kernel->refcount_decrement( $self->get_session_id(), "handles" );
252  
253 1859 delete $self->_children_hash->{$child}{handle};
254  
255 1859 ▶ if ( $self->_children_hash->{$child}{stopping} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_children_hash->{$child}{'stopping'}) { } 876 983 1859
elsif (my $restarting = delete $self->_stopping_for_restart->{$child}) { } 781 202 983
if ($self->_children_hash->{$child}{'stopping'}) { } 0 68 68
256 876 $self->call( stopped_per_request => $child, @args );
257 } elsif ( my $restarting = delete $self->_stopping_for_restart->{$child} ) {
258 781 $self->call( stopped_for_restart => $child, @args );
259 } else {
260 202 $self->call( stopped_unexpectedly => $child, @args );
261 }
262 26 };
263  
264 399 ▶event stopped_per_request => sub {
Subroutine stopped_per_request: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
265 399 my ( $self, $kernel, $child, @args ) = @_[OBJECT, KERNEL, ARG0 .. $#_];
266  
267 399 $self->logger->info("child " . $self->_child_id($child) . " exited as requested");
268  
269 399 $self->_unregister_child($child);
270 399 $kernel->refcount_decrement( $self->get_session_id(), "children" );
271 26 };
272  
273 133 ▶event stopped_unexpectedly => sub {
Subroutine stopped_unexpectedly: called CC=3 SCAR=11.1
274 133 my ( $self, $kernel, $child, @args ) = @_[OBJECT, KERNEL, ARG0 .. $#_];
275  
276 133 my $id = $self->_child_id($child);
277  
278 133 $self->logger->notice("child $id exited on its own");
279  
280 133 ▶ if ( $self->should_restart_child($child, @args) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->should_restart_child($child, @args)) { } 119 14 133
281 119 ▶ if ( $self->child_exit_is_fatal($child, @args) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->child_exit_is_fatal($child, @args)) { } 0 119 119
282 0 $self->logger->error("child $id exit is fatal, raising error");
283 0 $self->yield( fatal_exit => $child, @args );
284 } else {
285 119 my $policy = $self->restart_policy;
286 119 $self->logger->info("child $id will be restarted, restart policy is $policy");
287 119 $self->yield( "restart_$policy" => $child, @args );
288 }
289 } else {
290 14 $self->logger->info("child $id won't be restarted");
291 14 $self->_unregister_child($child);
292 14 $kernel->refcount_decrement( $self->get_session_id(), "children" );
293 }
294 26 };
295  
296 781 ▶event stopped_for_restart => sub {
Subroutine stopped_for_restart: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
297 781 my ( $self, $child, %args ) = @_[OBJECT, ARG0 .. $#_];
298  
299 781 $self->logger->info("child " . $self->_child_id($child) . " exited for restart as requested");
300  
301 781 $self->yield( respawn => $child );
302 26 };
303  
304 968 ▶event restart_one => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
Subroutine restart_one: called CC=2 SCAR=6.9
305 968 my ( $self, $child, %args ) = @_[OBJECT, ARG0 .. $#_];
306  
307 968 ▶ if ( my $handle = $self->_child_handle($child) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $handle = $self->_child_handle($child)) { } 781 187 968
if (my $handle = $self->_child_handle($child)) { } 398 332 730
308 781 $self->logger->info("stopping child " . $self->_child_id($child) . " for restart");
309 781 $self->_stopping_for_restart->{$child} = 1;
310 781 $handle->stop_for_restart();
311 } else {
312 187 $self->logger->debug("child " . $self->_child_id($child) . " is already dead, marking for respawn");
313 187 $self->yield( respawn => $child );
314 }
315 26 };
316  
317 59 ▶event restart_all => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
318 59 my ( $self, $child, %args ) = @_[OBJECT, ARG0 .. $#_];
319  
320 59 foreach my $child ( reverse $self->children ) {
321 590 $self->yield( restart_one => $child );
322 }
323 26 };
324  
325 50 ▶event restart_rest => sub {
Subroutine restart_rest: called CC=3 SCAR=11.0
326 50 my ( $self, $child, %args ) = @_[OBJECT, ARG0 .. $#_];
327  
328 50 my @children = $self->children;
329  
330 50 ▶ shift @children while $children[0] != $child;
Branch: 1 branch
BranchTrueFalseTotal
$children[0] != $child and do { shift @children } 200 50 250
331  
332 50 foreach my $child ( reverse @children ) {
333 300 $self->yield( restart_one => $child );
334 }
335 26 };
336  
337 0 ▶event fatal_exit => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
338 # stop everything
339 # exit with error
340 # FIXME how do we exit abstractly? yield to some callback?
341  
342 0 die "supervisor seppuku";
343 26 };
344  
345 187 ▶sub child_exit_is_fatal {
Subroutine child_exit_is_fatal: called CC=1 SCAR=0.0
Pod documented
346 187 my ( $self, $child, @args ) = @_;
347  
348 # check if the child exceeded the maximal number of restarts by looking at
349 # $self->_children_hash->{$child}{token_bucket}, the child descriptor's
350 # restart policy (if its transient or not, etc), $args{exit_code} being an
351 # error, etc
352  
353 187 return 0;
354 }
355  
356 202 ▶sub should_restart_child {
Subroutine should_restart_child: called CC=1 SCAR=0.0
Pod documented
357 202 my ( $self, $child, @args ) = @_;
358  
359 202 $child->should_restart(@args);
360 }
361  
362 0 ▶event _child => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
363 0 my ( $self, $verb, $child ) = @_[OBJECT, ARG0, ARG1];
364  
365 0 $self->logger->debug("supervisor $self had child event for $child: $verb");
366 26 };
367  
368 26 __PACKAGE__->meta->make_immutable;
369  
370 __END__
« Supervisor/Supervised.pm ↑ Summary Supervisor/Handle.pm »