blib/lib/IPC/Manager/Spawn.pm

filter: Statementstmt 89.2% 150 / 168 Branchbran 70.5% 48 / 68 Conditioncond 44.4% 16 / 36 MC/DCmcdc 9.5% 2 / 21 Subroutinesub 100% 22 / 22 Podpod 90.0% 9 / 10 totaltotal 76.0% 247 / 325 CC 58 SCAR 24.8
CC
58
Cov
79%
wait
25.8
cleave
18.3
sanity_delta
10.3
CRAP 11.9
« blib/lib/IPC/Manager/Service.pm ↑ Summary blib/lib/IPC/Manager/Role/Service.pm »
1 package IPC::Manager::Spawn;
2 461 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 461 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 461 our $VERSION = '0.000036';
6  
7 461 ▶use POSIX();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 461 ▶use Time::HiRes();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 461 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 461 ▶use IPC::Manager::Serializer::JSON();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 461 ▶use IPC::Manager::Util qw/tinysleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 use overload(
15 fallback => 1,
16  
17 23 ▶ '""' => sub { $_[0]->info },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 461 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 461 use Object::HashBase qw{
21 <protocol
22 <route
23 <serializer
24 <guard
25 <stash
26 <pid
27 <signal
28  
29 do_sanity_check
30 debug
31 461 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 sub init {
34 288 ▶ my $self = shift;
Subroutine init: called CC=6 SCAR=18.2
Pod undocumented
35  
36 288 ▶ $self->{+PID} //= $$;
Condition33%1/3
$self->{"pid"} //= $$
l!l&&r!l&&!r
02880
MC/DC0%0/2
$self->{"pid"} //= $$
$self->{"pid"} $$
Truth table33%1/3
A //= B
A= $self->{"pid"}B= $$
ABresult
1X1
011
000
37 288 ▶ $self->{+GUARD} //= 1;
Condition50%1/2
$self->{"guard"} //= 1
l!l
2880
MC/DC0%0/1
$self->{"guard"} //= 1
$self->{"guard"}
Truth table50%1/2
A
A= $self->{"guard"}
Aresult
11
00
38  
39 288 ▶ croak "'protocol' is a required attribute" unless $self->{+PROTOCOL};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"protocol"} 1 287 288
40 287 ▶ croak "'route' is a required attribute" unless $self->{+ROUTE};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"route"} 1 286 287
41 286 ▶ croak "'serializer' is a required attribute" unless $self->{+SERIALIZER};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"serializer"} 1 285 286
42 }
43  
44 sub cleave {
45 4 ▶ my $self = shift;
Subroutine cleave: called CC=7 SCAR=29.1
Pod documented
46  
47 4 my ($rh, $wh);
48 4 ▶ pipe($rh, $wh) or die "Could not open a pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe $rh, $wh or die "Could not open a pipe: $!" 0 4 4
49  
50 4 ▶ my $pid = fork() // die "Could not fork: $!";
Condition50%1/2
fork // die("Could not fork: $!")
l!l
40
MC/DC0%0/1
fork // die("Could not fork: $!")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
51  
52 4 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 4 0 4
53 4 close($wh);
54 4 chomp(my $out = <$rh>);
55 4 close($rh);
56 4 return $self->{+PID} = $out;
57 }
58  
59 0 close($rh);
60 0 ▶ POSIX::setsid() or die "Can't start a new session: $!";
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setsid() or die "Can't start a new session: $!" 0 0 0
61  
62 0 ▶ $pid = fork() // die "Could not fork: $!";
Condition0%0/2
fork // die("Could not fork: $!")
l!l
00
MC/DC0%0/1
fork // die("Could not fork: $!")
fork
Truth table0%0/2
A
A= fork
Aresult
11
00
63  
64 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
65 0 close($wh);
66 0 POSIX::_exit(0);
67 }
68  
69 0 print $wh "$$\n";
70 0 close($wh);
71  
72 0 $self->{+PID} = $$;
73 0 return 0;
74 }
75  
76 sub info {
77 283 ▶ my $self = shift;
Subroutine info: called CC=1 SCAR=0.0
Pod documented
78 283 return IPC::Manager::Serializer::JSON->serialize([@{$self}{PROTOCOL(), SERIALIZER(), ROUTE()}]);
79 }
80  
81 sub connect {
82 170 ▶ my $self = shift;
Subroutine connect: called CC=1 SCAR=0.0
Pod documented
83 170 my ($id) = @_;
84 170 return $self->{+PROTOCOL}->connect($id, $self->{+SERIALIZER}, $self->{+ROUTE});
85 }
86  
87 sub terminate {
88 157 ▶ my $self = shift;
Subroutine terminate: called CC=8 SCAR=22.5
Pod documented
89 157 my ($con) = @_;
90 157 ▶ $con //= $self->connect('spawn');
Condition33%1/3
$con //= $self->connect("spawn")
l!l&&r!l&&!r
15700
MC/DC0%0/2
$con //= $self->connect("spawn")
$con $self->connect("spawn")
Truth table33%1/3
A //= B
A= $conB= $self->connect("spawn")
ABresult
1X1
011
000
91  
92 # Send the signal before the broadcast so services can act on it
93 # before the terminate message arrives and ends the event loop.
94 157 ▶ if ($self->{+SIGNAL()}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"signal"}) 4 153 157
95 4 for my $peer ($con->peers) {
96 4 my $pid;
97 4 ▶ unless (eval { $pid = $con->peer_pid($peer); 1 }) { warn $@; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $pid = $con->peer_pid($peer); 1 } }) 0 4 4
98 4 ▶ next unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 4 4
99 4 ▶ next if $pid == $$;
Branch: 1 branch
BranchTrueFalseTotal
if $pid == $$ 0 4 4
100 4 ▶ kill($self->{+SIGNAL()}, $pid) if $self->{+SIGNAL()};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"signal"} 4 0 4
101 }
102 }
103  
104 157 $con->broadcast({terminate => 1});
105 }
106  
107 sub _collect_peer_pids {
108 157 ▶ my $self = shift;
Subroutine _collect_peer_pids: called CC=6 SCAR=18.3
109 157 my ($con) = @_;
110  
111 157 my %pids;
112 157 for my $peer ($con->peers) {
113 100 ▶ next if $peer eq $con->id;
Branch: 1 branch
BranchTrueFalseTotal
if $peer eq $con->id 0 100 100
114 100 my $pid;
115 100 ▶ unless (eval { $pid = $con->peer_pid($peer); 1 }) { next }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $pid = $con->peer_pid($peer); 1 } }) 0 100 100
116 100 ▶ $pids{$pid} = 1 if $pid && $pid != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $pid and $pid != $$ 88 12 100
Condition66%2/3
$pid and $pid != $$
!ll&&!rl&&r
01288
MC/DC50%1/2
$pid and $pid != $$
$pid $pid != $$
Truth table66%2/3
A and B
A= $pidB= $pid != $$
ABresult
0X0
100
111
117 }
118  
119 157 return \%pids;
120 }
121  
122 sub wait {
123 157 ▶ my $self = shift;
Subroutine wait: called CC=20 SCAR=32.5
Pod documented
124 157 my ($con, %params) = @_;
125 157 ▶ $con //= $self->connect('spawn');
Condition33%1/3
$con //= $self->connect("spawn")
l!l&&r!l&&!r
15700
MC/DC0%0/2
$con //= $self->connect("spawn")
$con $self->connect("spawn")
Truth table33%1/3
A //= B
A= $conB= $self->connect("spawn")
ABresult
1X1
011
000
126  
127 157 my %child_pids;
128 157 ▶ if (my $pre = $params{child_pids}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pre = $params{"child_pids"}) 157 0 157
129 157 %child_pids = %$pre;
130 }
131  
132 157 for my $client (IPC::Manager::Client->local_clients($self->{+ROUTE})) {
133 201 ▶ eval { $client->disconnect; 1 } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $client->disconnect; 1 } } or warn $@ 4 197 201
134 }
135  
136 # Bound the peer-disappear loop. The poll used to be unbounded with a
137 # 1-second sleep — a wedged service or a slow filesystem on a CPAN
138 # smoker could trap shutdown indefinitely. After the deadline we stop
139 # waiting for clean disconnect and force the reap path to clean up
140 # whatever pids we've seen.
141 my $peer_timeout = $params{peer_timeout}
142 // $ENV{IPC_MANAGER_SPAWN_PEER_TIMEOUT}
143 157 ▶ // 60;
Condition40%2/5
($params{"peer_timeout"} // $ENV{"IPC_MANAGER_SPAWN_PEER_TIMEOUT"}) // 60
l!l
0157
$params{"peer_timeout"} // $ENV{"IPC_MANAGER_SPAWN_PEER_TIMEOUT"}
l!l&&r!l&&!r
00157
MC/DC0%0/2
($params{"peer_timeout"} // $ENV{"IPC_MANAGER_SPAWN_PEER_TIMEOUT"}) // 60
$params{"peer_timeout"} $ENV{"IPC_MANAGER_SPAWN_PEER_TIMEOUT"}
Truth table33%1/3
A // B
A= $params{"peer_timeout"}B= $ENV{"IPC_MANAGER_SPAWN_PEER_TIMEOUT"}
ABresult
1X1
011
000
144 157 my $peer_deadline = Time::HiRes::time() + $peer_timeout;
145 157 my @last_found;
146  
147 157 while (1) {
148 325 my @found;
149 325 for my $peer ($con->peers) {
150 184 ▶ next if $peer eq $con->id;
Branch: 1 branch
BranchTrueFalseTotal
if $peer eq $con->id 0 184 184
151 184 my $pid;
152 184 ▶ unless (eval { $pid = $con->peer_pid($peer); 1 }) { warn $@; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $pid = $con->peer_pid($peer); 1 } }) 0 184 184
153 184 ▶ $child_pids{$pid} = 1 if $pid && $pid != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $pid and $pid != $$ 184 0 184
Condition33%1/3
$pid and $pid != $$
!ll&&!rl&&r
00184
MC/DC0%0/2
$pid and $pid != $$
$pid $pid != $$
Truth table33%1/3
A and B
A= $pidB= $pid != $$
ABresult
0X0
100
111
154 184 push @found => $peer;
155 }
156  
157 325 ▶ last unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 157 168 325
158 168 @last_found = @found;
159  
160 168 ▶ if (Time::HiRes::time() >= $peer_deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Time::HiRes::time() >= $peer_deadline) 0 168 168
161 0 warn "IPC::Manager::Spawn: gave up waiting for clients to disconnect after ${peer_timeout}s: "
162 . join(', ' => sort @found) . "\n";
163 0 last;
164 }
165  
166 168 ▶ print STDERR "Waiting for clients to go away: " . join(', ' => sort @found) . "\n" if $self->{+DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 168 168
167 168 sleep 1;
168 }
169  
170 # Wait for child processes to fully exit before returning, so that
171 # unspawn() does not destroy shared resources while children are
172 # still running cleanup code.
173 157 my $deadline = Time::HiRes::time() + 10;
174 157 ▶ while (keys %child_pids && Time::HiRes::time() < $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %child_pids and &Time::HiRes::time() < $deadline) 112 157 269
Condition66%2/3
keys %child_pids and &Time::HiRes::time() < $deadline
!ll&&!rl&&r
1570112
MC/DC50%1/2
keys %child_pids and &Time::HiRes::time() < $deadline
keys %child_pids &Time::HiRes::time() < $deadline
Truth table66%2/3
A and B
A= keys %child_pidsB= &Time::HiRes::time() < $deadline
ABresult
0X0
100
111
175 112 for my $pid (keys %child_pids) {
176 120 my $ret = waitpid($pid, POSIX::WNOHANG());
177 120 ▶ delete $child_pids{$pid} if $ret != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ret != 0 88 32 120
178 }
179 112 ▶ tinysleep(0.05) if keys %child_pids;
Branch: 1 branch
BranchTrueFalseTotal
if keys %child_pids 32 80 112
180 }
181 }
182  
183 sub sanity_delta {
184 8 ▶ my $self = shift;
Subroutine sanity_delta: called CC=10 SCAR=23.3
Pod documented
185 8 my ($con) = @_;
186 8 ▶ $con //= $self->connect('spawn');
Condition66%2/3
$con //= $self->connect("spawn")
l!l&&r!l&&!r
440
MC/DC0%0/2
$con //= $self->connect("spawn")
$con $self->connect("spawn")
Truth table66%2/3
A //= B
A= $conB= $self->connect("spawn")
ABresult
1X1
011
000
187  
188 8 my $stats = $con->all_stats;
189  
190 8 my $deltas = {};
191 8 for my $peer1 (keys %$stats) {
192 24 my $stat = $stats->{$peer1};
193 24 ▶ my $sent = $stat->{sent} // {};
Condition50%1/2
$stat->{"sent"} // {}
l!l
240
MC/DC0%0/1
$stat->{"sent"} // {}
$stat->{"sent"}
Truth table50%1/2
A
A= $stat->{"sent"}
Aresult
11
00
194 24 ▶ my $read = $stat->{read} // {};
Condition50%1/2
$stat->{"read"} // {}
l!l
240
MC/DC0%0/1
$stat->{"read"} // {}
$stat->{"read"}
Truth table50%1/2
A
A= $stat->{"read"}
Aresult
11
00
195  
196 24 for my $peer2 (keys %$sent) {
197 32 $deltas->{"$peer1 -> $peer2"} += $sent->{$peer2};
198 }
199  
200 24 for my $peer2 (keys %$read) {
201 30 $deltas->{"$peer2 -> $peer1"} -= $read->{$peer2};
202 }
203 }
204  
205 8 ▶ delete $deltas->{$_} for grep { /(:spawn|spawn:)/ || !$deltas->{$_} } keys %$deltas;
Branch: 1 branch
BranchTrueFalseTotal
/(:spawn|spawn:)/ or not $deltas->{$_} 32 0 32
206  
207 8 ▶ return undef unless keys %$deltas;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$deltas 2 6 8
208 6 return $deltas;
209 }
210  
211 sub sanity_check {
212 6 ▶ my $self = shift;
Subroutine sanity_check: called CC=2 SCAR=6.9
Pod documented
213  
214 6 ▶ my $delta = $self->sanity_delta(@_) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $delta = $self->sanity_delta(@_) or return 1 5 6
215  
216 5 die "\nMessages sent vs received mismatch:\n Positive means sent and not recieved.\n negative means recieved more messages than were sent\n" . join("\n" => map { " $delta->{$_} $_" } sort keys %$delta) . "\n\n";
217 }
218  
219 sub DESTROY {
220 283 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=6.9
221 283 ▶ return unless $self->{+GUARD};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"guard"} 19 264 283
222  
223 264 $self->shutdown();
224 }
225  
226 sub unspawn {
227 164 ▶ my $self = shift;
Subroutine unspawn: called CC=1 SCAR=0.0
Pod documented
228  
229 164 $self->{+PROTOCOL}->unspawn($self->{+ROUTE}, delete $self->{+STASH});
230 }
231  
232 sub shutdown {
233 265 ▶ my $self = shift;
Subroutine shutdown: called CC=5 SCAR=16.1
Pod documented
234  
235 265 ▶ return unless $self->{+PID} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pid"} == $$ 108 157 265
236  
237 157 my $con = $self->connect('spawn');
238  
239 # Snapshot peer PIDs before terminate, while all peers are still
240 # registered. After terminate the service may disconnect and exit
241 # before wait() gets a chance to see it.
242 157 my $child_pids = $self->_collect_peer_pids($con);
243  
244 157 $self->terminate($con);
245 157 $self->wait($con, child_pids => $child_pids);
246  
247 157 my $sanity_err;
248 157 ▶ if ($self->do_sanity_check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->do_sanity_check) 4 153 157
249 4 local $@;
250 4 ▶ eval { $self->sanity_check($con); 1 } or $sanity_err = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->sanity_check($con); 1 } } or $sanity_err = $@ 4 0 4
251 }
252  
253 157 $con->disconnect;
254 157 $con = undef;
255  
256 157 $self->unspawn;
257  
258 157 ▶ die $sanity_err if $sanity_err;
Branch: 1 branch
BranchTrueFalseTotal
if $sanity_err 4 153 157
259 }
260  
261 461 1;
262  
263 __END__
« blib/lib/IPC/Manager/Service.pm ↑ Summary blib/lib/IPC/Manager/Role/Service.pm »