Zing/Fork.pm

filter: Statementstmt 46.5% 40 / 86 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 62.5% 10 / 16 Podpod 75.0% 3 / 4 totaltotal 43.0% 53 / 123 CC 11 SCAR 23.3
CC
11
Cov
40%
execute
20.0
sanitize
20.0
terminate
20.0
CRAP 10.2
« Zing/Loop.pm ↑ Summary Zing/Daemon.pm »
1 package Zing::Fork;
2  
3 49 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 49 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 49 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 49 ▶use registry 'Zing::Types';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 49 ▶use routines;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 49 ▶use Data::Object::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 49 ▶use Data::Object::ClassHas;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 49 ▶use Data::Object::Space;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 49 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 49 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 49 our $VERSION = '0.11'; # VERSION
20  
21 # ATTRIBUTES
22  
23 49 has 'scheme' => (
24 is => 'ro',
25 isa => 'Scheme',
26 req => 1,
27 );
28  
29 49 has 'parent' => (
30 is => 'rw',
31 isa => 'Process',
32 req => 1,
33 );
34  
35 has 'processes' => (
36 is => 'rw',
37 isa => 'HashRef[Process]',
38 0 ▶ def => sub{{}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 49 );
40  
41 49 has 'space' => (
42 is => 'ro',
43 isa => 'Space',
44 new => 1
45 );
46  
47 0 ▶fun new_space($self) {
Subroutine new_space: not called CC=1 SCAR=6.9
Pod undocumented
48 0 Data::Object::Space->new($self->scheme->[0])
49 49 }
50  
51 # SHIMS
52  
53 sub _waitpid {
54 CORE::waitpid(shift, POSIX::WNOHANG)
55 }
56  
57 # METHODS
58  
59 0 ▶method execute() {
Subroutine execute: not called CC=4 SCAR=30.0
60 0 my $pid;
61 my $process;
62 0 my $sid = $$;
63  
64 0 ▶ if ($Config{d_pseudofork}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Zing::Fork::Config{"d_pseudofork"}) 0 0 0
65 0 Carp::confess "Error on fork: fork emulation not supported";
66 }
67  
68 0 ▶ if(!defined($pid = fork)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($pid = fork)) 0 0 0
69 0 Carp::confess "Error on fork: $!";
70 }
71  
72 # parent
73 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
74 $process = $self->space->load->new(
75 0 @{$self->scheme->[1]},
76 node => Zing::Node->new(pid => $pid),
77 parent => $self->parent,
78 );
79 0 return $self->processes->{$pid} = $process;
80 }
81 # child
82 else {
83 0 $pid = $$;
84 $process = $self->space->reload->new(
85 0 @{$self->scheme->[1]},
86 node => Zing::Node->new(pid => $pid),
87 parent => $self->parent,
88  
89 );
90 0 $process->execute;
91 }
92  
93 0 POSIX::_exit(0);
94 49 }
95  
96 0 ▶method monitor() {
Subroutine monitor: not called CC=2 SCAR=17.9
Pod documented
97 0 my $result = {};
98  
99 0 for my $pid (sort keys %{$self->processes}) {
100 0 $result->{$pid} = _waitpid $pid;
101 }
102  
103 0 return $result;
104 49 }
105  
106 0 ▶method sanitize() {
Subroutine sanitize: not called CC=4 SCAR=30.0
Pod documented
107 0 my $result = $self->monitor;
108  
109 0 for my $pid (sort keys %{$result}) {
110 0 ▶ if ($result->{$pid} == $pid || $result->{$pid} == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{$pid} == $pid or $result->{$pid} == -1) 0 0 0
Condition0%0/3
$result->{$pid} == $pid or $result->{$pid} == -1
l!l&&r!l&&!r
000
MC/DC0%0/2
$result->{$pid} == $pid or $result->{$pid} == -1
$result->{$pid} == $pid $result->{$pid} == -1
Truth table0%0/3
A or B
A= $result->{$pid} == $pidB= $result->{$pid} == -1
ABresult
1X1
011
000
111 0 delete $self->processes->{$pid};
112 }
113 }
114  
115 0 return scalar(keys %{$self->processes});
116 49 }
117  
118 0 ▶method terminate(Str $signal = 'kill') {
Subroutine terminate: not called CC=4 SCAR=30.0
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and $signal = "kill" 0 0 0
Type::Tiny::XS::Str($signal) or Function::Parameters::_croak('In method terminate: parameter 1 ($signal): ' . ${my $v = bless( {"_is_core" => 1,"_overload_coderef" => sub { package Sub::Defer; use warnings; use strict; $undeferred ||= undefer_sub($deferred_info->[4]); goto \&$undeferred;},"_overload_coderef_no_rebuild" => 1,"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Str","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Types::Standard; no warnings; use strict; ref \$_ eq 'SCALAR' or ref \(my $val = $_) eq 'SCALAR';},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 291,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Str","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; use warnings; use strict; $Type::Tiny::AvoidCallbacks ? goto $orig_inlined : "$xsubname($_[1])";},"library" => "Types::Standard","name" => "Str","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00\x00"} use strict; defined $_[0] and not ref $_[0] or _failed_check(8, 'Value', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Value","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Types::Standard; no warnings; use strict; not ref $_;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 276,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Value","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; "defined($_[1]) and not ref($_[1])";},"library" => "Types::Standard","name" => "Value","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00\x00"} use strict; defined $_[0] or _failed_check(7, 'Defined', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Defined","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"complementary_type" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00\x00"} use strict; defined $_[0] and _failed_check(6, 'Undef', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Undef","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"complementary_type" => {},"constraint" => sub { package Types::Standard; no warnings; use strict; not defined $_;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 254,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Undef","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; "!defined($_[1])";},"library" => "Types::Standard","name" => "Undef","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00\x00"} use strict; '???'; $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Item","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Type::Tiny; use warnings; use strict; !0;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 226,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Item","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; '!!1';},"library" => "Types::Standard","name" => "Item","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00\x00"} use strict; '???'; $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => do{my $o},"display_name" => "to_Any","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"complement_name" => "None","constraint" => do{my $o},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 220,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Any","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; '!!1';},"library" => "Types::Standard","name" => "Any","type_default" => sub { package Types::Standard; no warnings; use strict; return undef;},"uniq" => 1}, 'Type::Tiny' ),"uniq" => 2}, 'Type::Tiny' ),"type_default" => sub { package Types::Standard; no warnings; use strict; return undef;},"uniq" => 6}, 'Type::Tiny' ),"constraint" => sub { package Types::Standard; no warnings; use strict; defined $_;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 262,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Defined","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; "defined($_[1])";},"library" => "Types::Standard","name" => "Defined","parent" => {},"uniq" => 7}, 'Type::Tiny' ),"uniq" => 8}, 'Type::Tiny' ),"sorter" => sub { package Types::Standard; no warnings; use strict; $_[0] cmp $_[1];},"type_default" => sub { package Types::Standard; no warnings; use strict; return '';},"uniq" => 9}, 'Type::Tiny' );$v->{"coercion"}{"type_constraint"} = $v;$v->{"compiled_type_constraint"} = $v->{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"};$v->{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"parent"};$v->{"parent"}{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"complementary_type"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"parent"}{"complementary_type"};$v->{"parent"}{"parent"}{"complementary_type"}{"compiled_type_constraint"} = $v->{"parent"}{"parent"}{"complementary_type"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"complementary_type"}{"complementary_type"} = $v->{"parent"}{"parent"};$v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"};$v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"};$v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"constraint"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"constraint"};$v->{"parent"}{"parent"}{"parent"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"}; \$v}->get_message($signal)) 0 0 0
119 0 my $result = {};
120  
121 0 for my $pid (sort keys %{$self->processes}) {
122 0 $result->{$pid} = $self->processes->{$pid}->signal($pid, $signal);
123 }
124  
125 0 return $result;
126 49 }
127  
128 49 1;
129  
130 =encoding utf8
131  
132 =head1 NAME
133  
134 Zing::Fork - Fork Manager
135  
136 =cut
137  
138 =head1 ABSTRACT
139  
140 Scheme Fork Manager
141  
142 =cut
143  
144 =head1 SYNOPSIS
145  
146 use Zing::Fork;
147 use Zing::Process;
148  
149 my $scheme = ['MyApp', [], 1];
150 my $fork = Zing::Fork->new(scheme => $scheme, parent => Zing::Process->new);
151  
152 # $fork->execute;
153  
154 =cut
155  
156 =head1 DESCRIPTION
157  
158 This package provides provides a mechanism for forking and tracking processes,
159 as well as establishing the parent-child relationship. B<Note:> The C<$num>
160 part of the application scheme, i.e. C<['MyApp', [], $num]>, is ignored and
161 launching the desired forks requires calling L</execute> multiple times.
162  
163 =cut
164  
165 =head1 LIBRARIES
166  
167 This package uses type constraints from:
168  
169 L<Zing::Types>
170  
171 =cut
172  
173 =head1 ATTRIBUTES
174  
175 This package has the following attributes:
176  
177 =cut
178  
179 =head2 parent
180  
181 parent(Process)
182  
183 This attribute is read-only, accepts C<(Process)> values, and is required.
184  
185 =cut
186  
187 =head2 processes
188  
189 processes(HashRef[Process])
190  
191 This attribute is read-only, accepts C<(HashRef[Process])> values, and is optional.
192  
193 =cut
194  
195 =head2 scheme
196  
197 scheme(Scheme)
198  
199 This attribute is read-only, accepts C<(Scheme)> values, and is required.
200  
201 =cut
202  
203 =head2 space
204  
205 space(Space)
206  
207 This attribute is read-only, accepts C<(Space)> values, and is optional.
208  
209 =cut
210  
211 =head1 METHODS
212  
213 This package implements the following methods:
214  
215 =cut
216  
217 =head2 execute
218  
219 execute() : Process
220  
221 The execute method forks a process based on the scheme, adds it to the process
222 list and returns a representation of the child process.
223  
224 =over 4
225  
226 =item execute example #1
227  
228 # given: synopsis
229  
230 my $process = $fork->execute;
231  
232 =back
233  
234 =cut
235  
236 =head2 monitor
237  
238 monitor() : HashRef[Int]
239  
240 The monitor method calls L<perlfunc/waitpid> on tracked processes and returns
241 the results as a pid/result map.
242  
243 =over 4
244  
245 =item monitor example #1
246  
247 # given: synopsis
248  
249 $fork->execute;
250 $fork->execute;
251  
252 # forks still alive
253  
254 my $results = $fork->monitor;
255  
256 # { 1000 => 1000, ... }
257  
258 =back
259  
260 =over 4
261  
262 =item monitor example #2
263  
264 # given: synopsis
265  
266 $fork->execute;
267 $fork->execute;
268  
269 # forks are dead
270  
271 my $results = $fork->monitor;
272  
273 # { 1000 => -1, ... }
274  
275 =back
276  
277 =cut
278  
279 =head2 sanitize
280  
281 sanitize() : Int
282  
283 The sanitize method removes inactive child processes from the process list and
284 returns the number of processes remaining.
285  
286 =over 4
287  
288 =item sanitize example #1
289  
290 # given: synopsis
291  
292 $fork->execute; # dead
293 $fork->execute; # dead
294  
295 my $results = $fork->sanitize; # 0
296  
297 =back
298  
299 =over 4
300  
301 =item sanitize example #2
302  
303 # given: synopsis
304  
305 $fork->execute; # live
306 $fork->execute; # dead
307  
308 my $results = $fork->sanitize; # 1
309  
310 =back
311  
312 =over 4
313  
314 =item sanitize example #3
315  
316 # given: synopsis
317  
318 $fork->execute; # live
319 $fork->execute; # live
320  
321 my $results = $fork->sanitize; # 2
322  
323 =back
324  
325 =cut
326  
327 =head2 terminate
328  
329 terminate(Str $signal = 'kill') : HashRef[Int]
330  
331 The terminate method call L<perlfunc/kill> and sends a signal to all tracked
332 processes and returns the results as a pid/result map.
333  
334 =over 4
335  
336 =item terminate example #1
337  
338 # given: synopsis
339  
340 $fork->execute;
341 $fork->execute;
342  
343 my $results = $fork->terminate; # kill
344  
345 =back
346  
347 =over 4
348  
349 =item terminate example #2
350  
351 # given: synopsis
352  
353 $fork->execute;
354 $fork->execute;
355  
356 my $results = $fork->terminate('term');
357  
358 =back
359  
360 =over 4
361  
362 =item terminate example #3
363  
364 # given: synopsis
365  
366 $fork->execute;
367 $fork->execute;
368  
369 my $results = $fork->terminate('usr2');
370  
371 =back
372  
373 =cut
374  
375 =head1 AUTHOR
376  
377 Al Newkirk, C<awncorp@cpan.org>
378  
379 =head1 LICENSE
380  
381 Copyright (C) 2011-2019, Al Newkirk, et al.
382  
383 This is free software; you can redistribute it and/or modify it under the terms
384 of the The Apache License, Version 2.0, as elucidated in the L<"license
385 file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>.
386  
387 =head1 PROJECT
388  
389 L<Wiki|https://github.com/iamalnewkirk/zing/wiki>
390  
391 L<Project|https://github.com/iamalnewkirk/zing>
392  
393 L<Initiatives|https://github.com/iamalnewkirk/zing/projects>
394  
395 L<Milestones|https://github.com/iamalnewkirk/zing/milestones>
396  
397 L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md>
398  
399 L<Issues|https://github.com/iamalnewkirk/zing/issues>
400  
401 =cut
« Zing/Loop.pm ↑ Summary Zing/Daemon.pm »