Worker/Manager/Prefork.pm

filter: Statementstmt 75.4% 126 / 167 Branchbran 46.1% 12 / 26 Conditioncond 33.3% 3 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 65.2% 30 / 46 Podpod 100% 8 / 8 totaltotal 68.7% 180 / 262 CC 18 SCAR 12.8
CC
18
Cov
70%
__ANON__
12.0
_worker_process_error
12.0
_spawn_workers
10.8
CRAP 3.6
« Worker.pm ↑ Summary Worker/Role/Worker.pm »
1 package Cantella::Worker::Manager::Prefork;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use POE qw( Wheel::Run );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Data::GUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MooseX::Types::Log::Dispatch qw(Logger);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Cantella::Worker::Types qw/WorkerClassName/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.002002';
11 1 $VERSION = eval $VERSION;
12  
13 has logger => (
14 is => 'ro',
15 isa => Logger,
16 coerce => 1,
17 required => 1,
18 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 0 Log::Dispatch->new(outputs => [ ['Null', min_level => 'debug' ] ]);
20 }
21 1 );
22  
23 1 has program_name => (
24 is => 'ro',
25 isa => NonEmptySimpleStr,
26 predicate => 'has_program_name',
27 );
28  
29 1 has worker_class => (
30 is => 'ro',
31 isa => WorkerClassName,
32 required => 1,
33 );
34  
35 has worker_args => (
36 is => 'ro',
37 isa => 'HashRef',
38 required => 1,
39 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 1 );
41  
42 has workers => (
43 is => 'rw',
44 isa => 'Int',
45 required => 1,
46 0 ▶ default => sub { 5 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 1 );
48  
49 1 has max_worker_age => (
50 is => 'rw',
51 isa => 'Int',
52 predicate => 'has_max_worker_age',
53 clearer => 'clear_max_worker_age',
54 );
55  
56 has alias => (
57 is => 'ro',
58 isa => 'Str',
59 required => 1,
60 2 ▶ default => sub{ Data::GUID->new->as_string }
Subroutine __ANON__: called CC=1 SCAR=0.0
61 1 );
62  
63 has close_on_call => (
64 is => 'rw',
65 isa => 'Bool',
66 required => 1,
67 0 ▶ default => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 1 );
69  
70 has worker_detaches => (
71 is => 'rw',
72 isa => 'Bool',
73 required => 1,
74 2 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 1 );
76  
77 has worker_sets_process_group => (
78 is => 'rw',
79 isa => 'Bool',
80 required => 1,
81 2 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
82 1 );
83  
84 has worker_priority => (
85 is => 'rw',
86 isa => 'Int',
87 required => 1,
88 2 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 1 );
90  
91 has pid_to_wheel_map => (
92 is => 'ro',
93 isa => 'HashRef',
94 required => 1,
95 2 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
96 1 );
97  
98 has wid_to_wheel_map => (
99 is => 'ro',
100 isa => 'HashRef',
101 required => 1,
102 2 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
103 1 );
104  
105 has worker_stdout_log_level => (
106 is => 'rw',
107 isa => 'Str',
108 required => 1,
109 0 ▶ default => sub { 'info' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 1 );
111  
112 has worker_stderr_log_level => (
113 is => 'rw',
114 isa => 'Str',
115 required => 1,
116 0 ▶ default => sub { 'info' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 1 );
118  
119  
120 2 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.2
Pod documented
121 2 my ($self, $args) = @_;
122 POE::Session->create(
123 object_states => [
124 $self, {
125 _start => '_start',
126 _pause => '_pause',
127 _resume => '_resume',
128 shutdown => '_shutdown',
129 spawn_workers => '_spawn_workers',
130 retire_worker => '_retire_worker',
131 worker_process_error => '_worker_process_error',
132 worker_process_close => '_worker_process_close',
133 worker_process_stdout => '_worker_process_stdout',
134 worker_process_stderr => '_worker_process_stderr',
135 worker_process_sig_chld => '_worker_process_sig_chld',
136 } ],
137 inline_states => {
138 0 ▶ _keep_alive => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 $_[KERNEL]->delay(_keep_alive => 1000);
140 },
141 2 ▶ sig_term => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
142 2 my ($kernel) = $_[KERNEL];
143 2 $kernel->yield('shutdown');
144 2 $kernel->sig_handled;
145 },
146 0 ▶ sig_int => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 my ($kernel) = $_[KERNEL];
148 0 $kernel->yield('shutdown');
149 0 $kernel->sig_handled;
150 },
151 1 ▶ sig_usr1 => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
152 1 my ($kernel) = $_[KERNEL];
153 1 $kernel->yield('_pause');
154 1 $kernel->sig_handled;
155 },
156 1 ▶ sig_usr2 => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
157 1 my ($kernel) = $_[KERNEL];
158 1 $kernel->yield('_resume');
159 1 $kernel->sig_handled;
160 },
161 }
162 2 );
163 }
164  
165 15 ▶sub current_worker_count{
Subroutine current_worker_count: called CC=1 SCAR=0.0
Pod documented
166 15 my $self = shift;
167 15 scalar keys %{ $self->pid_to_wheel_map };
168 }
169  
170 4 ▶sub worker_wheels {
Subroutine worker_wheels: called CC=1 SCAR=0.0
Pod documented
171 4 my $self = shift;
172 4 return values %{ $self->pid_to_wheel_map }
173 }
174  
175 4 ▶sub signal_workers {
Subroutine signal_workers: called CC=3 SCAR=12.0
Pod documented
176 4 my ($self, $signal) = @_;
177 4 for my $worker ( $self->worker_wheels ){
178 12 ▶ unless ( $worker->kill($signal) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($worker->kill($signal)) 0 12 12
179 0 $self->logger->warning("Failed to signal process ".$worker->PID);
180 }
181 }
182 }
183  
184 2 ▶sub start {
Subroutine start: called CC=2 SCAR=8.4
Pod documented
185 2 my $self = shift;
186 2 ▶ if( $self->has_program_name ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_program_name) 0 2 2
187 0 my $name = join('-', $self->program_name, 'pm');
188 0 $0 = $name;
189 }
190 2 $poe_kernel->run;
191 }
192  
193 0 ▶sub pause {
Subroutine pause: not called CC=1 SCAR=6.9
Pod documented
194 0 my ($self) = @_;
195 0 $poe_kernel->call($self->alias, '_pause');
196 }
197  
198 0 ▶sub resume {
Subroutine resume: not called CC=1 SCAR=6.9
Pod documented
199 0 my ($self) = @_;
200 0 $poe_kernel->call($self->alias, '_resume');
201 }
202  
203 0 ▶sub shutdown {
Subroutine shutdown: not called CC=1 SCAR=6.9
Pod documented
204 0 my ($self) = @_;
205 0 $poe_kernel->post($self->alias, 'shutdown');
206 }
207  
208 #--------#---------#---------#---------#---------#---------#---------#--------#
209  
210 6 ▶sub _spawn_workers {
Subroutine _spawn_workers: called CC=5 SCAR=23.8
211 6 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
212 6 ▶ return unless exists($heap->{spawn_workers}) && $heap->{spawn_workers};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $heap->{"spawn_workers"} and $heap->{"spawn_workers"} 0 6 6
Condition33%1/3
exists $heap->{"spawn_workers"} and $heap->{"spawn_workers"}
!ll&&!rl&&r
006
MC/DC0%0/2
exists $heap->{"spawn_workers"} and $heap->{"spawn_workers"}
exists $heap->{"spawn_workers"} $heap->{"spawn_workers"}
Truth table33%1/3
A and B
A= exists $heap->{"spawn_workers"}B= $heap->{"spawn_workers"}
ABresult
0X0
100
111
213  
214 6 ▶ while ( $self->current_worker_count < $self->workers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_worker_count < $self->workers) 9 6 15
215 9 my $class = $self->worker_class;
216 my $child = POE::Wheel::Run->new(
217 0 ▶ Program => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
218 0 $poe_kernel->stop();
219  
220 0 my $instance = $class->new( @_ );
221 0 ▶ if( $self->has_program_name ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_program_name) 0 0 0
222 0 my $name = $self->program_name;
223 0 $0 = $name;
224 }
225 POE::Session->create(
226 inline_states => {
227 0 ▶ _start => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
228 0 $poe_kernel->select_read(\*STDIN, 'stdin_ready');
229 0 $poe_kernel->sig('TERM', 'stop_polling');
230 0 $poe_kernel->sig('INT', 'stop_polling');
231 },
232 0 ▶ stop_polling => sub { $poe_kernel->select_read(\*STDIN); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
233 0 ▶ stdin_ready => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
234 0 my $buffer;
235 0 my $status = sysread(\*STDIN, $buffer, 1);
236 0 ▶ if( not defined($status) ){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $status) { } 0 0 0
elsif ($status == 0) { } 0 0 0
237 0 $instance->logger->error("Error reading STDIN: $!");
238 } elsif($status == 0){
239 0 $instance->logger->error("Manager process unexpectedly died. Shutting down worker.");
240 0 $poe_kernel->select_read(\*STDIN);
241 0 $poe_kernel->signal($poe_kernel, 'TERM');
242 }
243 },
244 }
245 0 );
246  
247 0 $instance->start;
248 },
249 9 Priority => $self->worker_priority,
250 NoSetSid => !$self->worker_detaches,
251 NoSetPgrp => !$self->worker_sets_process_group,
252 ProgramArgs => [ $self->worker_args ],
253 CloseOnCall => $self->close_on_call,
254 StdoutEvent => 'worker_process_stdout',
255 StderrEvent => 'worker_process_stderr',
256 CloseEvent => 'worker_process_close',
257 );
258 9 $kernel->sig_child($child->PID, 'worker_process_sig_chld');
259 9 $self->wid_to_wheel_map->{ $child->ID } = $child;
260 9 $self->pid_to_wheel_map->{ $child->PID } = $child;
261 9 ▶ if( $self->has_max_worker_age ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_max_worker_age) 6 3 9
262 6 $kernel->delay_set('retire_worker' => $self->max_worker_age, $child->ID);
263 }
264 9 $self->logger->debug("spawning wheel ".$child->ID." as pid: ".$child->PID);
265 }
266 }
267  
268 3 ▶sub _retire_worker {
Subroutine _retire_worker: called CC=2 SCAR=7.0
269 3 my ($self, $wheel_id) = @_[OBJECT, ARG0];
270 3 ▶ if( exists $self->wid_to_wheel_map->{$wheel_id} ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->wid_to_wheel_map->{$wheel_id}) 3 0 3
271 3 $self->logger->debug("retiring wheel ${wheel_id} due to old age");
272 3 $self->wid_to_wheel_map->{$wheel_id}->kill('TERM');
273 }
274 }
275  
276 # Wheel event, including the wheel's ID.
277 9 ▶sub _worker_process_close {
Subroutine _worker_process_close: called CC=2 SCAR=7.0
278 9 my ($self,$wheel_id) = @_[OBJECT, ARG0];
279 9 $self->logger->debug("close for wheel ".$wheel_id);
280 9 ▶ if (defined(my $child = delete $self->wid_to_wheel_map->{$wheel_id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $child = delete $self->wid_to_wheel_map->{$wheel_id})) 9 0 9
281 9 delete $self->pid_to_wheel_map->{$child->PID};
282 }
283 9 return;
284 }
285  
286 9 ▶sub _worker_process_sig_chld {
Subroutine _worker_process_sig_chld: called CC=4 SCAR=14.2
287 9 my ($self, $kernel, $heap, $pid, $status_code) = @_[OBJECT, KERNEL, HEAP, ARG1, ARG2];
288 9 $self->logger->debug("SIGCHLD for pid ${pid} with exit code: ${status_code}");
289 9 ▶ if (defined(my $child = delete $self->pid_to_wheel_map->{$pid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $child = delete $self->pid_to_wheel_map->{$pid})) 0 9 9
290 0 delete $self->wid_to_wheel_map->{$child->ID};
291 }
292 9 ▶ if( exists($heap->{spawn_workers}) && $heap->{spawn_workers} ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $heap->{"spawn_workers"} and $heap->{"spawn_workers"}) 3 6 9
Condition66%2/3
exists $heap->{"spawn_workers"} and $heap->{"spawn_workers"}
!ll&&!rl&&r
603
MC/DC50%1/2
exists $heap->{"spawn_workers"} and $heap->{"spawn_workers"}
exists $heap->{"spawn_workers"} $heap->{"spawn_workers"}
Truth table66%2/3
A and B
A= exists $heap->{"spawn_workers"}B= $heap->{"spawn_workers"}
ABresult
0X0
100
111
293 3 $kernel->yield('spawn_workers');
294 }
295 9 return;
296 }
297  
298 0 ▶sub _worker_process_error {
Subroutine _worker_process_error: not called CC=3 SCAR=24.8
299 0 my ($self, $operation, $errnum, $errstr, $wid) = @_[OBJECT, ARG0, ARG1, ARG2, ARG3];
300 0 ▶ $errstr = "remote end closed" if $operation eq "read" and !$errnum;
Branch: 1 branch
BranchTrueFalseTotal
if $operation eq "read" and not $errnum 0 0 0
Condition0%0/3
$operation eq "read" and not $errnum
!ll&&!rl&&r
000
MC/DC0%0/2
$operation eq "read" and not $errnum
$operation eq "read" not $errnum
Truth table0%0/3
A and not(B)
A= $operation eq "read"B= not $errnum
ABresult
0X0
100
111
301 0 $self->logger->error("wheel $wid generated $operation error $errnum: '$errstr'");
302 }
303  
304 15 ▶sub _worker_process_stderr {
Subroutine _worker_process_stderr: called CC=1 SCAR=0.0
305 15 my($self, $line, $wid) = @_[OBJECT, ARG0, ARG1];
306 15 my $pid = $self->wid_to_wheel_map->{$wid}->PID;
307 15 my $message = "worker $pid STDERR: '${line}'";
308 15 $self->logger->log(level => $self->worker_stderr_log_level, message => $message);
309 }
310  
311 27 ▶sub _worker_process_stdout {
Subroutine _worker_process_stdout: called CC=1 SCAR=0.0
312 27 my($self, $line, $wid) = @_[OBJECT, ARG0, ARG1];
313 27 my $pid = $self->wid_to_wheel_map->{$wid}->PID;
314 27 my $message = "worker $pid STDOUT: '${line}'";
315 27 $self->logger->log(level => $self->worker_stdout_log_level, message => $message);
316 }
317  
318 #--------#---------#---------#---------#---------#---------#---------#--------#
319  
320 2 ▶sub _start {
Subroutine _start: called CC=1 SCAR=0.0
321 2 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
322 2 $self->logger->info("starting worker manager");
323 2 $kernel->alias_set($self->alias);
324 2 $kernel->sig(INT => 'sig_int');
325 2 $kernel->sig(TERM => 'sig_term');
326 2 $kernel->sig(USR1 => 'sig_usr1');
327 2 $kernel->sig(USR2 => 'sig_usr2');
328 2 $heap->{spawn_workers} = 1;
329  
330 2 $kernel->delay(_keep_alive => 1000);
331 2 $kernel->yield('spawn_workers');
332 }
333  
334 1 ▶sub _pause {
Subroutine _pause: called CC=1 SCAR=0.0
335 1 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
336 1 $self->logger->info("pausing worker manager");
337 1 $heap->{spawn_workers} = 0;
338 1 $self->signal_workers('USR1');
339 }
340  
341 1 ▶sub _resume {
Subroutine _resume: called CC=1 SCAR=0.0
342 1 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
343 1 $self->logger->info("resuming worker manager");
344 1 $kernel->yield('spawn_workers');
345 1 $heap->{spawn_workers} = 1;
346 1 $self->signal_workers('USR2');
347 }
348  
349 2 ▶sub _shutdown {
Subroutine _shutdown: called CC=1 SCAR=0.0
350 2 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
351 2 $self->logger->info("shutting down worker manager");
352 2 $self->workers(0);
353 2 $self->signal_workers('TERM');
354 2 $kernel->alarm_remove_all();
355 #cleaup heap, alias, alarms (no lingering refs n ish)
356 2 %$heap = ();
357 2 $kernel->alias_remove($self->alias);
358 }
359  
360 #--------#---------#---------#---------#---------#---------#---------#--------#
361  
362 1 __PACKAGE__->meta->make_immutable;
363  
364 1 1;
365  
366 __END__;
« Worker.pm ↑ Summary Worker/Role/Worker.pm »