blib/lib/AnyEvent/ForkManager.pm

filter: Statementstmt 96.2% 127 / 132 Branchbran 73.6% 28 / 38 Conditioncond 57.1% 8 / 14 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 96.4% 27 / 28 Podpod 30.7% 4 / 13 totaltotal 84.1% 197 / 234 SCAR 33.0
CC
26
Cov
89%
start
7.4
_wait_all_children_with_blocking
5.0
dequeue
4.0
CRAP 27.0
↑ Summary Build »
1 package AnyEvent::ForkManager;
2 15 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 our $VERSION = '0.06';
7  
8 15 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use POSIX qw/WNOHANG/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 use Class::Accessor::Lite 0.04 (
14 15 ro => [
15 qw/max_workers manager_pid/,
16 ],
17 rw => [
18 qw/on_start on_finish on_error on_enqueue on_dequeue on_working_max/,
19 qw/process_queue running_worker process_cb wait_async/,
20 ],
21 15 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 0 ▶sub default_max_workers { 10 }
Subroutine default_max_workers: not called CC=1 SCAR=6.9
Pod undocumented
24  
25 sub new {
26 14 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=12.0
Pod documented
27 14 ▶ my $arg = (@_ == 1) ? +shift : +{ @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 14 14
28 14 ▶ $arg->{max_workers} ||= $class->default_max_workers;
Condition33%1/3
$arg->{"max_workers"} ||= $class->default_max_workers
l!l&&r!l&&!r
1400
MC/DC0%0/2
$arg->{"max_workers"} ||= $class->default_max_workers
$arg->{"max_workers"} $class->default_max_workers
Truth table33%1/3
A ||= B
A= $arg->{"max_workers"}B= $class->default_max_workers
ABresult
1X1
011
000
29  
30 14 bless(+{
31 %$arg,
32 manager_pid => $$,
33 } => $class)->init;
34 }
35  
36 sub init {
37 14 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
38  
39 14 $self->process_queue([]);
40 14 $self->running_worker(+{});
41 14 $self->process_cb(+{});
42  
43 14 return $self;
44 }
45  
46 106 ▶sub is_child { shift->manager_pid != $$ }
Subroutine is_child: called CC=1 SCAR=0.0
Pod undocumented
47 sub is_working_max {
48 142 ▶ my $self = shift;
Subroutine is_working_max: called CC=1 SCAR=0.0
Pod undocumented
49  
50 142 $self->num_workers >= $self->max_workers;
51 }
52  
53 sub num_workers {
54 407 ▶ my $self = shift;
Subroutine num_workers: called CC=1 SCAR=0.0
Pod undocumented
55 407 return scalar keys %{ $self->running_worker };
56 }
57  
58 sub num_queues {
59 36 ▶ my $self = shift;
Subroutine num_queues: called CC=1 SCAR=0.0
Pod undocumented
60 36 return scalar @{ $self->process_queue };
61 }
62  
63 sub start {
64 94 ▶ my $self = shift;
Subroutine start: called CC=7 SCAR=20.0
Pod documented
65 94 ▶ my $arg = (@_ == 1) ? +shift : +{ @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 28 66 94
66  
67 94 ▶ die "\$fork_manager->start() should be called within the manager process\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_child 0 94 94
68 if $self->is_child;
69  
70 94 ▶ if ($self->is_working_max) {## child working max
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_working_max) { } 40 54 94
71 40 $self->_run_cb('on_working_max' => @{ $arg->{args} });
72 40 $self->enqueue($arg);
73 40 return;
74 }
75 else {## create child process
76 54 my $pid = fork;
77  
78 54 ▶ if (not(defined $pid)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 54 54
elsif ($pid) { } 42 12 54
79 0 $self->_run_cb('on_error' => @{ $arg->{args} });
80 0 return;
81 }
82 elsif ($pid) {
83 # parent
84 42 $self->_run_cb('on_start' => $pid, @{ $arg->{args} });
85 42 $self->process_cb->{$pid} = $self->_create_callback(@{ $arg->{args} });
86 $self->running_worker->{$pid} = AnyEvent->child(
87 pid => $pid,
88 42 cb => $self->process_cb->{$pid},
89 );
90  
91 # delete worker watcher if already finished child process.
92 42 ▶ delete $self->running_worker->{$pid} unless exists $self->process_cb->{$pid};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->process_cb->{$pid} 0 42 42
93  
94 42 return $pid;
95 }
96 else {
97 # child
98 12 $arg->{cb}->($self, @{ $arg->{args} });
99 0 $self->finish;
100 }
101 }
102 }
103  
104 sub _create_callback {
105 42 ▶ my($self, @args) = @_;
Subroutine _create_callback: called CC=1 SCAR=0.0
106  
107 42 weaken($self);
108 return sub {
109 32 ▶ my ($pid, $status) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
110 32 delete $self->running_worker->{$pid};
111 32 delete $self->process_cb->{$pid};
112 32 $self->_run_cb('on_finish' => $pid, $status, @args);
113  
114 32 ▶ if ($self->num_queues) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num_queues) 28 4 32
115 ## dequeue
116 28 $self->dequeue;
117 }
118 42 };
119 }
120  
121 sub finish {
122 12 ▶ my ($self, $exit_code) = @_;
Subroutine finish: called CC=3 SCAR=11.7
Pod undocumented
123 12 ▶ die "\$fork_manager->finish() shouln't be called within the manager process\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_child 0 12 12
124 unless $self->is_child;
125  
126 12 ▶ exit($exit_code || 0);
Condition50%1/2
$exit_code || 0
l!l
120
MC/DC0%0/1
$exit_code || 0
$exit_code
Truth table50%1/2
A
A= $exit_code
Aresult
11
00
127 }
128  
129 sub enqueue {
130 40 ▶ my($self, $arg) = @_;
Subroutine enqueue: called CC=1 SCAR=0.0
Pod undocumented
131  
132 40 $self->_run_cb('on_enqueue' => @{ $arg->{args} });
133 40 push @{ $self->process_queue } => $arg;
134 }
135  
136 sub dequeue {
137 28 ▶ my $self = shift;
Subroutine dequeue: called CC=4 SCAR=14.0
Pod undocumented
138  
139 28 ▶ until ($self->is_working_max) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_working_max) 28 20 48
140 28 ▶ last unless @{ $self->process_queue };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->process_queue;} 0 28 28
141  
142 # dequeue
143 28 ▶ if (my $arg = shift @{ $self->process_queue }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift @{$self->process_queue;}) 28 0 28
144 28 $self->_run_cb('on_dequeue' => @{ $arg->{args} });
145 28 $self->start($arg);
146 }
147 }
148 }
149  
150 sub signal_all_children {
151 10 ▶ my ($self, $sig) = @_;
Subroutine signal_all_children: called CC=2 SCAR=6.9
Pod documented
152 10 foreach my $pid (sort keys %{ $self->running_worker }) {
153 20 kill $sig, $pid;
154 }
155 }
156  
157 sub wait_all_children {
158 10 ▶ my $self = shift;
Subroutine wait_all_children: called CC=4 SCAR=13.9
Pod documented
159 10 ▶ my $arg = (@_ == 1) ? +shift : +{ @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 10 10
160  
161 10 my $cb = $arg->{cb};
162 10 ▶ if ($arg->{blocking}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{'blocking'}) { } 5 5 10
163 5 $self->_wait_all_children_with_blocking;
164 1 $self->$cb;
165 }
166 else {
167 5 ▶ die 'cannot call.' if $self->wait_async;
Branch: 1 branch
BranchTrueFalseTotal
if $self->wait_async 0 5 5
168  
169 5 my $super = $self->on_finish;
170  
171 5 weaken($self);
172 $self->on_finish(
173 sub {
174 16 ▶ $super->(@_);
Subroutine __ANON__: called CC=3 SCAR=11.0
175 16 ▶ if ($self->num_workers == 0 and $self->num_queues == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num_workers == 0 and $self->num_queues == 0) 1 15 16
Condition66%2/3
$self->num_workers == 0 and $self->num_queues == 0
!ll&&!rl&&r
1501
MC/DC50%1/2
$self->num_workers == 0 and $self->num_queues == 0
$self->num_workers == 0 $self->num_queues == 0
Truth table66%2/3
A and B
A= $self->num_workers == 0B= $self->num_queues == 0
ABresult
0X0
100
111
176 1 $self->$cb;
177 1 $self->on_finish($super);
178 1 $self->wait_async(0);
179 }
180 }
181 5 );
182  
183 5 $self->wait_async(1);
184 }
185 }
186  
187 sub _run_cb {
188 182 ▶ my $self = shift;
Subroutine _run_cb: called CC=2 SCAR=7.0
189 182 my $name = shift;
190  
191 182 my $cb = $self->$name();
192 182 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 182 0 182
193 182 $self->$cb(@_);
194 }
195 }
196  
197 15 our $WAIT_INTERVAL = 0.1 * 1000 * 1000;
198 sub _wait_all_children_with_blocking {
199 5 ▶ my $self = shift;
Subroutine _wait_all_children_with_blocking: called CC=5 SCAR=16.2
200  
201 5 ▶ until ($self->num_workers == 0 and $self->num_queues == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->num_workers == 0 and $self->num_queues == 0) 64 1 65
Condition66%2/3
$self->num_workers == 0 and $self->num_queues == 0
!ll&&!rl&&r
6401
MC/DC50%1/2
$self->num_workers == 0 and $self->num_queues == 0
$self->num_workers == 0 $self->num_queues == 0
Truth table66%2/3
A and B
A= $self->num_workers == 0B= $self->num_queues == 0
ABresult
0X0
100
111
202 64 my($pid, $status) = _wait_with_status(-1, WNOHANG);
203 64 ▶ if ($pid and exists $self->running_worker->{$pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid and exists $self->running_worker->{$pid}) 16 48 64
Condition66%2/3
$pid and exists $self->running_worker->{$pid}
!ll&&!rl&&r
48016
MC/DC50%1/2
$pid and exists $self->running_worker->{$pid}
$pid exists $self->running_worker->{$pid}
Truth table66%2/3
A and B
A= $pidB= exists $self->running_worker->{$pid}
ABresult
0X0
100
111
204 16 $self->process_cb->{$pid}->($pid, $status);
205 }
206 }
207 continue {
208 # retry interval
209 60 Time::HiRes::usleep( $WAIT_INTERVAL );
210 }
211 }
212  
213 # function
214 sub _wait_with_status {## blocking
215 64 ▶ my($waitpid, $option) = @_;
Subroutine _wait_with_status: called CC=1 SCAR=0.0
216  
217 15 ▶ use vmsish 'status';
Subroutine BEGIN: called CC=1 SCAR=0.0
218 64 local $?;
219  
220 64 my $pid = waitpid($waitpid, $option);
221 64 return ($pid, $?);
222 }
223  
224 15 1;
225 __END__
↑ Summary Build »