ReadWriteFork/SIGCHLD.pm

filter: Statementstmt 97.4% 38 / 39 Branchbran 83.3% 5 / 6 Conditioncond 41.1% 7 / 17 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 90.9% 10 / 11 Podpod 100% 3 / 3 totaltotal 73.5% 64 / 87 CC 12 SCAR 11.8
CC
12
Cov
81%
waitpid
5.5
__ANON__
5.0
is_waiting
2.1
CRAP 3.3
« ReadWriteFork.pm ↑ Summary
1 package Mojo::IOLoop::ReadWriteFork::SIGCHLD;
2 26 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 26 ▶use POSIX ':sys_wait_h';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 ▶use constant WAIT_PID_INTERVAL => $ENV{WAIT_PID_INTERVAL} || 0.05;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'WAIT_PID_INTERVAL'} || 0.05
l!l
026
MC/DC0%0/1
$ENV{'WAIT_PID_INTERVAL'} || 0.05
$ENV{'WAIT_PID_INTERVAL'}
Truth table50%1/2
A
A= $ENV{'WAIT_PID_INTERVAL'}
Aresult
11
00
8  
9 12 ▶has pids => sub { +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 2 ▶sub is_waiting {
Subroutine is_waiting: called CC=2 SCAR=7.6
Pod documented
12 2 my $self = shift;
13 2 ▶ return !!(%{$self->pids} || $self->{tid});
Condition33%1/3
%{$self->pids;} || $self->{'tid'}
l!l&&r!l&&!r
002
MC/DC0%0/2
%{$self->pids;} || $self->{'tid'}
%{$self->pids;} $self->{'tid'}
Truth table33%1/3
A || B
A= %{$self->pids;}B= $self->{'tid'}
ABresult
1X1
011
000
14 }
15  
16 27 ▶sub singleton { state $singleton = Mojo::IOLoop::ReadWriteFork::SIGCHLD->new }
Subroutine singleton: called CC=1 SCAR=0.0
Pod documented
17  
18 36 ▶sub waitpid {
Subroutine waitpid: called CC=5 SCAR=17.1
Pod documented
19 36 my ($self, $pid, $cb) = @_;
20 36 push @{$self->pids->{$pid}}, $cb;
21  
22 # The CHLD test is for code, such as Minion::Command::minion::worker
23 # where SIGCHLD is set up for manual waitpid() checks.
24 # See https://github.com/kraih/minion/issues/15 and
25 # https://github.com/jhthorsen/mojo-ioloop-readwritefork/issues/9 for details.
26 36 my $reactor = Mojo::IOLoop->singleton->reactor;
27 0 ▶ return $self->{ev}{$pid} ||= EV::child($pid, 0, sub { $self->_exit($pid, shift->rstatus) })
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 36 ▶ if !$SIG{CHLD} and $reactor->isa('Mojo::Reactor::EV');
Branch: 1 branch
BranchTrueFalseTotal
if not $SIG{"CHLD"} and $reactor->isa("Mojo::Reactor::EV") 0 36 36
Condition16%1/6
not $SIG{"CHLD"} and $reactor->isa("Mojo::Reactor::EV")
!ll&&!rl&&r
0360
$self->{"ev"}{$pid} ||= EV::child($pid, 0, sub { $self->_exit($pid, (shift())->rstatus); } )
l!l&&r!l&&!r
000
MC/DC0%0/2
not $SIG{"CHLD"} and $reactor->isa("Mojo::Reactor::EV")
not $SIG{"CHLD"} $reactor->isa("Mojo::Reactor::EV")
MC/DC0%0/2
$self->{"ev"}{$pid} ||= EV::child($pid, 0, sub { $self->_exit($pid, (shift())->rstatus); } )
$self->{"ev"}{$pid} EV::child($pid, 0, sub { $self->_exit($pid, (shift())->rstatus); } )
Truth table33%1/3
not(A) and B
A= not $SIG{"CHLD"}B= $reactor->isa("Mojo::Reactor::EV")
ABresult
0X0
100
111
Truth table0%0/3
A ||= B
A= $self->{"ev"}{$pid}B= EV::child($pid, 0, sub { $self->_exit($pid, (shift())->rstatus); } )
ABresult
1X1
011
000
29  
30 36 weaken $self;
31 $self->{tid} ||= Mojo::IOLoop->recurring(
32 WAIT_PID_INTERVAL,
33 98 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
34 98 my $ioloop = shift;
35 98 my $pids = $self->pids;
36 98 ▶ return $ioloop->remove(delete $self->{tid}) unless %$pids;
Branch: 1 branch
BranchTrueFalseTotal
unless %$pids 1 97 98
37  
38 97 for my $pid (keys %$pids) {
39 97 local ($?, $!);
40 97 my $kid = CORE::waitpid($pid, WNOHANG);
41 97 ▶ $self->_exit($pid, $?) if $kid == $pid or $kid == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $kid == $pid or $kid == -1 35 62 97
Condition66%2/3
$kid == $pid or $kid == -1
l!l&&r!l&&!r
35062
MC/DC50%1/2
$kid == $pid or $kid == -1
$kid == $pid $kid == -1
Truth table66%2/3
A or B
A= $kid == $pidB= $kid == -1
ABresult
1X1
011
000
42 }
43 }
44 36 ▶ );
Condition66%2/3
$self->{"tid"} ||= "Mojo::IOLoop"->recurring("0.05", sub { my $ioloop = shift(); my $pids = $self->pids; return $ioloop->remove(delete $self->{"tid"}) unless %$pids; foreach my $pid (keys %$pids) { local($?, $!); my $kid = CORE::waitpid($pid, 1); $self->_exit($pid, $?) if $kid == $pid or $kid == -1; } ; } )
l!l&&r!l&&!r
24120
MC/DC0%0/2
$self->{"tid"} ||= "Mojo::IOLoop"->recurring("0.05", sub { my $ioloop = shift(); my $pids = $self->pids; return $ioloop->remove(delete $self->{"tid"}) unless %$pids; foreach my $pid (keys %$pids) { local($?, $!); my $kid = CORE::waitpid($pid, 1); $self->_exit($pid, $?) if $kid == $pid or $kid == -1; } ; } )
$self->{"tid"} "Mojo::IOLoop"->recurring("0.05", sub { my $ioloop = shift(); my $pids = $self->pids; return $ioloop->remove(delete $self->{"tid"}) unless %$pids; foreach my $pid (keys %$pids) { local($?, $!); my $kid = CORE::waitpid($pid, 1); $self->_exit($pid, $?) if $kid == $pid or $kid == -1; } ; } )
Truth table66%2/3
A ||= B
A= $self->{"tid"}B= "Mojo::IOLoop"->recurring("0.05", sub { my $ioloop = shift(); my $pids = $self->pids; return $ioloop->remove(delete $self->{"tid"}) unless %$pids; foreach my $pid (keys %$pids) { local($?, $!); my $kid = CORE::waitpid($pid, 1); $self->_exit($pid, $?) if $kid == $pid or $kid == -1; } ; } )
ABresult
1X1
011
000
45 }
46  
47 35 ▶sub _exit {
Subroutine _exit: called CC=2 SCAR=6.9
48 35 my ($self, $pid, $status) = @_;
49 35 delete $self->{ev}{$pid};
50 35 my $listeners = delete $self->pids->{$pid};
51 35 for my $cb (@$listeners) { $cb->($status, $pid) }
52 }
53  
54 26 1;
55  
56 =head1 NAME
57  
58 Mojo::IOLoop::ReadWriteFork::SIGCHLD - Non-blocking waitpid for Mojolicious
59  
60 =head1 DESCRIPTION
61  
62 L<Mojo::IOLoop::ReadWriteFork::SIGCHLD> is a module that can wait for a child
63 process to exit. This is currently done either with L<EV/child> or a recurring
64 timer and C<waitpid>.
65  
66 =head1 ATTRIBUTES
67  
68 $hash_ref = $sigchld->pids;
69  
70 Returns a hash ref where the keys are active child process IDs and the values
71 are array-refs of callbacks passed on to L</waitpid>.
72  
73 =head1 METHODS
74  
75 =head2 is_waiting
76  
77 $bool = $sigchld->is_waiting;
78  
79 Returns true if C<$sigchld> is still has a recurring timer or waiting for a
80 process to exit.
81  
82 =head2 singleton
83  
84 $sigchld = Mojo::IOLoop::ReadWriteFork::SIGCHLD->singleton;
85  
86 Returns a shared L<Mojo::IOLoop::ReadWriteFork::SIGCHLD> object.
87  
88 =head2 waitpid
89  
90 $sigchld->waitpid($pid, sub { my ($exit_value) = @_ });
91  
92 Will call the provided callback with C<$?> when the C<$pid> is no longer running.
93  
94 =head1 SEE ALSO
95  
96 L<Mojo::IOLoop::ReadWriteFork>.
97  
98 =cut
« ReadWriteFork.pm ↑ Summary