Subprocess/Job.pm

filter: Statementstmt 100% 68 / 68 Branchbran 75.0% 6 / 8 Conditioncond 62.5% 5 / 8 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 92.5% 99 / 107 CC 8 SCAR 8.8
CC
8
Cov
94%
run
6.3
_init_run_instance
2.0
_run_child
2.0
CRAP 2.4
« Subprocess/Running/Delegate.pm ↑ Summary Subprocess/Running/Delegate/Handle.pm »
1 package AnyEvent::Subprocess::Job;
2 48 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 48 $AnyEvent::Subprocess::Job::VERSION = '1.102912';
4 }
5 # ABSTRACT: role representing a runnable job
6  
7 48 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 48 ▶use AnyEvent::Subprocess::Types qw(JobDelegate SubprocessCode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 48 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 48 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 48 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 47 with 'AnyEvent::Subprocess::Role::WithDelegates' => {
16 type => JobDelegate,
17 };
18  
19 47 has 'code' => (
20 is => 'ro',
21 isa => SubprocessCode,
22 required => 1,
23 coerce => 1,
24 );
25  
26 47 has 'on_completion' => (
27 is => 'ro',
28 isa => 'CodeRef',
29 predicate => '_has_on_completion',
30 );
31  
32 has 'run_class' => (
33 is => 'ro',
34 isa => 'ClassName',
35 required => 1,
36 59 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 59 require AnyEvent::Subprocess::Running;
38 59 return 'AnyEvent::Subprocess::Running';
39 },
40 47 );
41  
42 78 ▶sub _init_run_instance {
Subroutine _init_run_instance: called CC=2 SCAR=6.9
43 78 my ($self) = @_;
44 78 ▶ my $run = $self->run_class->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_on_completion ? : 49 29 78
45 delegates => [$self->_build_run_delegates],
46 ($self->_has_on_completion) ? (on_completion => $self->on_completion) : ()
47 );
48 78 return $run;
49 }
50  
51 78 ▶sub _build_run_delegates {
Subroutine _build_run_delegates: called CC=1 SCAR=0.0
52 78 my $self = shift;
53 78 return $self->_invoke_delegates('build_run_delegates');
54 }
55  
56 28 ▶sub _child_setup_hook {
Subroutine _child_setup_hook: called CC=1 SCAR=0.0
57 28 my $self = shift;
58  
59 28 $self->_invoke_delegates('child_setup_hook');
60 28 return;
61 }
62  
63 13 ▶sub _child_finalize_hook {
Subroutine _child_finalize_hook: called CC=1 SCAR=0.0
64 13 my $self = shift;
65 13 $self->_invoke_delegates('child_finalize_hook');
66 }
67  
68 78 ▶sub _parent_setup_hook {
Subroutine _parent_setup_hook: called CC=1 SCAR=0.0
69 78 my $self = shift;
70 78 my $run = shift;
71 78 $self->_invoke_delegates('parent_setup_hook', $run);
72 78 return;
73 }
74  
75 50 ▶sub _parent_finalize_hook {
Subroutine _parent_finalize_hook: called CC=1 SCAR=0.0
76 50 my $self = shift;
77 50 $self->_invoke_delegates('parent_finalize_hook');
78 50 return;
79 }
80  
81 28 ▶sub _build_code_args {
Subroutine _build_code_args: called CC=1 SCAR=0.0
82 28 my $self = shift;
83 28 return $self->_invoke_delegates('build_code_args');
84 }
85  
86 28 ▶sub _run_child {
Subroutine _run_child: called CC=2 SCAR=6.9
87 28 my $self = shift;
88 28 ▶ my $args = shift || {};
Condition100%2/2
shift() || {}
l!l
127
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
89  
90 28 my $exit_code = 0;
91 28 $self->_child_setup_hook;
92 28 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
93 28 my $result = $self->code->({%$args, $self->_build_code_args});
94 12 $self->_invoke_delegates('receive_child_result', $result);
95 1 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
96 1 my $error = $_;
97 1 $self->_invoke_delegates('receive_child_error', $error);
98 1 $exit_code = 255; # backcompat
99 28 };
100 13 $self->_child_finalize_hook;
101 12 exit $exit_code;
102 }
103  
104 78 ▶sub run {
Subroutine run: called CC=6 SCAR=18.3
Pod documented
105 78 my $orig_self = shift;
106 78 my $args_hash = shift;
107  
108 78 ▶ confess "argument to run must be a hashref, not $args_hash"
Branch: 1 branch
BranchTrueFalseTotal
if defined $args_hash and not ref $args_hash && ref $args_hash eq "HASH" 0 78 78
Condition50%3/6
defined $args_hash and not ref $args_hash && ref $args_hash eq "HASH"
!ll&&!rl&&r
7620
ref $args_hash && ref $args_hash eq 'HASH'
!ll&&!rl&&r
002
MC/DC0%0/3
defined $args_hash and not ref $args_hash && ref $args_hash eq "HASH"
defined $args_hash ref $args_hash ref $args_hash eq 'HASH'
Truth table50%2/4
A and not(B && C)
A= defined $args_hashB= ref $args_hashC= ref $args_hash eq 'HASH'
ABCresult
0XX0
1110
10X1
1101
109 if defined $args_hash && !(ref $args_hash && ref $args_hash eq 'HASH');
110  
111 78 my $self = $orig_self->clone;
112  
113 78 my $run = $self->_init_run_instance;
114  
115 78 $self->_parent_setup_hook($run);
116  
117 # an event loop must exist before the fork, in case the child
118 # exits before we create the watcher
119 78 AnyEvent::detect();
120  
121 # TODO: configurable/delegate-able fork
122 78 my $child_pid = fork;
123 78 ▶ confess "fork error: $!" unless defined $child_pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child_pid 0 78 78
124  
125 78 ▶ unless($child_pid){
Branch: 1 branch
BranchTrueFalseTotal
unless ($child_pid) 28 50 78
126 28 $self->_run_child($args_hash);
127 }
128  
129 50 $run->child_pid($child_pid);
130 50 $self->_parent_finalize_hook;
131  
132 50 return $run;
133 }
134  
135 47 1;
136  
137  
138  
139 =pod
140  
141 =head1 NAME
142  
143 AnyEvent::Subprocess::Job - role representing a runnable job
144  
145 =head1 VERSION
146  
147 version 1.102912
148  
149 =head1 ATTRIBUTES
150  
151 =head2 code
152  
153 Coderef to run in the subprocess; or an arrayref or string to pass to C<exec>.
154  
155 =head2 on_completion
156  
157 Coderef to be called when the process exits. Will be passed a
158 L<AnyEvent::Subprocess::Done|AnyEvent::Subprocess::Done> object.
159  
160 =head2 run_class
161  
162 The classname of the "run" class returned by C<run>; defaults to
163 C<AnyEvent::Subprocess::Running>.
164  
165 =head2 run
166  
167 The instance of the run class above; built lazily.
168  
169 Calling run twice does not run the process twice, but I think this
170 might change in the future.
171  
172 =head1 METHODS
173  
174 All the methods in this role are internal, and include:
175  
176 _init_run_instance
177 _build_run_delegates
178 _child_setup_hook
179 _child_finalize_hook
180 _parent_setup_hook
181 _parent_finalize_hook
182 _build_code_args
183 _run_child
184 _build_run
185  
186 If you want to have your own code run at various phases in the
187 process, implement a delegate. See
188 L<AnyEvent::Subprocess::Job::Delegate> for details.
189  
190 =head1 AUTHOR
191  
192 Jonathan Rockway <jrockway@cpan.org>
193  
194 =head1 COPYRIGHT AND LICENSE
195  
196 This software is copyright (c) 2011 by Jonathan Rockway.
197  
198 This is free software; you can redistribute it and/or modify it under
199 the same terms as the Perl 5 programming language system itself.
200  
201 =cut
202  
203  
204 __END__
« Subprocess/Running/Delegate.pm ↑ Summary Subprocess/Running/Delegate/Handle.pm »