lib/Gearman/Driver/Job.pm

filter: Statementstmt 98.5% 70 / 71 Branchbran 58.3% 7 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 16 / 16 Podpod 66.6% 2 / 3 totaltotal 91.5% 98 / 107 CC 9 SCAR 6.6
CC
9
Cov
92%
_on_process_stdout
4.1
_on_process_signal
2.1
__ANON__
2.0
CRAP 1.9
« script/gearman_driver_console.pl ↑ Summary lib/Gearman/Driver/Console.pm »
1 package Gearman::Driver::Job;
2  
3 28 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use Gearman::Driver::Adaptor;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use POE qw(Wheel::Run);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Gearman::Driver::Job - Handles the POE magic
10  
11 =head1 DESCRIPTION
12  
13 This class is responsible for starting/stopping processes as well as
14 handling all pipes (STDOUT/STDERR/STDIN) of the processes. All events
15 are written to a logfile. Possible events are:
16  
17 =over 4
18  
19 =item * Starting processes
20  
21 =item * STDOUT of processes
22  
23 =item * STDERR of processes
24  
25 =item * Stopping processes
26  
27 =back
28  
29 The current interface may only be interesting for people subclassing
30 L<Gearman::Driver> or for people writing commands/extensions for
31 L<Gearman::Driver::Console>.
32  
33 =head1 ATTRIBUTES
34  
35 =head2 driver
36  
37 Reference to the L<Gearman::Driver> instance.
38  
39 =cut
40  
41 28 has 'driver' => (
42 handles => { log => 'log' },
43 is => 'rw',
44 isa => 'Gearman::Driver',
45 required => 1,
46 weak_ref => 1,
47 );
48  
49 =head2 name
50  
51 The job's name.
52  
53 =cut
54  
55 28 has 'name' => (
56 is => 'rw',
57 isa => 'Str',
58 required => 1,
59 );
60  
61 =head2 methods
62  
63 ArrayRef of L<Gearman::Driver::Job::Method> objects.
64  
65 =cut
66  
67 28 has 'methods' => (
68 is => 'rw',
69 isa => 'ArrayRef[Gearman::Driver::Job::Method]',
70 required => 1,
71 );
72  
73 =head2 max_processes
74  
75 Maximum number of concurrent processes this job may have.
76  
77 =cut
78  
79 28 has 'max_processes' => (
80 default => 1,
81 is => 'rw',
82 isa => 'Int',
83 required => 1,
84 );
85  
86 =head2 min_processes
87  
88 Minimum number of concurrent processes this job may have.
89  
90 =cut
91  
92 28 has 'min_processes' => (
93 default => 1,
94 is => 'rw',
95 isa => 'Int',
96 required => 1,
97 );
98  
99 =head2 processes
100  
101 This attribute stores a key/value pair containing:
102 C<$pid> => L<$job|Gearman::Driver::Job>
103  
104 It provides following methods:
105  
106 =over 4
107  
108 =item * C<count_processes()>
109  
110 =item * C<delete_process($pid)>
111  
112 =item * C<get_process($pid)>
113  
114 =item * C<get_processes()>
115  
116 =item * C<get_pids()>
117  
118 =item * C<set_process($pid => $job)>
119  
120 =back
121  
122 =cut
123  
124 has 'processes' => (
125 18 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
126 28 handles => {
127 count_processes => 'count',
128 delete_process => 'delete',
129 get_process => 'get',
130 get_processes => 'values',
131 get_pids => 'keys',
132 set_process => 'set',
133 },
134 is => 'ro',
135 isa => 'HashRef',
136 traits => [qw(Hash)],
137 );
138  
139 =head2 gearman
140  
141 Instance of L<Gearman::Driver::Adaptor>.
142  
143 =cut
144  
145 28 has 'gearman' => (
146 is => 'ro',
147 isa => 'Gearman::Driver::Adaptor',
148 );
149  
150 =head2 session
151  
152 Instance of L<POE::Session>.
153  
154 =cut
155  
156 28 has 'session' => (
157 is => 'ro',
158 isa => 'POE::Session',
159 );
160  
161 =head2 lastrun
162  
163 Each time this job is called it stores C<time()> in this attribute.
164  
165 =cut
166  
167 28 has 'lastrun' => (
168 default => 0,
169 is => 'rw',
170 isa => 'Int',
171 );
172  
173 =head2 lasterror
174  
175 Each time this job failed it stores C<time()> in this attribute.
176  
177 =cut
178  
179 28 has 'lasterror' => (
180 default => 0,
181 is => 'rw',
182 isa => 'Int',
183 );
184  
185 =head2 lasterror_msg
186  
187 Each time this job failed it stores the error message in this
188 attribute.
189  
190 =cut
191  
192 28 has 'lasterror_msg' => (
193 default => '',
194 is => 'rw',
195 isa => 'Str',
196 );
197  
198 =head2 worker
199  
200 Reference to the worker object.
201  
202 =cut
203  
204 28 has 'worker' => (
205 is => 'rw',
206 isa => 'Any',
207 required => 1,
208 );
209  
210 =head1 METHODS
211  
212 =head2 add_process
213  
214 Starts/forks/adds another process of this job.
215  
216 =cut
217  
218 25 ▶sub add_process {
Subroutine add_process: called CC=1 SCAR=0.0
Pod documented
219 25 my ($self) = @_;
220 25 POE::Kernel->post( $self->session => 'add_process' );
221 }
222  
223 =head2 remove_process
224  
225 Removes/kills one process of this job.
226  
227 =cut
228  
229 1 ▶sub remove_process {
Subroutine remove_process: called CC=1 SCAR=0.0
Pod documented
230 1 my ($self) = @_;
231 1 POE::Kernel->post( $self->session => 'remove_process' );
232 }
233  
234 18 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
235 18 my ($self) = @_;
236  
237 18 $self->{gearman} = Gearman::Driver::Adaptor->new( server => $self->driver->server );
238  
239 18 foreach my $method ( @{ $self->methods } ) {
240 38 $self->gearman->add_function( $method->name => $method->wrapper );
241 }
242  
243 18 $self->{session} = POE::Session->create(
244 object_states => [
245 $self => {
246 _start => '_start',
247 got_process_stdout => '_on_process_stdout',
248 got_process_stderr => '_on_process_stderr',
249 got_process_close => '_on_process_close',
250 got_process_signal => '_on_process_signal',
251 add_process => '_add_process',
252 remove_process => '_remove_process',
253 }
254 ]
255 );
256 }
257  
258 18 ▶sub _start {
Subroutine _start: called CC=1 SCAR=0.0
259 18 $_[KERNEL]->alias_set( $_[OBJECT]->name );
260 }
261  
262 25 ▶sub _add_process {
Subroutine _add_process: called CC=1 SCAR=0.0
263 25 my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ];
264 my $process = POE::Wheel::Run->new(
265 1 ▶ Program => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
266 1 POE::Kernel->stop();
267  
268 1 ▶ if ( my $process_name = $self->worker->process_name( $0, $self->name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $process_name = $self->worker->process_name($0, $self->name)) 1 0 1
269 1 $0 = $process_name;
270 }
271  
272 1 $self->gearman->work;
273 },
274 25 StdoutEvent => "got_process_stdout",
275 StderrEvent => "got_process_stderr",
276 CloseEvent => "got_process_close",
277 );
278 24 $kernel->sig_child( $process->PID, "got_process_signal" );
279  
280 # Wheel events include the wheel's ID.
281 24 $heap->{wheels}{ $process->ID } = $process;
282  
283 24 $self->log->info( sprintf '(%d) [%s] Process started', $process->PID, $self->name );
284  
285 24 $self->set_process( $process->PID => $process );
286 }
287  
288 1 ▶sub _remove_process {
Subroutine _remove_process: called CC=2 SCAR=7.0
289 1 my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ];
290 1 my ($pid) = ( $self->get_pids )[0];
291 1 ▶ return unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 1 1
292 1 my $process = $self->delete_process($pid);
293 1 $process->kill();
294 1 $self->log->info( sprintf '(%d) [%s] Process killed', $process->PID, $self->name );
295 }
296  
297 50 ▶sub _on_process_stdout {
Subroutine _on_process_stdout: called CC=4 SCAR=14.0
298 50 my ( $self, $heap, $stdout, $wid ) = @_[ OBJECT, HEAP, ARG0, ARG1 ];
299 50 my $process = $heap->{wheels}{$wid};
300 50 my ( $attr, $value ) = $stdout =~ /^(\w+) (.*?)$/;
301 50 ▶ return if !defined $attr || !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr and defined $value 3 47 50
Condition66%2/3
defined $attr and defined $value
!ll&&!rl&&r
3047
MC/DC50%1/2
defined $attr and defined $value
defined $attr defined $value
Truth table66%2/3
A and B
A= defined $attrB= defined $value
ABresult
0X0
100
111
302 47 ▶ $self->$attr($value) if $self->can($attr);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($attr) 47 0 47
303 }
304  
305 3 ▶sub _on_process_stderr {
Subroutine _on_process_stderr: called CC=1 SCAR=0.0
306 3 my ( $self, $heap, $stderr, $wid ) = @_[ OBJECT, HEAP, ARG0, ARG1 ];
307 3 my $process = $heap->{wheels}{$wid};
308 3 $self->log->info( sprintf '(%d) [%s] STDERR: %s', $process->PID, $self->name, $stderr );
309 }
310  
311 6 ▶sub _on_process_close {
Subroutine _on_process_close: called CC=2 SCAR=7.0
312 6 my ( $self, $heap, $wid ) = @_[ OBJECT, HEAP, ARG0 ];
313  
314 6 my $process = delete $heap->{wheels}{$wid};
315  
316 # May have been reaped by got_process_signal
317 6 ▶ return unless defined $process;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $process 0 6 6
318  
319 6 $self->delete_process( $process->PID );
320 }
321  
322 6 ▶sub _on_process_signal {
Subroutine _on_process_signal: called CC=2 SCAR=7.4
323 6 my ( $self, $heap, $pid, $status ) = @_[ OBJECT, HEAP, ARG1 .. ARG2 ];
324  
325 6 my $process = $self->delete_process($pid);
326  
327 6 $self->log->info( sprintf '(%d) [%s] Exited with status %s', $pid, $self->name, $status );
328  
329 # May have been reaped by got_process_close
330 6 ▶ return unless defined $process;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $process 6 0 6
331  
332 0 delete $heap->{wheels}{ $process->ID };
333 }
334  
335 28 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
336  
337 28 __PACKAGE__->meta->make_immutable;
338  
339 =head1 AUTHOR
340  
341 See L<Gearman::Driver>.
342  
343 =head1 COPYRIGHT AND LICENSE
344  
345 See L<Gearman::Driver>.
346  
347 =head1 SEE ALSO
348  
349 =over 4
350  
351 =item * L<Gearman::Driver>
352  
353 =item * L<Gearman::Driver::Adaptor>
354  
355 =item * L<Gearman::Driver::Console>
356  
357 =item * L<Gearman::Driver::Console::Basic>
358  
359 =item * L<Gearman::Driver::Console::Client>
360  
361 =item * L<Gearman::Driver::Job::Method>
362  
363 =item * L<Gearman::Driver::Loader>
364  
365 =item * L<Gearman::Driver::Observer>
366  
367 =item * L<Gearman::Driver::Worker>
368  
369 =back
370  
371 =cut
372  
373 28 1;
« script/gearman_driver_console.pl ↑ Summary lib/Gearman/Driver/Console.pm »