Worker.pm

filter: Statementstmt 32.7% 94 / 287 Branchbran 15.0% 21 / 140 Conditioncond 13.0% 3 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 53.1% 17 / 32 Podpod 100% 12 / 12 totaltotal 28.8% 147 / 509 CC 85 SCAR 62.3
CC
85
Cov
26%
work
1482.0
_get_js_sock
121.8
_register_function
30.0
CRAP 510.2
↑ Summary Util.pm »
1 package Gearman::Worker;
2 11 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 $Gearman::Worker::VERSION = version->declare("2.004.014");
4  
5 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use base "Gearman::Objects";
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Gearman::Worker - Worker for gearman distributed job system
13  
14 =head1 SYNOPSIS
15  
16 use Gearman::Worker;
17 my $worker = Gearman::Worker->new;
18 $worker->job_servers(
19 '127.0.0.1',
20 {
21 host => '10.0.0.1',
22 port => 4730,
23 socket_cb => sub {...},
24 use_ssl => 1,
25 ca_file => ...,
26 cert_file => ...,
27 key_file => ...,
28 }
29 );
30  
31 $worker->register_function($funcname => $subref);
32  
33 $worker->work(
34 stop_if => sub {
35 my ($is_idle, $last_job_time) = @_;
36 # stop idle worker
37 return $is_idle;
38 }
39 );
40  
41  
42 =head1 DESCRIPTION
43  
44 I<Gearman::Worker> is a worker class for the Gearman distributed job system,
45 providing a framework for receiving and serving jobs from a Gearman server.
46  
47 Callers instantiate a I<Gearman::Worker> object, register a list of functions
48 and capabilities that they can handle, then enter an event loop, waiting
49 for the server to send jobs.
50  
51 The worker can send a return value back to the server, which then gets
52 sent back to the client that requested the job; or it can simply execute
53 silently.
54  
55 =head1 USAGE
56  
57 =head2 Gearman::Worker->new(%options)
58  
59 Creates a new I<Gearman::Worker> object, and returns the object.
60  
61 If I<%options> is provided, initializes the new worker object with the
62 settings in I<%options>, which can contain:
63  
64 I<Gearman::Worker> is derived from L<Gearman::Objects>
65  
66 =over 4
67  
68 =item * job_servers
69  
70 List of job servers. Value should be an array reference, hash reference
71 or scalar.
72 It will be ignored if this worker is running as a child
73 process of a gearman server.
74  
75 =item * prefix
76  
77 Calls I<prefix> (see below) to set the prefix / namespace.
78  
79 =item * client_id
80  
81 Unique worker identifier for C<job_servers>.
82  
83 =back
84  
85 =head2 $worker-E<gt>prefix($prefix)
86  
87 Sets the namespace / prefix for the function names. This is useful
88 for sharing job servers between different applications or different
89 instances of the same application (different development sandboxes for
90 example).
91  
92 The namespace is currently implemented as a simple tab separated
93 concatenation of the prefix and the function name.
94  
95 =head1 EXAMPLES
96  
97 =head2 Summation
98  
99 This is an example worker that receives a request to sum up a list of
100 integers.
101  
102 use Gearman::Worker;
103 use Storable qw( thaw );
104 use List::Util qw( sum );
105 my $worker = Gearman::Worker->new;
106 $worker->job_servers('127.0.0.1');
107 $worker->register_function(sum => sub { sum @{ thaw($_[0]->arg) } });
108 $worker->work while 1;
109  
110 See the L<Gearman::Client> documentation for a sample client sending the
111 I<sum> job.
112  
113 =head1 NOTE
114  
115 If you intend using UTF-8 data with SSL based connection,
116 beware there is no UTF-8 support in underlying L<Net::SSLeay>.
117 L<perlunicode/"Forcing-Unicode-in-Perl-(Or-Unforcing-Unicode-in-Perl)"> describes proper workarounds.
118  
119 =head1 METHODS
120  
121 =cut
122  
123 11 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
124 11 ▶use Gearman::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
125 11 ▶use Gearman::Job;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 11 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 use fields (
129 11 'last_connect_fail', # host:port -> unixtime
130 'down_since', # host:port -> unixtime
131 'connecting', # host:port -> unixtime connect started at
132 'can', # ability -> subref (ability is func with optional prefix)
133 'timeouts', # ability -> timeouts
134 'client_id', # random identifier string, no whitespace
135 'parent_pipe', # bool/obj: if we're a child process of a gearman server,
136 # this is socket to our parent process. also means parent
137 # sock can never disconnect or timeout, etc..
138 11 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 sub new {
141 5 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=8 SCAR=27.3
Pod documented
142 5 my $self = $class;
143 5 ▶ $self = fields::new($class) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 5 0 5
144  
145 5 ▶ if ($ENV{GEARMAN_WORKER_USE_STDIO}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"GEARMAN_WORKER_USE_STDIO"}) 0 5 5
146 0 ▶ if ($opts{job_servers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"job_servers"}) 0 0 0
147 0 warn join ' ', __PACKAGE__,
148 'ignores job_servers if $ENV{GEARMAN_WORKER_USE_STDIO} is set';
149  
150 # delete job_servers to insure Gearman::Objects
151 # does not treat correspondent object property
152 0 delete($opts{job_servers});
153 } ## end if ($opts{job_servers})
154 } ## end if ($ENV{GEARMAN_WORKER_USE_STDIO...})
155  
156 5 $self->SUPER::new(%opts);
157  
158 5 $self->{last_connect_fail} = {};
159 5 $self->{down_since} = {};
160 5 $self->{can} = {};
161 5 $self->{timeouts} = {};
162 $self->{client_id} = $opts{client_id}
163 5 ▶ || join('', map { chr(int(rand(26)) + 97) } (1 .. 30));
Condition66%2/3
$opts{'client_id'} || join('', map({chr int(rand 26) + 97;} 1..30))
l!l&&r!l&&!r
140
MC/DC0%0/2
$opts{'client_id'} || join('', map({chr int(rand 26) + 97;} 1..30))
$opts{'client_id'} join('', map({chr int(rand 26) + 97;} 1..30))
Truth table66%2/3
A || B
A= $opts{'client_id'}B= join('', map({chr int(rand 26) + 97;} 1..30))
ABresult
1X1
011
000
164  
165 5 ▶ if ($ENV{GEARMAN_WORKER_USE_STDIO}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"GEARMAN_WORKER_USE_STDIO"}) 0 5 5
166 0 ▶ open my $sock, '+<&', \*STDIN
Branch: 1 branch
BranchTrueFalseTotal
open my $sock, "+<&", \*STDIN or die "Unable to dup STDIN to socket for worker to use." 0 0 0
167 or die "Unable to dup STDIN to socket for worker to use.";
168 0 $self->{job_servers} = [$sock];
169 0 $self->{parent_pipe} = $sock;
170  
171 0 ▶ die "Unable to initialize connection to gearmand"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_set_client_id($sock) 0 0 0
172 unless $self->_set_client_id($sock);
173 } ## end if ($ENV{GEARMAN_WORKER_USE_STDIO...})
174  
175 5 return $self;
176 } ## end sub new
177  
178 =head2 reset_abilities
179  
180 tell all the jobservers that this worker can't do anything
181  
182 B<return> true if C<reset_abilityes> request successfully transmitted to C<job_servers>
183  
184 =cut
185  
186 sub reset_abilities {
187 1 ▶ my $self = shift;
Subroutine reset_abilities: called CC=1 SCAR=0.0
Pod documented
188 1 my $req = _rc("reset_abilities");
189  
190 1 $self->{can} = {};
191 1 $self->{timeouts} = {};
192  
193 1 return $self->_register_all($req);
194 } ## end sub reset_abilities
195  
196 =head2 work(%opts)
197  
198 Endless loop takes a job and wait for the next one.
199 You can pass "stop_if", "on_start", "on_complete" and "on_fail" callbacks in I<%opts>.
200  
201 =cut
202  
203 11 my %job_done;
204  
205 sub work {
206 0 ▶ my ($self, %opts) = @_;
Subroutine work: not called CC=38 SCAR=73.0
Pod documented
207 0 ▶ my $stop_if = delete($opts{stop_if}) || sub {0};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
delete $opts{'stop_if'} || sub { 0; }
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $opts{'stop_if'} || sub { 0; }
delete $opts{'stop_if'} sub { 0; }
Truth table0%0/3
A || B
A= delete $opts{'stop_if'}B= sub { 0; }
ABresult
1X1
011
000
208 0 my $complete_cb = delete $opts{on_complete};
209 0 my $fail_cb = delete $opts{on_fail};
210 0 my $start_cb = delete $opts{on_start};
211 0 ▶ die "Unknown opts" if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
212  
213 0 my $grab_req = _rc("grab_job");
214 0 my $presleep_req = _rc("pre_sleep");
215  
216 0 my $last_job_time;
217  
218 my $on_connect = sub {
219 0 ▶ return _send($_[0], \$presleep_req);
Subroutine __ANON__: not called CC=1 SCAR=6.9
220 0 };
221  
222 0 my %js_map = map { $self->_js_str($_) => $_ } $self->job_servers;
223  
224 # "Active" job servers are servers that have woken us up and should be
225 # queried to see if they have jobs for us to handle. On our first pass
226 # in the loop we contact all servers.
227 0 my %active_js = map { $_ => 1 } keys(%js_map);
228  
229 # ( js => last_update_time, ... )
230 0 my %last_update_time;
231  
232 0 while (1) {
233  
234 # "Jobby" job servers are the set of server which we will contact
235 # on this pass through the loop, because we need to clear and use
236 # the "Active" set to plan for our next pass through the loop.
237 0 my @jobby_js = keys %active_js;
238  
239 0 %active_js = ();
240  
241 0 my $js_count = @jobby_js;
242 0 my $js_offset = int(rand($js_count));
243  
244 0 ▶ for (my $i = 0; $i < $js_count; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $js_count) 0 0 0
245 0 my $js_index = ($i + $js_offset) % $js_count;
246 0 my $js_str = $jobby_js[$js_index];
247 0 my $js = $js_map{$js_str};
248 0 ▶ my $jss = $self->_get_js_sock(
Branch: 1 branch
BranchTrueFalseTotal
my $jss = $self->_get_js_sock($js, "on_connect", $on_connect, "register_on_reconnect", 1) or next 0 0 0
249 $js,
250 on_connect => $on_connect,
251 register_on_reconnect => 1
252 ) or next;
253  
254 # TODO: add an optional sleep in here for the test suite
255 # to test gearmand server going away here. (SIGPIPE on
256 # send_req, etc) this testing has been done manually, at
257 # least.
258 0 ▶ unless (_send($jss, \$grab_req)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_send($jss, \$grab_req)) 0 0 0
259 0 ▶ if ($!{EPIPE} && $self->{parent_pipe}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{"EPIPE"} and $self->{"parent_pipe"}) 0 0 0
Condition0%0/3
$!{"EPIPE"} and $self->{"parent_pipe"}
!ll&&!rl&&r
000
MC/DC0%0/2
$!{"EPIPE"} and $self->{"parent_pipe"}
$!{"EPIPE"} $self->{"parent_pipe"}
Truth table0%0/3
A and B
A= $!{"EPIPE"}B= $self->{"parent_pipe"}
ABresult
0X0
100
111
260  
261 # our parent process died, so let's just quit
262 # gracefully.
263 0 exit(0);
264 } ## end if ($!{EPIPE} && $self...)
265  
266 0 $self->_uncache_sock($js, "grab_job_timeout");
267 0 delete $last_update_time{$js_str};
268 0 next;
269 } ## end unless (_send($jss, \$grab_req...))
270  
271 # if we're a child process talking over a unix pipe, give more
272 # time, since we know there are no network issues, and also
273 # because on failure, we can't "reconnect". all we can do is
274 # die and hope our parent process respawns us.
275 0 ▶ my $timeout = $self->{parent_pipe} ? 5 : 0.50;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parent_pipe'} ? : 0 0 0
276 0 ▶ unless (Gearman::Util::wait_for_readability($jss->fileno, $timeout))
Branch: 1 branch
BranchTrueFalseTotal
unless (Gearman::Util::wait_for_readability($jss->fileno, $timeout)) 0 0 0
277 {
278 0 $self->_uncache_sock($js, "grab_job_timeout");
279 0 delete $last_update_time{$js_str};
280 0 next;
281 } ## end unless (Gearman::Util::wait_for_readability...)
282  
283 0 my $res;
284 do {
285 0 my $err;
286 0 $res = Gearman::Util::read_res_packet($jss, \$err);
287 0 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 0 0
288 0 $self->_uncache_sock($js, "read_res_error");
289 0 delete $last_update_time{$js_str};
290 0 next;
291 }
292 0 ▶ } while ($res->{type} eq "noop");
Branch: 1 branch
BranchTrueFalseTotal
$res->{"type"} eq "noop" and do { do { my $err; $res = Gearman::Util::read_res_packet($jss, \$err); unless ($res) { $self->_uncache_sock($js, "read_res_error"); delete $last_update_time{$js_str}; next; } } } 0 0 0
293  
294 0 ▶ if ($res->{type} eq "no_job") {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"type"} eq "no_job") 0 0 0
295 0 ▶ unless (_send($jss, \$presleep_req)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_send($jss, \$presleep_req)) 0 0 0
296 0 delete $last_update_time{$js_str};
297 0 $self->_uncache_sock($js, "write_presleep_error");
298 }
299 0 $last_update_time{$js_str} = time;
300 0 next;
301 } ## end if ($res->{type} eq "no_job")
302  
303 0 ▶ unless ($res->{type} eq "job_assign") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"type"} eq "job_assign") 0 0 0
304 0 my $msg = "unexpected packet type: $res->{type}";
305  
306 0 ▶ if ($res->{type} eq "error") {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"type"} eq "error") 0 0 0
307 0 $msg .= " [${$res->{blobref}}]\n";
308 0 $msg =~ s/\0/ -- /g;
309 }
310 0 die $msg;
311 } ## end unless ($res->{type} eq "job_assign")
312  
313 0 ▶ ${ $res->{blobref} } =~ s/^(.+?)\0(.+?)\0//
Branch: 1 branch
BranchTrueFalseTotal
${$$res{"blobref"};} =~ s/^(.+?)\0(.+?)\0// or die "regexp on job_assign failed" 0 0 0
314 or die "regexp on job_assign failed";
315 0 my ($handle, $ability) = ($1, $2);
316 my $job = Gearman::Job->new(
317 func => $ability,
318 argref => $res->{blobref},
319 0 handle => $handle,
320 jss => $jss,
321 js => $js
322 );
323  
324 0 my $jobhandle = join("//", $js_str, $job->handle);
325 0 ▶ $start_cb->($jobhandle) if $start_cb;
Branch: 1 branch
BranchTrueFalseTotal
if $start_cb 0 0 0
326  
327 0 my $handler = $self->{can}{$ability};
328 0 my $ret = eval { $handler->($job); };
329 0 my $err = $@;
330 0 ▶ warn "Job '$ability' died: $err" if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
331  
332 0 $last_update_time{$js_str} = $last_job_time = time();
333 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
334 0 my $exception_req
335 = _rc("work_exception",
336 _join0($handle, Storable::nfreeze(\$err)));
337 0 ▶ unless (_send($jss, \$exception_req)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_send($jss, \$exception_req)) 0 0 0
338 0 $self->_uncache_sock($js, "write_res_error");
339 0 next;
340 }
341 } ## end if ($err)
342  
343 0 ▶ if (!defined $job_done{ $job->handle }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $job_done{$job->handle}) 0 0 0
344 0 ▶ if (defined $ret) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret) { } 0 0 0
345 0 $self->send_work_complete($job, $ret);
346 }
347 else {
348 0 $self->send_work_fail($job);
349 }
350 } ## end if (!defined $job_done...)
351  
352 0 my $done = delete $job_done{ $job->handle };
353 0 ▶ if ($done->{command} eq "work_complete") {
Branch: 1 branch
BranchTrueFalseTotal
if ($done->{'command'} eq 'work_complete') { } 0 0 0
354 0 ▶ $complete_cb->($jobhandle, $ret) if $complete_cb;
Branch: 1 branch
BranchTrueFalseTotal
if $complete_cb 0 0 0
355 }
356 else {
357 0 ▶ $fail_cb->($jobhandle, $err) if $fail_cb;
Branch: 1 branch
BranchTrueFalseTotal
if $fail_cb 0 0 0
358 }
359  
360 0 ▶ unless ($done->{result}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done->{"result"}) 0 0 0
361 0 $self->_uncache_sock($js, "write_res_error");
362 0 next;
363 }
364  
365 0 $active_js{$js_str} = 1;
366 } ## end for (my $i = 0; $i < $js_count...)
367  
368 0 my @jss;
369  
370 0 foreach my $js_str (keys(%js_map)) {
371 my $jss = $self->_get_js_sock(
372 0 ▶ $js_map{$js_str},
Branch: 1 branch
BranchTrueFalseTotal
my $jss = $self->_get_js_sock($js_map{$js_str}, "on_connect", $on_connect, "register_on_reconnect", 1) or next 0 0 0
373 on_connect => $on_connect,
374 register_on_reconnect => 1
375 ) or next;
376 0 push @jss, [$js_str, $jss];
377 } ## end foreach my $js_str (keys(%js_map...))
378  
379 0 my $wake_vec = '';
380  
381 0 foreach my $j (@jss) {
382 0 (undef, my $_jss) = @{$j};
383 0 my $fd = $_jss->fileno;
384 0 vec($wake_vec, $fd, 1) = 1;
385 }
386  
387 0 ▶ my $timeout = keys(%active_js) ? 0 : (10 + rand(2));
Branch: 1 branch
BranchTrueFalseTotal
keys %active_js ? : 0 0 0
388  
389 # chill for some arbitrary time until we're woken up again
390 0 my $nready = select(my $wout = $wake_vec, undef, undef, $timeout);
391  
392 0 ▶ if ($nready) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nready) 0 0 0
393 0 foreach my $j (@jss) {
394 0 my ($js_str, $jss) = @{$j};
395 0 my $fd = $jss->fileno;
396 0 ▶ $active_js{$js_str} = 1
Branch: 1 branch
BranchTrueFalseTotal
if vec $wout, $fd, 1 0 0 0
397 if vec($wout, $fd, 1);
398 } ## end foreach my $j (@jss)
399 } ## end if ($nready)
400  
401 0 ▶ my $is_idle = scalar(keys %active_js) > 0 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %active_js > 0 ? : 0 0 0
402  
403 0 ▶ return if $stop_if->($is_idle, $last_job_time);
Branch: 1 branch
BranchTrueFalseTotal
if &$stop_if($is_idle, $last_job_time) 0 0 0
404  
405 0 my $update_since = time - (15 + rand 60);
406  
407 0 ▶ while (my ($js_str, $last_update) = each %last_update_time) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %last_update_time) 0 0 0
408 0 ▶ $active_js{$js_str} = 1 if $last_update < $update_since;
Branch: 1 branch
BranchTrueFalseTotal
if $last_update < $update_since 0 0 0
409 }
410 } ## end while (1)
411  
412 } ## end sub work
413  
414 =head2 $worker->register_function($funcname, $subref)
415  
416 =head2 $worker->register_function($funcname, $timeout, $subref)
417  
418 Registers the function C<$funcname> as being provided by the worker
419 C<$worker>, and advertises these capabilities to all of the job servers
420 defined in this worker.
421  
422 C<$subref> must be a subroutine reference that will be invoked when the
423 worker receives a request for this function. It will be passed a
424 L<Gearman::Job> object representing the job that has been received by the
425 worker.
426  
427 C<$timeout> is an optional parameter specifying how long the jobserver will
428 wait for your subroutine to give an answer. Exceeding this time will result
429 in the jobserver reassigning the task and ignoring your result. This prevents
430 a gimpy worker from ruining the 'user experience' in many situations.
431  
432 B<return> true if C<$funcname> registration successfully transmitted to C<job_servers>
433  
434 =cut
435  
436 sub register_function {
437 2 ▶ my $self = shift;
Subroutine register_function: called CC=7 SCAR=21.1
Pod documented
438 2 my $func = shift;
439 2 ▶ $func || return;
Branch: 1 branch
BranchTrueFalseTotal
$func or return 0 2 2
440  
441 2 my $timeout;
442 2 ▶ if (ref($_[0]) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] ne "CODE") 1 1 2
443 1 $timeout = shift;
444 }
445  
446 2 my $subref = shift;
447 2 my $ability = $self->func($func);
448 2 $self->{can}{$ability} = $subref;
449  
450 2 ▶ if (defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) 1 1 2
451 1 $self->{timeouts}{$ability} = $timeout;
452 }
453  
454 2 my @job_servers = $self->job_servers();
455 2 ▶ @job_servers || return;
Branch: 1 branch
BranchTrueFalseTotal
@job_servers or return 2 0 2
456  
457 0 my $done = 0;
458 0 foreach my $js (@job_servers) {
459 0 ▶ $self->_register_function($ability, $js) && $done++;
Branch: 1 branch
BranchTrueFalseTotal
$self->_register_function($ability, $js) and ++$done 0 0 0
460 }
461  
462 0 return $done == scalar @job_servers;
463 } ## end sub register_function
464  
465 =head2 unregister_function($funcname)
466  
467 send cant_do C<$funcname> request to L<job_servers>
468  
469 B<return> true if CANT_DO C<$funcname> request successfully transmitted to C<job_servers>
470  
471 =cut
472  
473 sub unregister_function {
474 0 ▶ my ($self, $func) = @_;
Subroutine unregister_function: not called CC=1 SCAR=6.9
Pod documented
475 0 my $ability = $self->func($func);
476 0 delete $self->{can}{$ability};
477  
478 0 my $req = _rc("cant_do", $ability);
479 0 return $self->_register_all($req);
480 } ## end sub unregister_function
481  
482 =head2 job_servers(@servers)
483  
484 Override L<Gearman::Objects> method to skip job server initialization if
485 working with L<Gearman::Server>.
486  
487 Calling this method will do nothing in a worker that is running as a child
488 process of a gearman server.
489  
490 =cut
491  
492 sub job_servers {
493 3 ▶ my $self = shift;
Subroutine job_servers: called CC=2 SCAR=7.1
Pod documented
494 3 ▶ $ENV{GEARMAN_WORKER_USE_STDIO} && return $self->{job_servers};
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"GEARMAN_WORKER_USE_STDIO"} and return $self->{"job_servers"} 0 3 3
495  
496 3 return $self->SUPER::job_servers(@_);
497 } ## end sub job_servers
498  
499 =head2 send_work_complete($job, $v)
500  
501 notify the server (and listening clients) that job completed successfully
502  
503 =cut
504  
505 sub send_work_complete {
506 0 ▶ return shift->_finish_job_request("work_complete", @_);
Subroutine send_work_complete: not called CC=1 SCAR=6.9
Pod documented
507 }
508  
509 =head2 send_work_data($job, $data)
510  
511 Use this method to update the client with data from a running job.
512  
513 =cut
514  
515 sub send_work_data {
516 0 ▶ my ($self, $job, $data) = @_;
Subroutine send_work_data: not called CC=2 SCAR=17.9
Pod documented
517 return $self->_job_request("work_data", $job,
518 0 ▶ ref($data) ? ${$data} : $data);
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 0 0 0
519 }
520  
521 =head2 send_work_warning($job, $message)
522  
523 Use this method to send a warning C<$message> to the server (and any listening clients) with regard to the running C<job>.
524  
525 =cut
526  
527 sub send_work_warning {
528 0 ▶ my ($self, $job, $msg) = @_;
Subroutine send_work_warning: not called CC=1 SCAR=6.9
Pod documented
529 0 return $self->_job_request("work_warning", $job, $msg);
530 }
531  
532 =head2 send_work_exception($job, $exception)
533  
534 Use this method to notify the server (and any listening clients) that the C<job> failed with the given C<$exception>.
535  
536 If you are using L<Gearman::Client>, you have to set parameter exceptions properly to get worker exception notifications.
537  
538 =cut
539  
540 sub send_work_exception {
541 0 ▶ my ($self) = shift;
Subroutine send_work_exception: not called CC=1 SCAR=6.9
Pod documented
542 0 return $self->_finish_job_request("work_exception", @_);
543 }
544  
545 =head2 send_work_fail($job)
546  
547 Use this method to notify the server (and any listening clients) that the job failed.
548  
549 =cut
550  
551 sub send_work_fail {
552 0 ▶ return shift->_finish_job_request("work_fail", shift);
Subroutine send_work_fail: not called CC=1 SCAR=6.9
Pod documented
553 }
554  
555 =head2 send_work_status($job, $numerator, $denominator)
556  
557 Use this method to send periodically to the server status update for long running jobs to update the percentage
558 complete.
559  
560 =cut
561  
562 sub send_work_status {
563 0 ▶ my ($self, $job, $numerator, $denominator) = @_;
Subroutine send_work_status: not called CC=1 SCAR=6.9
Pod documented
564 0 return $self->_job_request("work_status", $job, $numerator, $denominator);
565 }
566  
567 # _finish_job_request($cmd, $job, [$v])
568 #
569 # send some data or message to the client for finished job
570 # $cmd = work_complete || work_fail
571 #
572 sub _finish_job_request {
573 0 ▶ my ($self, $cmd, $job, $v) = @_;
Subroutine _finish_job_request: not called CC=2 SCAR=17.9
574 0 ▶ my $res = $self->_job_request($cmd, $job, ref($v) ? ${$v} : $v);
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 0 0 0
575  
576 # set job done flag because work method check it
577 0 $job_done{ $job->handle } = { command => $cmd, result => $res };
578  
579 0 return $res;
580 } ## end sub _finish_job_request
581  
582 # _job_request($cmd, $job, [$v])
583 #
584 # send some data to the client for the running job
585 #
586 sub _job_request {
587 0 ▶ my ($self, $cmd, $job, $v) = @_;
Subroutine _job_request: not called CC=2 SCAR=17.9
588 0 ▶ my $req = _rc($cmd, $v ? _join0($job->handle, $v) : $job->handle);
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 0 0 0
589  
590 0 return _send($job->{jss}, \$req);
591 } ## end sub _job_request
592  
593 #
594 # _register_all($req)
595 #
596 sub _register_all {
597 1 ▶ my ($self, $req) = @_;
Subroutine _register_all: called CC=5 SCAR=26.7
598 1 my @job_servers = $self->job_servers();
599 1 my $done = 0;
600 1 foreach my $js (@job_servers) {
601 0 my $jss = $self->_get_js_sock($js);
602  
603 0 ▶ ($jss && $req) || next;
Branch: 1 branch
BranchTrueFalseTotal
$jss and $req or next 0 0 0
Condition0%0/3
$jss and $req
!ll&&!rl&&r
000
MC/DC0%0/2
$jss and $req
$jss $req
Truth table0%0/3
A and B
A= $jssB= $req
ABresult
0X0
100
111
604  
605 0 ▶ unless (_send($jss, \$req)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_send($jss, \$req)) 0 0 0
606 0 $self->_uncache_sock($js, "$req request failed");
607 0 next;
608 }
609  
610 0 $done++;
611 } ## end foreach my $js (@job_servers)
612  
613 1 return $done == scalar @job_servers;
614 } ## end sub _register_all
615  
616 #
617 # _get_js_sock($js, %opts)
618 #
619 sub _get_js_sock {
620 3 ▶ my ($self, $js, %opts) = @_;
Subroutine _get_js_sock: called CC=19 SCAR=48.0
621 3 ▶ $js || return;
Branch: 1 branch
BranchTrueFalseTotal
$js or return 1 2 3
622  
623 2 my $js_str = $self->_js_str($js);
624 2 my $on_connect = delete $opts{on_connect};
625  
626 # Someday should warn when called with extra opts.
627  
628 2 ▶ warn "getting job server socket: $js_str" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 2 2
629  
630 # special case, if we're a child process of a gearman::server
631 # parent process, talking over a unix pipe...
632 2 ▶ return $self->{parent_pipe} if $self->{parent_pipe};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent_pipe"} 1 1 2
633  
634 1 ▶ if (my $sock = $self->_sock_cache($js)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sock = $self->_sock_cache($js)) 0 1 1
635 0 ▶ return $sock if getpeername($sock);
Branch: 1 branch
BranchTrueFalseTotal
if getpeername $sock 0 0 0
636  
637 0 $self->_uncache_sock($js, "getpeername failed");
638 }
639  
640 1 my $now = time;
641 1 my $down_since = $self->{down_since}{$js_str};
642 1 ▶ if ($down_since) {
Branch: 1 branch
BranchTrueFalseTotal
if ($down_since) 0 1 1
643 0 my $down_for = $now - $down_since;
644 0 ▶ warn "$js_str down for $down_for" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
645  
646 0 ▶ my $retry_period = $down_for > 60 ? 30 : (int($down_for / 2) + 1);
Branch: 1 branch
BranchTrueFalseTotal
$down_for > 60 ? : 0 0 0
647 0 ▶ if ($self->{last_connect_fail}{$js_str} > $now - $retry_period) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"last_connect_fail"}{$js_str} > $now - $retry_period) 0 0 0
648 0 return;
649 }
650 } ## end if ($down_since)
651  
652 1 ▶ warn "connecting to '$js_str'" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
653  
654 1 my $sock = $self->socket($js, 1);
655 1 ▶ unless ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 1 0 1
656 1 ▶ $self->{down_since}{$js_str} ||= $now;
Condition33%1/3
$self->{"down_since"}{$js_str} ||= $now
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"down_since"}{$js_str} ||= $now
$self->{"down_since"}{$js_str} $now
Truth table33%1/3
A ||= B
A= $self->{"down_since"}{$js_str}B= $now
ABresult
1X1
011
000
657 1 $self->{last_connect_fail}{$js_str} = $now;
658  
659 1 return;
660 } ## end unless ($sock)
661  
662 0 $sock->autoflush(1);
663 0 $self->sock_nodelay($sock);
664  
665 0 delete $self->{last_connect_fail}{$js_str};
666 0 delete $self->{down_since}{$js_str};
667  
668 0 ▶ if ($opts{register_on_reconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"register_on_reconnect"}) 0 0 0
669 0 my @fail = ();
670 0 foreach (keys %{ $self->{can} }) {
671 0 ▶ $self->_register_function($_, $js, $sock) || push @fail, $_;
Branch: 1 branch
BranchTrueFalseTotal
$self->_register_function($_, $js, $sock) or push @fail, $_ 0 0 0
672 }
673  
674 0 ▶ if (@fail) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fail) 0 0 0
675 0 $self->_uncache_sock($js, join ' ', "failed registration of",
676 @fail);
677 0 return;
678 }
679 } ## end if ($opts{register_on_reconnect...})
680  
681 0 $self->_sock_cache($js, $sock);
682  
683 0 ▶ if ($on_connect && !$on_connect->($sock)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_connect and not &$on_connect($sock)) 0 0 0
Condition0%0/3
$on_connect and not &$on_connect($sock)
!ll&&!rl&&r
000
MC/DC0%0/2
$on_connect and not &$on_connect($sock)
$on_connect not &$on_connect($sock)
Truth table0%0/3
A and not(B)
A= $on_connectB= not &$on_connect($sock)
ABresult
0X0
100
111
684 0 $self->_uncache_sock($js, "on connect callback failed");
685 0 return;
686 }
687  
688 0 return $sock;
689 } ## end sub _get_js_sock
690  
691 =head2 _uncache_sock($js, $reason)
692  
693 close TCP connection
694  
695 =cut
696  
697 sub _uncache_sock {
698 0 ▶ my ($self, $js, $reason) = @_;
Subroutine _uncache_sock: not called CC=4 SCAR=30.0
699  
700 # we can't reconnect as a child process, so all we can do is die and hope our
701 # parent process respawns us...
702 die "Error/timeout talking to gearman parent process: [$reason]"
703 0 ▶ if $self->{parent_pipe};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent_pipe"} 0 0 0
704  
705 0 ▶ $self->debug && warn join ' ', "close connection to", $self->_js_str($js),
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn join(" ", "close connection to", $self->_js_str($js), $reason || "") 0 0 0
Condition0%0/2
$reason || ''
l!l
00
MC/DC0%0/1
$reason || ''
$reason
Truth table0%0/2
A
A= $reason
Aresult
11
00
706 $reason || '';
707  
708 # normal case, we just close this TCP connection and we'll reconnect later.
709 # delete cached sock
710 0 $self->_sock_cache($js, undef, 1);
711 } ## end sub _uncache_sock
712  
713 #
714 # _set_client_id($sock)
715 #
716 sub _set_client_id {
717 0 ▶ my ($self, $sock) = @_;
Subroutine _set_client_id: not called CC=1 SCAR=6.9
718 0 my $req = _rc("set_client_id", $self->{client_id});
719 0 return _send($sock, \$req);
720 }
721  
722 #
723 # _set_ability($sock, $ability, [$timeout])
724 #
725 sub _set_ability {
726 3 ▶ my ($self, $sock, $ability, $timeout) = @_;
Subroutine _set_ability: called CC=2 SCAR=6.9
727 3 my $req;
728 3 ▶ if (defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) { } 1 2 3
729 1 $req = _rc("can_do_timeout", _join0($ability, $timeout));
730 }
731 else {
732 2 $req = _rc("can_do", $ability);
733 }
734 3 return _send($sock, \$req);
735 } ## end sub _set_ability
736  
737 #
738 # _register_function($ability, $js, [$sock])
739 # set client id
740 # can do
741 #
742 sub _register_function {
743 0 ▶ my ($self, $ability, $js, $sock) = @_;
Subroutine _register_function: not called CC=5 SCAR=34.0
744 0 ▶ $sock ||= $self->_get_js_sock($js);
Condition0%0/3
$sock ||= $self->_get_js_sock($js)
l!l&&r!l&&!r
000
MC/DC0%0/2
$sock ||= $self->_get_js_sock($js)
$sock $self->_get_js_sock($js)
Truth table0%0/3
A ||= B
A= $sockB= $self->_get_js_sock($js)
ABresult
1X1
011
000
745 0 ▶ $sock || return;
Branch: 1 branch
BranchTrueFalseTotal
$sock or return 0 0 0
746  
747 0 ▶ unless ($self->_set_client_id($sock)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_set_client_id($sock)) 0 0 0
748 0 $self->_uncache_sock($js, "set client id request failed");
749 0 return;
750 }
751  
752 0 ▶ unless ($self->_set_ability($sock, $ability, $self->{timeouts}{$ability})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_set_ability($sock, $ability, $self->{"timeouts"}{$ability})) 0 0 0
753 0 $self->_uncache_sock($js, "can do request failed");
754 0 return;
755 }
756  
757 0 return 1;
758 } ## end sub _register_function
759  
760 #
761 # _send($jss, $req_ref)
762 #
763 # send C<$req> to C<$jss>
764 #
765 11 *_send = \&Gearman::Util::send_req;
766  
767 #
768 # _rc($cmd, [@val])
769 #
770 11 *_rc = \&Gearman::Util::pack_req_command;
771  
772 #
773 # _join0(@v)
774 #
775 sub _join0 {
776 1 ▶ return join("\0", @_);
Subroutine _join0: called CC=1 SCAR=0.0
777 }
778  
779 11 1;
780 __END__
↑ Summary Util.pm »