blib/lib/POE/Component/ProcTerminator.pm

filter: Statementstmt 76.5% 101 / 132 Branchbran 25.0% 8 / 32 Conditioncond 38.4% 5 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 92.0% 23 / 25 Podpod 100% 1 / 1 totaltotal 65.4% 138 / 211 CC 22 SCAR 29.4
CC
22
Cov
64%
_clean_all
67.9
_do_terminate
10.3
__ANON__
6.0
CRAP 19.0
↑ Summary
1 package POE::Component::ProcTerminator::Batch;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Proc::Terminator;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 extends 'Proc::Terminator::Batch';
8 2 has timer_id => (is => 'rw');
9 has failure_callback => (
10 is => 'ro',
11 0 ▶ isa => sub { ref $_[0] eq 'CODE'
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "CODE" or die "Failure callback should be a CODE reference" 0 0 0
12 or die "Failure callback should be a CODE reference" }
13 2 );
14 2 has cleanup_flags => ( is => 'rw', default => 0 );
15  
16 package POE::Component::ProcTerminator;
17 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use POE::Sugar::Attributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use POE::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Proc::Terminator;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use base qw(POE::Sugar::Attributes Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Class::Struct;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use POSIX qw(:signal_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 our @EXPORT;
29 2 our $DefaultSession = "proc_terminator";
30  
31 2 ▶use Constant::Generate [qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
32 SIGKILL_LAST
33 SIGKILL_ONLY
34 CLEANUP_BLOCK
35 2 )], -type => 'bit', -export => 1, -prefix => 'PROCTERMf_';
36  
37 2 struct
38 'POE::Component::ProcTerminator::SessionInfo' =>
39 [
40 # hash of 'batches', indexed by their timer IDs
41 batches => '*%',
42
43 # default options
44 defaults => '*%'
45
46 ];
47
48 2 our $VERSION = 0.03;
49  
50  
51 1 ▶sub spawn {
Subroutine spawn: called CC=2 SCAR=7.1
Pod documented
52 1 my ($cls,%options) = @_;
53 1 my $alias = delete $options{Alias};
54 1 ▶ $alias ||= $DefaultSession;
Condition33%1/3
$alias ||= $POE::Component::ProcTerminator::DefaultSession
l!l&&r!l&&!r
100
MC/DC0%0/2
$alias ||= $POE::Component::ProcTerminator::DefaultSession
$alias $POE::Component::ProcTerminator::DefaultSession
Truth table33%1/3
A ||= B
A= $aliasB= $POE::Component::ProcTerminator::DefaultSession
ABresult
1X1
011
000
55  
56 1 my $inline_states =
57 POE::Sugar::Attributes->inline_states(__PACKAGE__, $alias);
58
59 1 my $sessinfo = POE::Component::ProcTerminator::SessionInfo->new();
60 1 $sessinfo->defaults(\%options);
61 1 POE::Session->create(
62 inline_states => $inline_states,
63 heap => $sessinfo
64 );
65 }
66  
67 11 ▶sub _do_terminate :Event(terminate)
Subroutine _do_terminate: called CC=7 SCAR=23.3
68 {
69 11 my ($sessinfo,$pids,$options) = @_[HEAP, ARG0..ARG2];
70 11 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
110
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
71 11 $options = { %{ $sessinfo->defaults }, %$options };
72
73 11 ▶ $options->{cleanup_flags} ||= 0;
Condition50%1/2
$options->{"cleanup_flags"} ||= 0
l!l
011
MC/DC0%0/1
$options->{"cleanup_flags"} ||= 0
$options->{"cleanup_flags"}
Truth table50%1/2
A
A= $options->{"cleanup_flags"}
Aresult
11
00
74
75 11 ▶ if (delete $options->{max_wait}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $options->{"max_wait"}) 0 11 11
76 0 warn "max_wait does not make sense in POE::Component::ProcTerminator";
77 }
78 11 my $batch = POE::Component::ProcTerminator::Batch->with_pids(
79 $pids,
80 max_wait => 0,
81 %$options
82 );
83 11 $batch->begin_time(time);
84 11 my $ret = $batch->loop_once;
85 11 ▶ if ($ret) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ret) { } 11 0 11
elsif (not defined $ret and $batch->failure_callback) { } 0 0 0
86 11 my $new_aid =$_[KERNEL]->delay_set(
87 _grace_next => $batch->grace_period,
88 $batch
89 );
90 11 $batch->timer_id($new_aid);
91 ▶ } elsif ((!defined $ret) && $batch->failure_callback) {
Condition0%0/3
not defined $ret and $batch->failure_callback
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $ret and $batch->failure_callback
not defined $ret $batch->failure_callback
Truth table0%0/3
not(A) and B
A= not defined $retB= $batch->failure_callback
ABresult
0X0
100
111
92 0 $batch->failure_callback->($batch);
93 }
94 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 13 ▶sub _grace_next :Event {
Subroutine _grace_next: called CC=4 SCAR=14.2
97 13 my ($sessinfo,$batch) = @_[HEAP,ARG0];
98 13 my $ret = $batch->loop_once();
99 13 ▶ if ($ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) { } 2 11 13
100 2 my $new_aid = $_[KERNEL]->delay_set(
101 $_[STATE],
102 $batch->grace_period,
103 $batch,
104 );
105 2 $batch->timer_id($new_aid);
106
107 } else {
108 11 ▶ if ( (!defined $ret) && $batch->failure_callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ret and $batch->failure_callback) 0 11 11
Condition66%2/3
not defined $ret and $batch->failure_callback
!ll&&!rl&&r
1100
MC/DC0%0/2
not defined $ret and $batch->failure_callback
not defined $ret $batch->failure_callback
Truth table66%2/3
not(A) and B
A= not defined $retB= $batch->failure_callback
ABresult
0X0
100
111
109 0 $batch->failure_callback->($batch);
110 }
111 11 delete $sessinfo->batches->{$batch->timer_id};
112 }
113 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115  
116 0 ▶sub _unknown :Event(_default)
Subroutine _unknown: not called CC=1 SCAR=6.9
117 {
118 0 my ($event,$args) = @_[ARG0,ARG1];
119 0 warn "Unhandled Event '$event'";
120 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122  
123 1 ▶sub _clean_all :Stop {
Subroutine _clean_all: called CC=11 SCAR=42.2
124 1 my $sessinfo = $_[HEAP];
125
126 # iterate through all the batches. Figure out which ones should
127 # be killed immediately
128 1 my $remaining_batch = Proc::Terminator::Batch->with_pids([]);
129 1 my $counter = 0;
130
131 1 ▶ while (my ($aid,$batch) = each %{$sessinfo->batches}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$sessinfo->batches;}) 0 1 1
132 0 ▶ if ($batch->cleanup_flags & PROCTERMf_SIGKILL_ONLY) {
Branch: 2 branches
BranchTrueFalseTotal
if ($batch->cleanup_flags & 2) { } 0 0 0
elsif ($batch->cleanup_flags & 1) { } 0 0 0
133 0 $batch->cleanup_flags(0);
134 0 kill (SIGKILL, $batch->pid);
135 0 $_[KERNEL]->alarm_remove($batch->timer_id);
136 } elsif ($batch->cleanup_flags & PROCTERMf_SIGKILL_LAST) {
137 0 ▶ while (my ($pid,$ctx) = each %{$batch->procs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$batch->procs;}) 0 0 0
138 0 ▶ if ($ctx->siglist->[$#{$ctx->siglist}] != SIGKILL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->siglist->[$#{$ctx->siglist;}] != 9) 0 0 0
139 0 push @{$ctx->siglist}, SIGKILL;
140 }
141 }
142 }
143
144 0 delete $sessinfo->batches->{$aid};
145
146 0 ▶ if (($batch->cleanup_flags & PROCTERMf_CLEANUP_BLOCK) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($batch->cleanup_flags & 4) == 0) { } 0 0 0
147 0 $batch->loop_once();
148 } else {
149 # cleanup..
150 0 @{$remaining_batch->procs}{keys %{$batch->procs}} =
151 0 values %{$batch->procs};
152 }
153 }
154
155 1 ▶ if (%{$remaining_batch->procs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$remaining_batch->procs;}) 0 1 1
156 0 $remaining_batch->max_wait(5); # configurable maybe?
157 0 $remaining_batch->begin_time(time);
158 0 ▶ while ($remaining_batch->loop_once) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining_batch->loop_once) 0 0 0
159 0 sleep(0.25);
160 }
161 }
162
163 # ensure the final signal is indeed sent..
164 1 ▶ while (my ($pid,$ctx) = each %{$remaining_batch->procs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$remaining_batch->procs;}) 0 1 1
165 0 my $sig = pop @{$ctx->siglist};
166 0 ▶ if ($sig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig) 0 0 0
167 0 kill ($sig, $ctx->pid);
168 }
169 }
170 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
171  
172 2 1;
173  
174 __END__
↑ Summary