Supervisor/Handle/Session.pm

filter: Statementstmt 79.4% 85 / 107 Branchbran 55.5% 20 / 36 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 88.2% 15 / 17 Podpod 50.0% 2 / 4 totaltotal 72.7% 123 / 169 SCAR 35.8
CC
24
Cov
73%
START
11.3
__ANON__
6.7
exception
6.7
CRAP 35.8
« Supervisor/Handle/Proc.pm ↑ Summary Supervisor.pm »
1 #!/usr/bin/perl
2  
3 package POE::Component::Supervisor::Handle::Session;
4 12 ▶use MooseX::POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 with qw(
7 POE::Component::Supervisor::Handle
8 POE::Component::Supervisor::LogDispatch
9 );
10  
11 12 ▶use POE::API::Peek;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #use MooseX::Types::Set::Object;
14 12 ▶use Set::Object ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 12 has implicit_tracking => (
19 isa => "Bool",
20 is => "ro",
21 default => 0,
22 );
23  
24 12 has start_callback => (
25 isa => "CodeRef|Str",
26 is => "ro",
27 required => 1,
28 );
29  
30 12 has error => (
31 is => "rw",
32 writer => "_error",
33 predicate => "has_error",
34 );
35  
36 has _sessions => (
37 #isa => "Set::Object",
38 is => "rw",
39 init_arg => undef,
40 474 ▶ default => sub { Set::Object->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 12 );
42  
43 has _dead_sessions => (
44 #isa => "Set::Object",
45 is => "rw",
46 init_arg => undef,
47 474 ▶ default => sub { Set::Object->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 12 );
49  
50 12 has _started => (
51 isa => "Bool",
52 is => "rw",
53 );
54  
55 sub START {
56 464 ▶ my ( $self, $kernel ) = @_[OBJECT, KERNEL];
Subroutine START: called CC=9 SCAR=24.3
Pod undocumented
57  
58 # traps child death under POE 0.9999_01 and up
59 464 $kernel->sig( DIE => "exception" );
60  
61 464 my $cb = $self->start_callback;
62  
63 464 ▶ $self->logger->debug("calling start callback $cb, implicit child session tracking is " . ( $self->implicit_tracking ? "on" : "off" ));
Branch: 1 branch
BranchTrueFalseTotal
$self->implicit_tracking ? : 2 462 464
64  
65 464 my ( @ret, $e );
66  
67 {
68 464 local $@;
69  
70 464 @ret = eval { $self->$cb() };
71 464 $e = $@;
72 };
73  
74 464 ▶ if ( $e ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 0 464 464
75 0 $self->logger->error("error in start callback: $@");
76  
77 # in case there is implicit tracking, don't leak session refs
78 0 $self->_sessions->clear;
79  
80 0 die $e;
81 }
82  
83 464 ▶ if ( $self->implicit_tracking ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->implicit_tracking) { } 2 462 464
84 2 ▶ $self->logger->log_and_die( level => "error", message => "No sessions created in callback" ) unless $self->_sessions->size;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_sessions->size 0 2 2
85 } else {
86 462 foreach my $session ( @ret ) {
87 462 ▶ unless ( blessed($session) and $session->isa("POE::Session") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($session) and $session->isa("POE::Session")) 0 462 462
Condition33%1/3
builtin::blessed($session) and $session->isa("POE::Session")
!ll&&!rl&&r
00462
MC/DC0%0/2
builtin::blessed($session) and $session->isa("POE::Session")
builtin::blessed($session) $session->isa("POE::Session")
Truth table33%1/3
A and B
A= builtin::blessed($session)B= $session->isa("POE::Session")
ABresult
0X0
100
111
88 0 ▶ $self->logger->log_and_die( level => "error", message => "return value from start_callback is not a POE session: " . ( defined($session) ? $session : "undef" ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $session ? : 0 0 0
89 }
90 }
91  
92 462 $self->_sessions->insert(@ret);
93 }
94  
95 464 $self->notify_spawn( sessions => [ $self->_sessions->members ] );
96 }
97  
98 event exception => sub {
99 62 ▶ my ( $self, $kernel, $error_info ) = @_[OBJECT, KERNEL, ARG1];
Subroutine exception: called CC=4 SCAR=19.0
Subroutine __ANON__: not called CC=4 SCAR=19.0
100  
101 # currently we don't keep the full $error_info because I'm worried about leaks (it refs sessions)
102  
103 62 my $error = $error_info->{error_str}; # could be a ref too
104 62 my $session = $error_info->{dest_session};
105  
106 62 $self->logger->debug("tracked sessions: @{ $self->_sessions }");
107  
108 62 my $sessions = $self->_sessions;
109 62 my $tracked_session = $session;
110  
111 62 my $peek = POE::API::Peek->new;
112 62 ▶ until ( $sessions->includes($tracked_session) ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($sessions->includes($tracked_session)) 0 62 62
unless ($sessions->includes($tracked_session)) 0 0 0
113 0 $tracked_session = $peek->get_session_parent($tracked_session); # FIXME violates POE::Kernel's encapsulation
114 }
115  
116 {
117 12 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.1
118 62 ▶ $self->logger->warning( join " ",
Branch: 2 branches
BranchTrueFalseTotal
$session == $tracked_session ? : 62 0 62
$session == $tracked_session ? : 0 0 0
119 $session,
120 ( $session == $tracked_session ? () : "(untracked)" ),
121 "generated an error: $error"
122 );
123 }
124  
125 62 ▶ if ( $tracked_session ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tracked_session) 62 0 62
if ($tracked_session) 0 0 0
126 # sig_handled does not keep the child alive, but prevents the kernel from closing
127 62 $kernel->sig_handled;
128 62 $self->_error($error);
129  
130 62 $self->yield("stop_tracked_sessions");
131 }
132 12 };
133  
134 sub CHILD {
135 932 ▶ my ( $self, $action, $child_session ) = @_[OBJECT, ARG0, ARG1];
Subroutine CHILD: called CC=2 SCAR=7.2
Pod undocumented
136  
137 932 $self->logger->debug("child event $action for $child_session");
138  
139 932 my $method = "_child_$action";
140  
141 932 ▶ if ( my $coderef = $self->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $coderef = $self->can($method)) { } 932 0 932
142 932 goto &$coderef; # maybe OBJECT != 0, so shift->$method(@_) could break
143 } else {
144 0 return;
145 }
146 }
147  
148 sub _child_create {
149 466 ▶ my ( $self, $session ) = @_[OBJECT, ARG1, ARG2];
Subroutine _child_create: called CC=2 SCAR=6.9
150  
151 466 $self->logger->debug("new child session for $self: $session");
152  
153 466 ▶ if ( $self->implicit_tracking ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->implicit_tracking) 4 462 466
154 4 $self->logger->info("implicitly tracking $session");
155 4 $self->_sessions->insert($session);
156 }
157 }
158  
159 sub _child_lose {
160 466 ▶ my ( $self, $session ) = @_[OBJECT, ARG1, ARG2];
Subroutine _child_lose: called CC=4 SCAR=13.9
161  
162 466 $self->_dead_sessions->insert($session);
163  
164 466 $self->logger->debug("child session of $self stopped: $session");
165  
166 466 ▶ if ( not $self->is_running ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_running) 464 2 466
167 464 $self->logger->info("all tracked sessions of $self have stopped. Actually spawned " . $self->spawned);
168  
169 464 ▶ if ( $self->spawned ) { # only notify if we also notified start
Branch: 1 branch
BranchTrueFalseTotal
if ($self->spawned) 464 0 464
170 464 ▶ $self->notify_stop( $self->has_error ? ( error => $self->error ) : () );
Branch: 1 branch
BranchTrueFalseTotal
$self->has_error ? : 61 403 464
171 }
172 }
173 }
174  
175 ▶sub _child_gain {
Subroutine _child_gain: not called CC=1 SCAR=0.0
176 # does this ever actually happen?
177 }
178  
179 sub stop {
180 401 ▶ my $self = shift;
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
181  
182 401 $self->logger->debug("stopping $self");
183  
184 401 $self->yield("stop_tracked_sessions");
185 }
186  
187 event stop_tracked_sessions => sub {
188 463 ▶ my ( $self, $kernel ) = @_[OBJECT, KERNEL];
Subroutine __ANON__: called CC=3 SCAR=11.0
189  
190 463 ▶ if ( my @roots = $self->_sessions->difference( $self->_dead_sessions )->members ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@roots) = $self->_sessions->difference($self->_dead_sessions)->members) 401 62 463
191 401 $self->logger->debug("$self killing tracked sessions @roots");
192 401 $kernel->signal( $_ => "KILL" ) for @roots;
193 }
194 12 };
195  
196 sub is_running {
197 466 ▶ my $self = shift;
Subroutine is_running: called CC=1 SCAR=0.0
Pod documented
198  
199 # does this impl suffer from race conditions?
200  
201 466 return not $self->_dead_sessions->superset( $self->_sessions );
202  
203 # more cumbersome, but potentially more reliable
204  
205 #foreach my $session ( $self->_sessions->members ) {
206 # return unless defined POE::API::Peek->new->resolve_session_to_id($session);
207 #}
208 #return 1;
209 }
210  
211 12 __PACKAGE__
212  
213 __END__
214  
215 =pod
216  
217 =head1 NAME
218  
219 POE::Component::Supervisor::Handle::Session - Helps
220 L<POE::Component::Supervisor> babysit POE sessions.
221  
222 =head1 SYNOPSIS
223  
224 # used by L<POE::Component::Supervisor::Supervised::Session>
225  
226 =head1 DESCRIPTION
227  
228 This supervision handle will watch L<POE::Session>s
229  
230 =head1 ATTRIBUTES
231  
232 These attributes apply to the
233 L<POE::Component::Supervisor::Supervised::Session> constructor.
234  
235 =over 4
236  
237 =item start_callback
238  
239 The body of code to run in order to (re)spawn the session(s).
240  
241 If C<implicit_tracking> is off (the default) the sessions to be tracked must be
242 returned from this method.
243  
244 =item implicit_tracking
245  
246 When true, all created child sessions will be implicitly tracked.
247  
248 This means that you can generally just create POE components or sessions in the
249 start callback.
250  
251 Otherwise the sessions to be tracked must be returned from the C<start_callback>.
252  
253 Defaults to false.
254  
255 =item
256  
257 =back
258  
259 =head1 METHODS
260  
261 =over 4
262  
263 =item new
264  
265 Never called directly, only called by L<POE::Component::Supervisor::Supervised::Session>.
266  
267 =item stop
268  
269 Stop the supervised sessions.
270  
271 =item is_running
272  
273 =back
274  
275 =cut
276  
277  
« Supervisor/Handle/Proc.pm ↑ Summary Supervisor.pm »