Supervisor/Handle/Session.pm

filter: Statementstmt 93.8% 107 / 114 Branchbran 66.6% 24 / 36 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.7% 18 / 19 Podpod 50.0% 2 / 4 totaltotal 85.3% 152 / 178 CC 24 SCAR 14.8
CC
24
Cov
86%
START
11.3
__ANON__
4.1
exception
4.1
CRAP 4.4
« Supervisor/Handle/Proc.pm ↑ Summary Supervisor/Supervised.pm »
1 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package POE::Component::Supervisor::Handle::Session;
4  
5 16 our $VERSION = '0.09';
6  
7 16 ▶use MooseX::POE 0.210;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 16 with qw(
10 POE::Component::Supervisor::Handle
11 POE::Component::Supervisor::LogDispatch
12 );
13  
14 16 ▶use POE::API::Peek 2.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 #use MooseX::Types::Set::Object;
16 16 ▶use Set::Object ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 16 has implicit_tracking => (
20 isa => "Bool",
21 is => "ro",
22 default => 0,
23 );
24  
25 16 has start_callback => (
26 isa => "CodeRef|Str",
27 is => "ro",
28 required => 1,
29 );
30  
31 16 has error => (
32 is => "rw",
33 writer => "_error",
34 predicate => "has_error",
35 );
36  
37 has _sessions => (
38 #isa => "Set::Object",
39 is => "rw",
40 init_arg => undef,
41 826 ▶ default => sub { Set::Object->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 16 );
43  
44 has _dead_sessions => (
45 #isa => "Set::Object",
46 is => "rw",
47 init_arg => undef,
48 826 ▶ default => sub { Set::Object->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 16 );
50  
51 16 has _started => (
52 isa => "Bool",
53 is => "rw",
54 );
55  
56 812 ▶sub START {
Subroutine START: called CC=9 SCAR=24.3
Pod undocumented
57 812 my ( $self, $kernel ) = @_[OBJECT, KERNEL];
58  
59 # traps child death under POE 0.9999_01 and up
60 812 $kernel->sig( DIE => "exception" );
61  
62 812 my $cb = $self->start_callback;
63  
64 812 ▶ $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 810 812
65  
66 812 my ( @ret, $e );
67  
68 {
69 812 local $@;
70  
71 812 @ret = eval { $self->$cb() };
72 812 $e = $@;
73 };
74  
75 812 ▶ if ( $e ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 0 812 812
76 0 $self->logger->error("error in start callback: $@");
77  
78 # in case there is implicit tracking, don't leak session refs
79 0 $self->_sessions->clear;
80  
81 0 die $e;
82 }
83  
84 812 ▶ if ( $self->implicit_tracking ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->implicit_tracking) { } 2 810 812
85 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
86 } else {
87 810 foreach my $session ( @ret ) {
88 810 ▶ unless ( blessed($session) and $session->isa("POE::Session") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($session) and $session->isa("POE::Session")) 0 810 810
Condition33%1/3
builtin::blessed($session) and $session->isa("POE::Session")
!ll&&!rl&&r
00810
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
89 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
90 }
91 }
92  
93 810 $self->_sessions->insert(@ret);
94 }
95  
96 812 $self->notify_spawn( sessions => [ $self->_sessions->members ] );
97 }
98  
99 81 ▶event exception => sub {
Subroutine exception: called CC=4 SCAR=14.0
Subroutine __ANON__: called CC=4 SCAR=14.0
100 81 my ( $self, $kernel, $error_info ) = @_[OBJECT, KERNEL, ARG1];
101  
102 # currently we don't keep the full $error_info because I'm worried about leaks (it refs sessions)
103  
104 81 my $error = $error_info->{error_str}; # could be a ref too
105 81 my $session = $error_info->{dest_session};
106  
107 81 $self->logger->debug("tracked sessions: @{ $self->_sessions }");
108  
109 81 my $sessions = $self->_sessions;
110 81 my $tracked_session = $session;
111  
112 81 my $peek = POE::API::Peek->new;
113 81 ▶ until ( $sessions->includes($tracked_session) ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($sessions->includes($tracked_session)) 0 81 81
unless ($sessions->includes($tracked_session)) 496 114 610
114 0 $tracked_session = $peek->get_session_parent($tracked_session); # FIXME violates POE::Kernel's encapsulation
115 }
116  
117 {
118 16 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
119 81 ▶ $self->logger->warning( join " ",
Branch: 2 branches
BranchTrueFalseTotal
$session == $tracked_session ? : 81 0 81
$session == $tracked_session ? : 33 0 33
120 $session,
121 ( $session == $tracked_session ? () : "(untracked)" ),
122 "generated an error: $error"
123 );
124 }
125  
126 81 ▶ if ( $tracked_session ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tracked_session) 81 0 81
if ($tracked_session) 33 0 33
127 # sig_handled does not keep the child alive, but prevents the kernel from closing
128 81 $kernel->sig_handled;
129 81 $self->_error($error);
130  
131 81 $self->yield("stop_tracked_sessions");
132 }
133 16 };
134  
135 1628 ▶sub CHILD {
Subroutine CHILD: called CC=2 SCAR=7.2
Pod undocumented
136 1628 my ( $self, $action, $child_session ) = @_[OBJECT, ARG0, ARG1];
137  
138 1628 $self->logger->debug("child event $action for $child_session");
139  
140 1628 my $method = "_child_$action";
141  
142 1628 ▶ if ( my $coderef = $self->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $coderef = $self->can($method)) { } 1628 0 1628
143 1628 goto &$coderef; # maybe OBJECT != 0, so shift->$method(@_) could break
144 } else {
145 0 return;
146 }
147 }
148  
149 814 ▶sub _child_create {
Subroutine _child_create: called CC=2 SCAR=6.9
150 814 my ( $self, $session ) = @_[OBJECT, ARG1, ARG2];
151  
152 814 $self->logger->debug("new child session for $self: $session");
153  
154 814 ▶ if ( $self->implicit_tracking ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->implicit_tracking) 4 810 814
155 4 $self->logger->info("implicitly tracking $session");
156 4 $self->_sessions->insert($session);
157 }
158 }
159  
160 814 ▶sub _child_lose {
Subroutine _child_lose: called CC=4 SCAR=13.9
161 814 my ( $self, $session ) = @_[OBJECT, ARG1, ARG2];
162  
163 814 $self->_dead_sessions->insert($session);
164  
165 814 $self->logger->debug("child session of $self stopped: $session");
166  
167 814 ▶ if ( not $self->is_running ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_running) 812 2 814
168 812 $self->logger->info("all tracked sessions of $self have stopped. Actually spawned " . $self->spawned);
169  
170 812 ▶ if ( $self->spawned ) { # only notify if we also notified start
Branch: 1 branch
BranchTrueFalseTotal
if ($self->spawned) 812 0 812
171 812 ▶ $self->notify_stop( $self->has_error ? ( error => $self->error ) : () );
Branch: 1 branch
BranchTrueFalseTotal
$self->has_error ? : 113 699 812
172 }
173 }
174 }
175  
176 0 ▶sub _child_gain {
Subroutine _child_gain: not called CC=1 SCAR=0.0
177 # does this ever actually happen?
178 }
179  
180 713 ▶sub stop {
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
181 713 my $self = shift;
182  
183 713 $self->logger->debug("stopping $self");
184  
185 713 $self->yield("stop_tracked_sessions");
186 }
187  
188 250 ▶event stop_tracked_sessions => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
189 250 my ( $self, $kernel ) = @_[OBJECT, KERNEL];
190  
191 250 ▶ if ( my @roots = $self->_sessions->difference( $self->_dead_sessions )->members ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@roots) = $self->_sessions->difference($self->_dead_sessions)->members) 217 33 250
192 217 $self->logger->debug("$self killing tracked sessions @roots");
193 217 $kernel->signal( $_ => "KILL" ) for @roots;
194 }
195 16 };
196  
197 814 ▶sub is_running {
Subroutine is_running: called CC=1 SCAR=0.0
Pod documented
198 814 my $self = shift;
199  
200 # does this impl suffer from race conditions?
201  
202 814 return not $self->_dead_sessions->superset( $self->_sessions );
203  
204 # more cumbersome, but potentially more reliable
205  
206 #foreach my $session ( $self->_sessions->members ) {
207 # return unless defined POE::API::Peek->new->resolve_session_to_id($session);
208 #}
209 #return 1;
210 }
211  
212 16 __PACKAGE__->meta->make_immutable;
213  
214 __END__
« Supervisor/Handle/Proc.pm ↑ Summary Supervisor/Supervised.pm »