Worker.pm

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