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