Manager/Role/Service.pm

filter: Statementstmt 94.7% 307 / 324 Branchbran 78.9% 109 / 138 Conditioncond 55.7% 29 / 52 MC/DCmcdc 25.8% 8 / 31 Subroutinesub 82.2% 65 / 79 Podpod 90.9% 40 / 44 totaltotal 83.5% 558 / 668 CC 97 SCAR 24.3
CC
97
Cov
87%
run
31.8
watch
30.8
reap_workers
7.1
CRAP 11.4
« Manager/Spawn.pm ↑ Summary Manager/Base/DBI.pm »
1 package IPC::Manager::Role::Service;
2 198 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 198 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 198 our $VERSION = '0.000035';
6  
7 # Not included in role:
8 198 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 198 ▶use POSIX qw/:sys_wait_h/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 198 ▶use List::Util qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 198 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 198 ▶use Test2::Util::UUID qw/gen_uuid/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 198 ▶use IPC::Manager::Service::Handle();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 198 ▶use IPC::Manager::Service::Peer();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 198 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 198 with 'IPC::Manager::Role::Service::Select';
20 198 with 'IPC::Manager::Role::Service::Requests';
21  
22 # Included in role:
23 198 ▶use IPC::Manager::Util qw/pid_is_running tinysleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 198 requires qw{
26 new
27 orig_io
28 name
29 run
30 ipcm_info
31 pid
32 set_pid
33 watch_pids
34 handle_request
35 };
36  
37 279 ▶sub cycle { 0.2 }
Subroutine cycle: called CC=1 SCAR=0.0
Pod documented
38 263 ▶sub interval { 0.2 }
Subroutine interval: called CC=1 SCAR=0.0
Pod documented
39 280 ▶sub use_posix_exit { 0 }
Subroutine use_posix_exit: called CC=1 SCAR=0.0
Pod documented
40 274 ▶sub intercept_errors { 0 }
Subroutine intercept_errors: called CC=1 SCAR=0.0
Pod documented
41 263 ▶sub expose_error_details { 0 }
Subroutine expose_error_details: called CC=1 SCAR=0.0
Pod documented
42  
43 748 ▶sub terminated { $_[0]->{_TERMINATED} }
Subroutine terminated: called CC=1 SCAR=0.0
Pod documented
44 369 ▶sub is_terminated { defined $_[0]->{_TERMINATED} ? 1 : 0 }
Subroutine is_terminated: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'_TERMINATED'} ? : 105 264 369
45 106 ▶sub terminate { $_[0]->{_TERMINATED} ||= pop if @_ > 1; $_[0]->{_TERMINATED} //= 0 }
Subroutine terminate: called CC=4 SCAR=14.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 106 0 106
Condition80%4/5
$_[0]{"_TERMINATED"} ||= pop()
l!l&&r!l&&!r
11104
$_[0]{"_TERMINATED"} //= 0
l!l
1060
MC/DC100%2/2
$_[0]{"_TERMINATED"} ||= pop()
$_[0]{"_TERMINATED"} pop()
MC/DC0%0/1
$_[0]{"_TERMINATED"} //= 0
$_[0]{"_TERMINATED"}
Truth table100%3/3
A ||= B
A= $_[0]{"_TERMINATED"}B= pop()
ABresult
1X1
011
000
Truth table50%1/2
A
A= $_[0]{"_TERMINATED"}
Aresult
11
00
46  
47 9 ▶sub peer_class { 'IPC::Manager::Service::Peer' }
Subroutine peer_class: called CC=1 SCAR=0.0
Pod documented
48 142 ▶sub handle_class { 'IPC::Manager::Service::Handle' }
Subroutine handle_class: called CC=1 SCAR=0.0
Pod documented
49  
50 0 ▶sub signals_to_grab { () }
Subroutine signals_to_grab: not called CC=1 SCAR=6.9
Pod documented
51  
52 ▶sub redirect { }
Subroutine redirect: not called CC=1 SCAR=0.0
Pod documented
53 ▶sub pre_fork_hook { }
Subroutine pre_fork_hook: called CC=1 SCAR=0.0
Pod documented
54 ▶sub post_fork_hook { }
Subroutine post_fork_hook: called CC=1 SCAR=0.0
Pod documented
55  
56 ▶sub run_on_all { }
Subroutine run_on_all: not called CC=1 SCAR=0.0
Pod documented
57 ▶sub run_on_cleanup { }
Subroutine run_on_cleanup: not called CC=1 SCAR=0.0
Pod documented
58 ▶sub run_on_general_message { }
Subroutine run_on_general_message: not called CC=1 SCAR=0.0
Pod documented
59 ▶sub run_on_interval { }
Subroutine run_on_interval: not called CC=1 SCAR=0.0
Pod documented
60 ▶sub run_on_peer_delta { }
Subroutine run_on_peer_delta: not called CC=1 SCAR=0.0
Pod documented
61 ▶sub run_on_pid { }
Subroutine run_on_pid: not called CC=1 SCAR=0.0
Pod documented
62 ▶sub run_on_sig { }
Subroutine run_on_sig: not called CC=1 SCAR=0.0
Pod documented
63 ▶sub run_on_start { }
Subroutine run_on_start: not called CC=1 SCAR=0.0
Pod documented
64 ▶sub run_should_end { }
Subroutine run_should_end: not called CC=1 SCAR=0.0
Pod documented
65  
66 sub run_on_unhandled {
67 0 ▶ my $self = shift;
Subroutine run_on_unhandled: not called CC=2 SCAR=17.9
Pod documented
68 0 my ($activity) = @_;
69  
70 0 my @unhandled = keys %$activity;
71 0 ▶ return unless @unhandled;
Branch: 1 branch
BranchTrueFalseTotal
unless @unhandled 0 0 0
72  
73 0 die "Failed to handle activity: " . join(", " => @unhandled);
74 }
75  
76 #<<< Do not tidy this
77 263 ▶sub _run_on_all { my ($self, @args) = @_; $self->try(sub { $self->run_on_all(@args) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _run_on_all: called CC=1 SCAR=0.0
78 104 ▶sub _run_on_cleanup { my ($self, @args) = @_; $self->try(sub { $self->run_on_cleanup(@args) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _run_on_cleanup: called CC=1 SCAR=0.0
79 123 ▶sub _run_on_interval { my ($self, @args) = @_; $self->try(sub { $self->run_on_interval(@args) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _run_on_interval: called CC=1 SCAR=0.0
80 216 ▶sub _run_on_message { my ($self, @args) = @_; $self->try(sub { $self->run_on_message(@args) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _run_on_message: called CC=1 SCAR=0.0
81 98 ▶sub _run_on_peer_delta { my ($self, @args) = @_; $self->try(sub { $self->run_on_peer_delta(@args) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _run_on_peer_delta: called CC=1 SCAR=0.0
82 4 ▶sub _run_on_pid { my ($self, @args) = @_; $self->try(sub { $self->run_on_pid(@args) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _run_on_pid: called CC=1 SCAR=0.0
83 8 ▶sub _run_on_sig { my ($self, @args) = @_; $self->try(sub { $self->run_on_sig(@args) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _run_on_sig: called CC=1 SCAR=0.0
84 116 ▶sub _run_on_start { my ($self, @args) = @_; $self->try(sub { $self->run_on_start(@args) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _run_on_start: called CC=1 SCAR=0.0
85 0 ▶sub _run_on_unhandled { my ($self, @args) = @_; $self->try(sub { $self->run_on_unhandled(@args) }) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine _run_on_unhandled: not called CC=1 SCAR=6.9
86 263 ▶sub _run_should_end { my ($self, @args) = @_; $self->try(sub { $self->run_should_end(@args) }) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _run_should_end: called CC=1 SCAR=0.0
87 #>>>
88  
89 sub clear_service_fields {
90 280 ▶ my $self = shift;
Subroutine clear_service_fields: called CC=1 SCAR=0.0
Pod documented
91  
92 280 $self->clear_serviceselect_fields;
93 280 $self->clear_servicerequests_fields();
94  
95 280 delete $self->{_CLIENT};
96 280 delete $self->{_LAST_INTERVAL};
97 280 delete $self->{_ORIG_SIG};
98 280 delete $self->{_PEER_CACHE};
99 280 delete $self->{_PEER_STATE};
100 280 delete $self->{_SIGS_SEEN};
101 280 delete $self->{_TERMINATED};
102 280 delete $self->{_WORKERS};
103 }
104  
105 sub register_worker {
106 14 ▶ my $self = shift;
Subroutine register_worker: called CC=1 SCAR=0.0
Pod documented
107 14 my ($name, $pid) = @_;
108  
109 14 $self->{_WORKERS}->{$pid} = $name;
110 }
111  
112 sub workers {
113 8 ▶ my $self = shift;
Subroutine workers: called CC=1 SCAR=0.0
Pod documented
114 8 return $self->{_WORKERS};
115 }
116  
117 sub terminate_workers {
118 279 ▶ my $self = shift;
Subroutine terminate_workers: called CC=5 SCAR=16.3
Pod undocumented
119  
120 279 ▶ my $workers = $self->{_WORKERS} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $workers = $self->{"_WORKERS"} or return 271 8 279
121  
122 8 $self->reap_workers(kill => 'TERM');
123  
124 8 my $start = time;
125 8 ▶ while (keys %$workers) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$workers) 123 8 131
126 123 my $kill;
127 123 ▶ if (time - $start > 15) {
Branch: 1 branch
BranchTrueFalseTotal
if (&IPC::Manager::Role::Service::time() - $start > 15) 0 123 123
128 0 $kill = 'KILL';
129 0 $start = time;
130 }
131  
132 123 $self->reap_workers(kill => $kill);
133 123 ▶ tinysleep(0.05) if keys %$workers;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$workers 119 4 123
134 }
135 }
136  
137 sub reap_workers {
138 131 ▶ my $self = shift;
Subroutine reap_workers: called CC=7 SCAR=19.6
Pod documented
139 131 my %params = @_;
140  
141 131 ▶ my $workers = $self->{_WORKERS} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $workers = $self->{"_WORKERS"} or return 0 131 131
142  
143 131 my %out;
144 131 for my $pid (keys %$workers) {
145 143 local $?;
146 143 my $check = waitpid($pid, WNOHANG);
147 143 my $exit = $?;
148  
149 143 ▶ unless ($check) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($check) 131 12 143
150 131 ▶ kill($params{kill}, $pid) if $params{kill};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"kill"} 8 123 131
151 131 next;
152 }
153  
154 12 my $name = delete $workers->{$pid};
155  
156 12 ▶ if ($check == $pid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($check == $pid) { } 8 4 12
elsif ($check < 0) { } 4 0 4
157 8 $out{$pid} = {name => $name, exit => $exit};
158 }
159 elsif ($check < 0) {
160 4 $out{$pid} = {name => $name, exit => $check};
161 }
162 else {
163 0 die "Nonsensical return from waitpid";
164 }
165 }
166  
167 131 return \%out;
168 }
169  
170 sub reap_children {
171 383 ▶ my $self = shift;
Subroutine reap_children: called CC=5 SCAR=16.1
Pod documented
172 383 local $?;
173  
174 383 my $workers = $self->{_WORKERS};
175 383 my %pids;
176  
177 383 while (1) {
178 389 my $pid = waitpid(-1, WNOHANG);
179 389 ▶ last if $pid == 0 || $pid == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $pid == 0 or $pid == -1 383 6 389
Condition100%3/3
$pid == 0 or $pid == -1
l!l&&r!l&&!r
483356
MC/DC100%2/2
$pid == 0 or $pid == -1
$pid == 0 $pid == -1
Truth table100%3/3
A or B
A= $pid == 0B= $pid == -1
ABresult
1X1
011
000
180  
181 6 my $exit = $?;
182  
183 6 ▶ if ($workers && exists $workers->{$pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($workers and exists $workers->{$pid}) 1 5 6
Condition66%2/3
$workers and exists $workers->{$pid}
!ll&&!rl&&r
501
MC/DC50%1/2
$workers and exists $workers->{$pid}
$workers exists $workers->{$pid}
Truth table66%2/3
A and B
A= $workersB= exists $workers->{$pid}
ABresult
0X0
100
111
184 1 delete $workers->{$pid};
185 1 next;
186 }
187  
188 5 $pids{$pid} = $exit;
189 }
190  
191 383 return \%pids;
192 }
193  
194 sub run_on_message {
195 216 ▶ my $self = shift;
Subroutine run_on_message: called CC=4 SCAR=13.9
Pod undocumented
196 216 my ($msg) = @_;
197  
198 216 my $c = $msg->content;
199  
200 216 ▶ if (ref($c) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq "HASH") 216 0 216
201 216 ▶ return $self->run_on_request_message($msg) if $c->{ipcm_request_id};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"ipcm_request_id"} 124 92 216
202 92 ▶ return $self->run_on_response_message($msg) if $c->{ipcm_response_id};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"ipcm_response_id"} 4 88 92
203 }
204  
205 88 return $self->run_on_general_message($msg);
206 }
207  
208 sub run_on_response_message {
209 4 ▶ my $self = shift;
Subroutine run_on_response_message: called CC=1 SCAR=0.0
Pod undocumented
210 4 my ($msg) = @_;
211  
212 4 my $resp = $msg->content;
213 4 $self->handle_response($resp, $msg);
214  
215 4 return;
216 }
217  
218 sub send_response {
219 126 ▶ my $self = shift;
Subroutine send_response: called CC=2 SCAR=6.9
Pod documented
220 126 my ($peer, $id, $resp, $error) = @_;
221  
222 126 my %payload = (
223 ipcm_response_id => $id,
224 response => $resp,
225 );
226  
227 126 ▶ $payload{ipcm_error} = $error if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 12 114 126
228  
229 # Inside a service the client is non-blocking. send_message
230 # honors send_blocking and dispatches through the outbox; the
231 # event loop drains. No call site needs to know which mode the
232 # client is in.
233 126 $self->client->send_message($peer, \%payload);
234 }
235  
236 sub run_on_request_message {
237 125 ▶ my $self = shift;
Subroutine run_on_request_message: called CC=7 SCAR=19.5
Pod undocumented
238 125 my ($msg) = @_;
239  
240 125 my $peer = $msg->from;
241 125 my $req = $msg->content;
242  
243 # return empty list to not send a response yet.
244 # return one item (can be undef, 0, '', or any other value)
245 125 my (@resp, $err);
246 {
247 125 local $@;
248 125 ▶ eval { @resp = $self->handle_request($req, $msg); 1 } or $err = $@ || "There was an error: $@";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { @resp = $self->handle_request($req, $msg); 1 } } or $err = $@ || "There was an error: $@" 12 113 125
Condition50%1/2
$@ || "There was an error: $@"
l!l
120
MC/DC0%0/1
$@ || "There was an error: $@"
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
249 }
250  
251 125 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 12 113 125
252 12 ▶ my $error_message = $self->expose_error_details
Branch: 1 branch
BranchTrueFalseTotal
$self->expose_error_details ? : 8 4 12
253 ? "$err"
254 : "Internal service error";
255  
256 12 $self->send_response($peer, $req->{ipcm_request_id}, undef, $error_message);
257 12 return;
258 }
259  
260 113 ▶ return unless @resp;
Branch: 1 branch
BranchTrueFalseTotal
unless @resp 12 101 113
261 101 ▶ croak "Incorrect number of responses to request" if @resp != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @resp != 1 0 101 101
262  
263 101 $self->send_response($peer, $req->{ipcm_request_id}, $resp[0]);
264  
265 101 return;
266 }
267  
268 sub client {
269 2114 ▶ return $_[0]->{_CLIENT} if $_[0]->{_CLIENT};
Subroutine client: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"_CLIENT"} 2006 108 2114
270 108 require IPC::Manager;
271 108 return $_[0]->{_CLIENT} = IPC::Manager->connect($_[0]->name, $_[0]->ipcm_info);
272 }
273  
274 sub in_correct_pid {
275 118 ▶ my $self = shift;
Subroutine in_correct_pid: called CC=2 SCAR=6.9
Pod documented
276 118 my $pid = $self->pid;
277 118 ▶ croak "Incorrect PID (did your fork leak? $$ vs $pid)" unless $$ == $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $$ == $pid 1 117 118
278 }
279  
280 sub debug {
281 14 ▶ my $self = shift;
Subroutine debug: called CC=4 SCAR=14.6
Pod documented
282  
283 14 my $io = $self->orig_io;
284 14 ▶ my $fh = $io ? $io->{stderr} // $io->{stdout} // \*STDERR : \*STDERR;
Branch: 1 branch
BranchTrueFalseTotal
$io ? : 13 1 14
Condition40%2/5
($io->{"stderr"} // $io->{"stdout"}) // \*STDERR
l!l
130
$io->{"stderr"} // $io->{"stdout"}
l!l&&r!l&&!r
1300
MC/DC0%0/2
($io->{"stderr"} // $io->{"stdout"}) // \*STDERR
$io->{"stderr"} $io->{"stdout"}
Truth table33%1/3
A // B
A= $io->{"stderr"}B= $io->{"stdout"}
ABresult
1X1
011
000
285  
286 14 print $fh @_;
287 }
288  
289 sub handle {
290 141 ▶ my $self = shift;
Subroutine handle: called CC=2 SCAR=7.0
Pod documented
291 141 my (%params) = @_;
292  
293 141 ▶ croak "'name' is a required parameter" unless $params{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"name"} 0 141 141
294  
295 141 return $self->handle_class->new(
296 %params,
297 service_name => $self->name,
298 ipcm_info => $self->ipcm_info,
299 );
300 }
301  
302 sub peer {
303 12 ▶ my $self = shift;
Subroutine peer: called CC=3 SCAR=11.3
Pod documented
304 12 my ($name, %params) = @_;
305  
306 12 ▶ croak "peer() can only be called on the service in the service process"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pid == $$ 0 12 12
307 unless $self->pid == $$;
308  
309 12 ▶ return $self->{_PEER_CACHE}->{$name} //= $self->peer_class->new(
Condition66%2/3
$self->{"_PEER_CACHE"}{$name} //= $self->peer_class->new(%params, "name", $name, "service", $self, "ipcm_info", $self->ipcm_info)
l!l&&r!l&&!r
480
MC/DC0%0/2
$self->{"_PEER_CACHE"}{$name} //= $self->peer_class->new(%params, "name", $name, "service", $self, "ipcm_info", $self->ipcm_info)
$self->{"_PEER_CACHE"}{$name} $self->peer_class->new(%params, "name", $name, "service", $self, "ipcm_info", $self->ipcm_info)
Truth table66%2/3
A //= B
A= $self->{"_PEER_CACHE"}{$name}B= $self->peer_class->new(%params, "name", $name, "service", $self, "ipcm_info", $self->ipcm_info)
ABresult
1X1
011
000
310 %params,
311  
312 name => $name,
313 service => $self,
314 ipcm_info => $self->ipcm_info,
315 );
316 }
317  
318 sub try {
319 1676 ▶ my $self = shift;
Subroutine try: called CC=3 SCAR=11.0
Pod documented
320 1676 my ($cb) = @_;
321  
322 1676 ▶ unless ($self->intercept_errors) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->intercept_errors) 1508 168 1676
323 1508 my $out = $cb->();
324 1495 return {ok => 1, err => '', out => $out};
325 }
326  
327 168 my $err;
328 {
329 168 local $@;
330 168 my $out;
331 168 ▶ if (eval { $out = $cb->(); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $out = &$cb(); 1 } }) 156 12 168
332 156 return {ok => 1, err => '', out => $out};
333 }
334 12 $err = $@;
335 }
336  
337 12 $self->debug("Peer '" . $self->name . "' caught exception: $err");
338 12 warn $err;
339  
340 12 return {ok => 0, err => $err};
341 }
342  
343 sub peer_delta {
344 484 ▶ my $self = shift;
Subroutine peer_delta: called CC=6 SCAR=17.9
Pod documented
345 484 my (%params) = @_;
346  
347 484 my $prev = $self->{_PEER_STATE};
348 484 my $curr = {map {($_ => 1)} $self->client->peers};
349  
350 484 my $delta = { %$curr };
351 484 ▶ if ($prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev) 379 105 484
352 # Any that exist, and already existed will be set to 0
353 # Any that used to exist, but do not now will be set to -1
354 # Any that did not exist, but do now will be set to 1
355 379 for my $peer (keys %$prev) {
356 422 $delta->{$peer}--;
357 422 ▶ delete $delta->{$peer} unless $delta->{$peer};
Branch: 1 branch
BranchTrueFalseTotal
unless $delta->{$peer} 332 90 422
358 }
359 }
360  
361 484 ▶ $self->{_PEER_STATE} = $curr unless $params{peek};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"peek"} 484 0 484
362  
363 484 ▶ return $delta if keys %$delta;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$delta 200 284 484
364 284 return undef;
365 }
366  
367 sub select_handles {
368 153 ▶ my $self = shift;
Subroutine select_handles: called CC=3 SCAR=11.0
Pod documented
369 153 my $client = $self->client;
370  
371 153 ▶ my @message_handles = $client->have_handles_for_select ? $client->handles_for_select : ();
Branch: 1 branch
BranchTrueFalseTotal
$client->have_handles_for_select ? : 101 52 153
372 153 ▶ my @peer_handles = $client->have_handles_for_peer_change ? $client->handles_for_peer_change : ();
Branch: 1 branch
BranchTrueFalseTotal
$client->have_handles_for_peer_change ? : 0 153 153
373  
374 153 return (@message_handles, @peer_handles);
375 }
376  
377 sub watch {
378 263 ▶ my $self = shift;
Subroutine watch: called CC=25 SCAR=34.3
Pod documented
379  
380 263 ▶ my $sig = $self->{_SIGS_SEEN} // {};
Condition50%1/2
$self->{"_SIGS_SEEN"} // {}
l!l
2630
MC/DC0%0/1
$self->{"_SIGS_SEEN"} // {}
$self->{"_SIGS_SEEN"}
Truth table50%1/2
A
A= $self->{"_SIGS_SEEN"}
Aresult
11
00
381 263 my $cycle = $self->cycle;
382 263 my $client = $self->client;
383 263 my $interval = $self->interval;
384 263 my $have_interval = defined $interval;
385  
386 263 ▶ my $last_interval = $self->{_LAST_INTERVAL} //= time;
Condition66%2/3
$self->{"_LAST_INTERVAL"} //= &IPC::Manager::Role::Service::time()
l!l&&r!l&&!r
1591040
MC/DC0%0/2
$self->{"_LAST_INTERVAL"} //= &IPC::Manager::Role::Service::time()
$self->{"_LAST_INTERVAL"} &IPC::Manager::Role::Service::time()
Truth table66%2/3
A //= B
A= $self->{"_LAST_INTERVAL"}B= &IPC::Manager::Role::Service::time()
ABresult
1X1
011
000
387  
388 263 my $term = $self->terminated;
389  
390 263 while (1) {
391 378 my @messages;
392 my %activity;
393  
394 378 my $reaped = $self->reap_children;
395 378 ▶ $activity{pids} = $reaped if $reaped && keys %$reaped;
Branch: 1 branch
BranchTrueFalseTotal
if $reaped and keys %$reaped 4 374 378
Condition66%2/3
$reaped and keys %$reaped
!ll&&!rl&&r
03744
MC/DC50%1/2
$reaped and keys %$reaped
$reaped keys %$reaped
Truth table66%2/3
A and B
A= $reapedB= keys %$reaped
ABresult
0X0
100
111
396  
397 # Drain any queued outbound messages first; a previous
398 # iteration may have left bytes pending and the kernel may
399 # have made room while we were not looking.
400 378 ▶ $client->drain_pending if $client->have_pending_sends;
Branch: 1 branch
BranchTrueFalseTotal
if $client->have_pending_sends 0 378 378
401  
402 378 my $select_r = $self->select;
403 378 my $select_w = $self->select_write; # undef when no backlog
404  
405 378 ▶ if ($select_r || $select_w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select_r or $select_w) { } 134 244 378
Condition66%2/3
$select_r or $select_w
l!l&&r!l&&!r
1340244
MC/DC50%1/2
$select_r or $select_w
$select_r $select_w
Truth table66%2/3
A or B
A= $select_rB= $select_w
ABresult
1X1
011
000
406 134 require IO::Select;
407 134 my ($r, $w) = IO::Select->select($select_r, $select_w, undef, $cycle);
408  
409 134 ▶ if ($r && @$r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r and @$r) 118 16 134
Condition66%2/3
$r and @$r
!ll&&!rl&&r
160118
MC/DC50%1/2
$r and @$r
$r @$r
Truth table66%2/3
A and B
A= $rB= @$r
ABresult
0X0
100
111
410 118 @messages = $client->get_messages;
411 118 ▶ $client->reset_handles_for_peer_change if $client->have_handles_for_peer_change;
Branch: 1 branch
BranchTrueFalseTotal
if $client->have_handles_for_peer_change 0 118 118
412 }
413  
414 # If any writable handle fired, drain again. This is the
415 # portability path: on platforms where the FIFO buffer
416 # cannot grow past the kernel default, the loop wakes the
417 # moment room appears.
418 134 ▶ $client->drain_pending if $w && @$w;
Branch: 1 branch
BranchTrueFalseTotal
if $w and @$w 0 134 134
Condition66%2/3
$w and @$w
!ll&&!rl&&r
161180
MC/DC0%0/2
$w and @$w
$w @$w
Truth table66%2/3
A and B
A= $wB= @$w
ABresult
0X0
100
111
419 }
420 else {
421 244 @messages = $client->get_messages;
422 }
423  
424 378 my $term2 = $self->terminated;
425 378 ▶ if (defined($term2) && !(defined($term) && $term == $term2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $term2 and not defined $term && $term == $term2) 0 378 378
Condition16%1/6
defined $term2 and not defined $term && $term == $term2
!ll&&!rl&&r
37800
defined $term && $term == $term2
!ll&&!rl&&r
000
MC/DC0%0/3
defined $term2 and not defined $term && $term == $term2
defined $term2 defined $term $term == $term2
Truth table25%1/4
A and not(B && C)
A= defined $term2B= defined $termC= $term == $term2
ABCresult
0XX0
1110
10X1
1101
426 0 $activity{terminated} = {value => $term2};
427 }
428  
429 378 ▶ $activity{messages} = \@messages if @messages;
Branch: 1 branch
BranchTrueFalseTotal
if @messages 201 177 378
430 378 ▶ $activity{sigs} = $sig if keys %$sig;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$sig 8 370 378
431  
432 378 ▶ if (my $delta = $self->peer_delta) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $delta = $self->peer_delta) 98 280 378
433 98 $activity{peer_delta} = $delta;
434 }
435  
436 378 my $now = time;
437 378 ▶ if ($now - $last_interval >= $interval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $last_interval >= $interval) 123 255 378
438 123 $activity{interval} = 1;
439  
440 # This will get reset when we run our callbacks, but put this here as a safety
441 123 $self->{_LAST_INTERVAL} = $now;
442 }
443  
444 378 ▶ if (my $pids = $self->watch_pids) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pids = $self->watch_pids) 351 27 378
445 351 ▶ $activity{pid_watch} = 1 if any { !$self->pid_is_running($_) } @$pids;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { not $self->pid_is_running($_); } , @$pids) 0 351 351
446 }
447  
448 378 ▶ return \%activity if keys %activity;
Branch: 1 branch
BranchTrueFalseTotal
if keys %activity 263 115 378
449  
450 115 ▶ tinysleep($cycle) unless $select_r || $select_w;
Branch: 1 branch
BranchTrueFalseTotal
unless $select_r or $select_w 115 0 115
Condition33%1/3
$select_r or $select_w
l!l&&r!l&&!r
00115
MC/DC0%0/2
$select_r or $select_w
$select_r $select_w
Truth table33%1/3
A or B
A= $select_rB= $select_w
ABresult
1X1
011
000
451 }
452 }
453  
454 sub run {
455 116 ▶ my $self = shift;
Subroutine run: called CC=27 SCAR=34.6
Pod documented
456  
457 116 $self->in_correct_pid;
458  
459 116 my %sig_seen;
460 116 $self->{_ORIG_SIG} = {%SIG};
461 116 $self->{_SIGS_SEEN} = \%sig_seen;
462  
463 116 for my $sig ($self->signals_to_grab) {
464 8 my $key = "$sig";
465 8 ▶ $SIG{$key} = sub { $sig_seen{$key}++ };
Subroutine __ANON__: called CC=1 SCAR=0.0
466 }
467  
468 116 my $start_res = $self->_run_on_start();
469  
470 # Inside the service event loop, sends never block: the loop
471 # drains the outbox each iteration when needed. Clients that do
472 # not consume Role::Outbox treat this as a no-op. Done AFTER
473 # _run_on_start so startup-time sends keep the simpler
474 # synchronous semantics that custom services may depend on.
475 104 $self->client->set_send_blocking(0);
476  
477 # If there was an exception on startup we do not keep going
478 104 ▶ die "Exception in process startup, aborting" unless $start_res->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $start_res->{"ok"} 0 104 104
479  
480 104 $self->peer_delta; # Initialize it
481 104 ▶ until ($self->is_terminated) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_terminated) 263 104 367
482 263 my $activity;
483 263 ▶ $self->try(sub { $activity = $self->watch(\%sig_seen) })->{ok} or next;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
$self->try(sub { $activity = $self->watch(\%sig_seen); } )->{"ok"} or next 0 263 263
484 263 ▶ next unless $activity;
Branch: 1 branch
BranchTrueFalseTotal
unless $activity 0 263 263
485  
486 263 $self->_run_on_all($activity);
487  
488 263 ▶ if (my $sigs = delete $activity->{sigs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sigs = delete $activity->{"sigs"}) 8 255 263
489 8 for my $sig (keys %$sigs) {
490 8 ▶ my $count = delete $sigs->{$sig} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $count = delete $sigs->{$sig} or next 0 8 8
491 8 $self->_run_on_sig($sig) for $count;
492 }
493 }
494  
495 263 ▶ if (my $pids = delete $activity->{pids}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pids = delete $activity->{"pids"}) 4 259 263
496 4 for my $pid (keys %$pids) {
497 4 $self->_run_on_pid($pid, $pids->{$pid});
498 }
499 }
500  
501 263 ▶ if (delete $activity->{pid_watch}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $activity->{"pid_watch"}) 0 263 263
502 0 $self->terminate(0);
503 0 last;
504 }
505  
506 263 ▶ if (delete $activity->{interval}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $activity->{"interval"}) 123 140 263
507 123 $self->_run_on_interval();
508 123 $self->{_LAST_INTERVAL} = time;
509 }
510  
511 263 ▶ if (my $delta = delete $activity->{peer_delta}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $delta = delete $activity->{"peer_delta"}) 98 165 263
512 98 my $client = $self->client;
513 98 for my $peer (keys %$delta) {
514 106 ▶ $client->peer_left($peer) if $delta->{$peer} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delta->{$peer} < 0 90 16 106
515 }
516 98 $self->_run_on_peer_delta($delta);
517 }
518  
519 263 ▶ if (my $msgs = delete $activity->{messages}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msgs = delete $activity->{"messages"}) 201 62 263
520 201 for my $msg (@$msgs) {
521 216 ▶ $self->terminate(0) if $msg->is_terminate;
Branch: 1 branch
BranchTrueFalseTotal
if $msg->is_terminate 80 136 216
522 216 ▶ $self->try(sub { $self->_run_on_message($msg) });
Subroutine __ANON__: called CC=1 SCAR=0.0
523 }
524 }
525  
526 263 ▶ $self->_run_on_unhandled($activity) if keys %$activity;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$activity 0 263 263
527  
528 263 ▶ $self->terminate(0) if $self->_run_should_end()->{out};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_run_should_end->{"out"} 24 239 263
529 }
530  
531 104 $self->_run_on_cleanup();
532  
533 # Drain any queued outbound IPC sends before terminating
534 # workers / returning. Sends queued during the last loop
535 # iteration (e.g. the response to a 'terminate' request) must
536 # reach the peer before this process exits, or the caller will
537 # see 'peer went away while awaiting response'. Loop with a
538 # 5-second deadline so a wedged peer cannot wedge our exit.
539 {
540 104 my $client = $self->client;
541 104 my $deadline = time + 5;
542 104 ▶ while ($client->have_pending_sends && time < $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->have_pending_sends and &IPC::Manager::Role::Service::time() < $deadline) 0 104 104
Condition33%1/3
$client->have_pending_sends and &IPC::Manager::Role::Service::time() < $deadline
!ll&&!rl&&r
10400
MC/DC0%0/2
$client->have_pending_sends and &IPC::Manager::Role::Service::time() < $deadline
$client->have_pending_sends &IPC::Manager::Role::Service::time() < $deadline
Truth table33%1/3
A and B
A= $client->have_pending_sendsB= &IPC::Manager::Role::Service::time() < $deadline
ABresult
0X0
100
111
543 0 ▶ $client->drain_pending or last;
Branch: 1 branch
BranchTrueFalseTotal
$client->drain_pending or last 0 0 0
544 0 ▶ tinysleep(0.01) if $client->have_pending_sends;
Branch: 1 branch
BranchTrueFalseTotal
if $client->have_pending_sends 0 0 0
545 }
546 }
547  
548 104 $self->terminate_workers();
549  
550 104 %SIG = %{$self->{_ORIG_SIG}};
551  
552 104 ▶ return $self->terminated // 0;
Condition50%1/2
$self->terminated // 0
l!l
1040
MC/DC0%0/1
$self->terminated // 0
$self->terminated
Truth table50%1/2
A
A= $self->terminated
Aresult
11
00
553 }
554  
555 198 1;
556  
557 __END__
« Manager/Spawn.pm ↑ Summary Manager/Base/DBI.pm »