blib/lib/Proc/Terminator.pm

filter: Statementstmt 79.6% 129 / 162 Branchbran 54.8% 34 / 62 Conditioncond 47.0% 8 / 17 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 81.8% 27 / 33 Podpod 25.0% 1 / 4 totaltotal 69.7% 201 / 288 CC 41 SCAR 25.8
CC
41
Cov
71%
loop_once
48.2
try_kill
17.1
with_pids
7.2
CRAP 13.1
↑ Summary
1 package Proc::Terminator::Ctx;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use POSIX qw(errno_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 my $DEBUG = $ENV{PROC_TERMINATOR_DEBUG};
6  
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 has pid => (
9 is =>'ro',
10 required => 1,
11 3 ▶ isa => sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
12 3 ▶ ($_[0] && $_[0] > 0) or die "PID must be a positive number!"
Branch: 1 branch
BranchTrueFalseTotal
$_[0] and $_[0] > 0 or die "PID must be a positive number!" 3 0 3
Condition33%1/3
$_[0] and $_[0] > 0
!ll&&!rl&&r
003
MC/DC0%0/2
$_[0] and $_[0] > 0
$_[0] $_[0] > 0
Truth table33%1/3
A and B
A= $_[0]B= $_[0] > 0
ABresult
0X0
100
111
13 },
14 1 );
15  
16 has siglist => (
17 is => 'rw',
18 required => 0,
19 3 ▶ isa => sub { ref $_[0] eq 'ARRAY' or die "Siglist must be an array reference" },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "ARRAY" or die "Siglist must be an array reference" 3 0 3
20 0 ▶ default => sub { [] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 1 );
22  
23 has last_sent => (
24 is => 'rw',
25 0 ▶ default => sub { 0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 1 );
27  
28 has error => (
29 is => 'rw',
30 3 ▶ default => sub { "" }
Subroutine __ANON__: called CC=1 SCAR=0.0
31 1 );
32  
33 15 ▶sub try_kill {
Subroutine try_kill: called CC=11 SCAR=28.4
Pod undocumented
34 15 my ($self,$do_kill) = @_;
35
36 15 ▶ if (kill(0, $self->pid) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill(0, $self->pid) == 0) 2 13 15
37 2 my $errno_save = $!;
38 2 ▶ $DEBUG and warn "Kill with signal=0 returned 0 (dead!)";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "Kill with signal=0 returned 0 (dead!)" 0 2 2
39 2 ▶ if ($errno_save != ESRCH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errno_save != 3) 0 2 2
40 0 $self->error($errno_save);
41 0 warn $errno_save;
42 0 return -1;
43 }
44 # else, == ESRCH
45 2 return 1;
46 }
47
48 13 ▶ if (!$do_kill) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($do_kill) 7 6 13
49 7 ▶ $DEBUG and warn "We were not requested to proceed with signal. Returning";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "We were not requested to proceed with signal. Returning" 0 7 7
50 7 return 0;
51 }
52 6 my $sig = shift @{$self->siglist};
53  
54 6 ▶ if (!defined $sig) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sig) 1 5 6
55 0 ▶ $DEBUG and warn "Cannot kill ${\$self->pid} because no signals remain";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "Cannot kill ${\$self->pid;} because no signals remain" 0 1 1
56 1 return -1;
57 }
58 0 ▶ $DEBUG and warn "Using signal $sig for ${\$self->pid}";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "Using signal $sig for ${\$self->pid;}" 0 5 5
59
60 5 ▶ if (kill($sig, $self->pid) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill($sig, $self->pid) == 1) 5 0 5
61 5 return 0;
62 }
63
64 0 ▶ if ($! == ESRCH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 3) { } 0 0 0
65 0 return 1;
66 } else {
67 0 warn $!;
68 0 return -1;
69 }
70 }
71  
72 # This class represents a single 'batch' of PIDs each withe
73 package Proc::Terminator::Batch;
74 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 ▶use POSIX qw(:errno_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
77 1 ▶use Time::HiRes qw(sleep time);
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 has procs => (
81 is => 'rw',
82 3 ▶ isa => sub { ref $_[0] eq 'HASH' or die "Expected hash reference!" },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "HASH" or die "Expected hash reference!" 3 0 3
83 0 ▶ default => sub { { } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 1 );
85  
86 0 ▶has grace_period => ( is => 'rw', default => sub { 0.75 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
87 0 ▶has max_wait => ( is => 'rw', default => sub { 10 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
88 0 ▶has interval => (is => 'rw', default => sub { 0.25 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
89 has badprocs => (is => 'rw',
90 3 ▶ isa => sub { ref $_[0] eq 'ARRAY' or die "Expected arrayref!" },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "ARRAY" or die "Expected arrayref!" 3 0 3
91 3 ▶ default => sub { [ ] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
92 3 ▶has begin_time => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
93  
94 3 ▶sub with_pids {
Subroutine with_pids: called CC=7 SCAR=19.8
Pod undocumented
95 3 my ($cls,$pids,%options) = @_;
96 3 ▶ $pids = ref $pids ? $pids : [ $pids ];
Branch: 1 branch
BranchTrueFalseTotal
ref $pids ? : 0 3 3
97
98 my $siglist = delete $options{siglist} ||
99 3 ▶ [ @Proc::Terminator::DefaultSignalOrder ];
Condition100%2/2
delete $options{'siglist'} || [@Proc::Terminator::DefaultSignalOrder]
l!l
21
MC/DC100%1/1
delete $options{'siglist'} || [@Proc::Terminator::DefaultSignalOrder]
delete $options{'siglist'}
Truth table100%2/2
A
A= delete $options{'siglist'}
Aresult
11
00
100
101 3 my %procs;
102 3 foreach my $pid (@$pids) {
103 3 $procs{$pid} = Proc::Terminator::Ctx->new(
104 pid => $pid,
105 siglist => [ @$siglist ],
106 last_sent => 0);
107 }
108
109 my $self = $cls->new(
110 procs => \%procs,
111 max_wait => delete $options{max_wait} || 10,
112 interval => delete $options{interval} || 0.25,
113 3 ▶ grace_period => delete $options{grace_period} || 0.75,
Condition66%4/6
delete $options{'max_wait'} || 10
l!l
30
delete $options{'interval'} || 0.25
l!l
03
delete $options{'grace_period'} || 0.75
l!l
21
MC/DC0%0/1
delete $options{'max_wait'} || 10
delete $options{'max_wait'}
MC/DC0%0/1
delete $options{'interval'} || 0.25
delete $options{'interval'}
MC/DC100%1/1
delete $options{'grace_period'} || 0.75
delete $options{'grace_period'}
Truth table50%1/2
A
A= delete $options{'max_wait'}
Aresult
11
00
Truth table50%1/2
A
A= delete $options{'interval'}
Aresult
11
00
Truth table100%2/2
A
A= delete $options{'grace_period'}
Aresult
11
00
114 );
115 3 return $self;
116 }
117  
118 15 ▶sub _check_one_proc {
Subroutine _check_one_proc: called CC=5 SCAR=16.1
119 15 my ($self,$ctx,$now) = @_;
120
121 15 my $do_send_kill = $now - $ctx->last_sent > $self->grace_period;
122
123 15 ▶ if ($do_send_kill) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_send_kill) 6 9 15
124 6 $ctx->last_sent($now);
125 0 ▶ $DEBUG and warn("Will send signal to ${\$ctx->pid}");
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "Will send signal to ${\$ctx->pid;}" 0 6 6
126 }
127
128 15 my $ret = $ctx->try_kill($do_send_kill);
129
130 15 ▶ if ($ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) 3 12 15
131 3 delete $self->procs->{$ctx->pid};
132 3 ▶ if ($ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret == -1) 1 2 3
133 1 push @{ $self->badprocs }, $ctx;
134 }
135 }
136
137 15 return $ret;
138 }
139  
140 # The point of abstracting this is so that this module may be integrated
141 # within event loops, where this method is called by a timer, or something.
142 15 ▶sub loop_once {
Subroutine loop_once: called CC=13 SCAR=38.7
Pod undocumented
143 15 my $self = shift;
144 15 my @ctxs = values %{ $self->procs };
145
146 15 ▶ if (!scalar @ctxs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @ctxs) 0 15 15
147 0 ▶ $DEBUG and warn "Nothing left to check..";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "Nothing left to check.." 0 0 0
148 0 ▶ if (@{$self->badprocs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->badprocs;}) 0 0 0
149 0 return undef;
150 }
151 0 return 0; #nothing left to do
152 }
153
154 15 my $now = time();
155
156 15 ▶ if ($self->max_wait &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->max_wait and $now - $self->begin_time > $self->max_wait) 0 15 15
Condition33%1/3
$self->max_wait and $now - $self->begin_time > $self->max_wait
!ll&&!rl&&r
0150
MC/DC0%0/2
$self->max_wait and $now - $self->begin_time > $self->max_wait
$self->max_wait $now - $self->begin_time > $self->max_wait
Truth table33%1/3
A and B
A= $self->max_waitB= $now - $self->begin_time > $self->max_wait
ABresult
0X0
100
111
157 ($now - $self->begin_time > $self->max_wait)) {
158 # do one last sweep?
159 0 ▶ while (my ($pid,$ctx) = each %{$self->procs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->procs;}) 0 0 0
160 0 ▶ if (kill(0, $pid) == 0 && $! == ESRCH) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill(0, $pid) == 0 and $! == 3) { } 0 0 0
Condition0%0/3
kill(0, $pid) == 0 and $! == 3
!ll&&!rl&&r
000
MC/DC0%0/2
kill(0, $pid) == 0 and $! == 3
kill(0, $pid) == 0 $! == 3
Truth table0%0/3
A and B
A= kill(0, $pid) == 0B= $! == 3
ABresult
0X0
100
111
161 0 delete $self->procs->{$pid};
162 } else {
163 0 push @{$self->badprocs}, $ctx;
164 }
165 }
166 0 ▶ if (@{$self->badprocs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->badprocs;}) 0 0 0
167 0 return undef;
168 }
169 0 return 0;
170 }
171 15 $self->_check_one_proc($_, $now) foreach (@ctxs);
172 15 ▶ if (keys %{$self->procs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$self->procs;}) { } 12 3 15
173 12 return scalar keys %{$self->procs};
174 } else {
175 3 ▶ if (@{$self->badprocs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->badprocs;}) 1 2 3
176 1 return undef;
177 }
178 2 return 0;
179 }
180 }
181  
182  
183  
184 package Proc::Terminator;
185 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
187 1 ▶use Time::HiRes qw(time sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
188 1 ▶use POSIX qw(:signal_h :sys_wait_h :errno_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
189 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
190  
191 1 our $VERSION = 0.05;
192  
193 1 our @DefaultSignalOrder = (
194 SIGINT,
195 SIGQUIT,
196 SIGTERM,
197 SIGKILL
198 );
199  
200 1 our @EXPORT = qw(proc_terminate);
201 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
202 # Kill a bunch of processes
203 3 ▶sub proc_terminate {
Subroutine proc_terminate: called CC=4 SCAR=14.2
Pod documented
204 3 my ($pids, %options) = @_;
205
206 3 my $batch = Proc::Terminator::Batch->with_pids($pids, %options);
207
208 3 $batch->begin_time(time());
209 #print Dumper($batch);
210 3 ▶ while ($batch->loop_once) {
Branch: 1 branch
BranchTrueFalseTotal
if ($batch->loop_once) 12 3 15
211 0 ▶ $DEBUG and warn "Sleeping for ${\$batch->interval} seconds";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "Sleeping for ${\$batch->interval;} seconds" 0 12 12
212 12 sleep($batch->interval);
213 }
214
215 3 my @badprocs = map { $_->pid } @{$batch->badprocs};
216
217 3 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 3 3
218 0 return @badprocs;
219 } else {
220 3 return !@badprocs;
221 }
222 }
223  
224 __END__
↑ Summary