blib/lib/IPC/Manager/Role/Service.pm

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