Client.pm

filter: Statementstmt 37.0% 74 / 200 Branchbran 19.5% 16 / 82 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 53.3% 16 / 30 Podpod 100% 10 / 10 totaltotal 32.4% 116 / 358 CC 52 SCAR 36.0
CC
52
Cov
30%
_get_js_sock
110.0
get_status
90.0
_job_server_status_command
72.0
CRAP 36.5
« Taskset.pm ↑ Summary ResponseParser.pm »
1 package Gearman::Client;
2 7 ▶use version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 $Gearman::Client::VERSION = version->declare("2.004.015");
4  
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Gearman::Client - Client for gearman distributed job system
11  
12 =head1 SYNOPSIS
13  
14 use Gearman::Client;
15 my $client = Gearman::Client->new;
16 $client->job_servers(
17 '127.0.0.1',
18 {
19 host => '10.0.0.1',
20 port => 4730,
21 socket_cb => sub {...},
22 use_ssl => 1,
23 ca_file => ...,
24 cert_file => ...,
25 key_file => ...,
26 }
27 );
28  
29 # running a single task
30 my $result_ref = $client->do_task("add", "1+2", {
31 on_fail => sub {...},
32 on_complete => sub {...}
33 });
34 print "1 + 2 = $$result_ref\n";
35  
36 # waiting on a set of tasks in parallel
37 my $taskset = $client->new_task_set;
38 $taskset->add_task( "add" => "1+2", {
39 on_complete => sub { ... }
40 });
41 $taskset->add_task( "divide" => "5/0", {
42 on_fail => sub { print "divide by zero error!\n"; },
43 });
44 $taskset->wait;
45  
46  
47 =head1 DESCRIPTION
48  
49 I<Gearman::Client> is a client class for the Gearman distributed job
50 system, providing a framework for sending jobs to one or more Gearman
51 servers. These jobs are then distributed out to a farm of workers.
52  
53 Callers instantiate a I<Gearman::Client> object and from it dispatch
54 single tasks, sets of tasks, or check on the status of tasks.
55  
56 I<Gearman::Client> is derived from L<Gearman::Objects>
57  
58 =head1 USAGE
59  
60 =head2 Gearman::Client->new(%options)
61  
62 Creates a new I<Gearman::Client> object, and returns the object.
63  
64 If I<%options> is provided, initializes the new client object with the
65 settings in I<%options>, which can contain:
66  
67 =over 4
68  
69 =item
70  
71 exceptions
72  
73 If true, the client sends an L<OPTION_REQ exceptions|http://gearman.org/protocol/> request for each connection to the job server.
74 This causes job server to forward WORK_EXCEPTION packets to the client.
75  
76 =item
77  
78 job_servers
79  
80 List of job servers. Value should be an array reference, hash reference
81 or scalar.
82  
83 Calls L<Gearman::Objects> to set I<job_servers>
84  
85 =item
86  
87 prefix
88  
89 Calls I<prefix> (see L<Gearman::Objects>) to set the prefix / namespace.
90  
91 =item
92  
93 command_timeout
94  
95 Maximum time a gearman command should take to get a result (not a job timeout)
96  
97 default: 30 seconds
98  
99 =item
100  
101 backoff_max
102  
103 Max number of failed connection attempts before an job server will be temporary disabled
104  
105 default: 90
106  
107 =back
108  
109 =head1 EXAMPLES
110  
111 =head2 Summation
112  
113 This is an example client that sends off a request to sum up a list of
114 integers.
115  
116 use Gearman::Client;
117 use Storable qw( freeze );
118 my $client = Gearman::Client->new;
119 $client->job_servers('127.0.0.1');
120 my $tasks = $client->new_task_set;
121 my $handle = $tasks->add_task(sum => freeze([ 3, 5 ]), {
122 on_complete => sub { print ${ $_[0] }, "\n" }
123 });
124 $tasks->wait;
125  
126 See the L<Gearman::Worker> documentation for the worker for the I<sum>
127 function.
128  
129 =head1 NOTE
130  
131 If you intend using UTF-8 data with SSL based connection,
132 beware there is no UTF-8 support in underlying L<Net::SSLeay>.
133 L<perlunicode/"Forcing-Unicode-in-Perl-(Or-Unforcing-Unicode-in-Perl)"> describes proper workarounds.
134  
135 =cut
136  
137 7 ▶use base 'Gearman::Objects';
Subroutine BEGIN: called CC=1 SCAR=0.0
138  
139 7 ▶use fields (
Subroutine BEGIN: called CC=1 SCAR=0.0
140 7 'sock_info', # hostport -> hashref
141 'hooks', # hookname -> coderef
142 'exceptions',
143 'backoff_max',
144  
145 # maximum time a gearman command should take to get a result (not a job timeout)
146 'command_timeout',
147 7 );
148  
149 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 7 ▶use Gearman::Task;
Subroutine BEGIN: called CC=1 SCAR=0.0
151 7 ▶use Gearman::Taskset;
Subroutine BEGIN: called CC=1 SCAR=0.0
152 7 ▶use Gearman::JobStatus;
Subroutine BEGIN: called CC=1 SCAR=0.0
153 7 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
154  
155 9 ▶sub new {
Subroutine new: called CC=5 SCAR=16.5
Pod documented
156 9 my ($self, %opts) = @_;
157 9 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 9 0 9
158 9 $self = fields::new($self);
159 }
160  
161 9 $self->SUPER::new(%opts);
162  
163 9 $self->{hooks} = {};
164 9 $self->{exceptions} = 0;
165 9 $self->{backoff_max} = 90;
166 9 $self->{command_timeout} = 30;
167  
168 $self->{exceptions} = delete $opts{exceptions}
169 9 ▶ if exists $opts{exceptions};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"exceptions"} 0 9 9
170  
171 $self->{backoff_max} = $opts{backoff_max}
172 9 ▶ if defined $opts{backoff_max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"backoff_max"} 0 9 9
173  
174 $self->{command_timeout} = $opts{command_timeout}
175 9 ▶ if defined $opts{command_timeout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"command_timeout"} 0 9 9
176  
177 9 return $self;
178 } ## end sub new
179  
180 =head1 METHODS
181  
182 =head2 new_task_set()
183  
184 Creates and returns a new L<Gearman::Taskset> object.
185  
186 =cut
187  
188 1 ▶sub new_task_set {
Subroutine new_task_set: called CC=1 SCAR=0.0
Pod documented
189 1 my $self = shift;
190 1 my $taskset = Gearman::Taskset->new($self);
191 1 $self->run_hook('new_task_set', $self, $taskset);
192 1 return $taskset;
193 } ## end sub new_task_set
194  
195 #
196 # _job_server_status_command($command, $each_line_sub)
197 # $command e.g. "status\n".
198 # $each_line_sub A sub to be called on each line of response;
199 # takes $hostport and the $line as args.
200 #
201 0 ▶sub _job_server_status_command {
Subroutine _job_server_status_command: not called CC=8 SCAR=42.8
202 0 my ($self, $command, $each_line_sub) = (shift, shift, shift);
203  
204 0 ▶ my $list
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 0 0 0
205 = scalar(@_)
206 ? $self->canonicalize_job_servers(@_)
207 : $self->job_servers();
208 0 my %js_map = map { $self->_js_str($_) => 1 } $self->job_servers();
209  
210 0 foreach my $js (@{$list}) {
211 0 ▶ defined($js_map{ $self->_js_str($js) }) || next;
Branch: 1 branch
BranchTrueFalseTotal
defined $js_map{$self->_js_str($js)} or next 0 0 0
212  
213 0 ▶ my $sock = $self->_get_js_sock($js)
Branch: 1 branch
BranchTrueFalseTotal
my $sock = $self->_get_js_sock($js) or next 0 0 0
214 or next;
215  
216 0 my $rv = $sock->write($command);
217  
218 0 my $err;
219 0 my @lines = Gearman::Util::read_text_status($sock, \$err);
220 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
221  
222 0 ▶ $self->debug() && warn $err;
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn $err 0 0 0
223 0 next;
224 }
225  
226 0 foreach my $l (@lines) {
227 0 $each_line_sub->($js, $l);
228 }
229  
230 0 $self->_sock_cache($js, $sock);
231 } ## end foreach my $js (@{$list})
232 } ## end sub _job_server_status_command
233  
234 =head2 get_job_server_status()
235  
236 B<return> C<< {job_server => {job => {capable, queued, running}}} >>
237  
238 =cut
239  
240 0 ▶sub get_job_server_status {
Subroutine get_job_server_status: not called CC=1 SCAR=6.9
Pod documented
241 0 my $self = shift;
242  
243 0 my $js_status = {};
244 $self->_job_server_status_command(
245 "status\n",
246 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
247 0 my ($js, $line) = @_;
248 0 ▶ unless ($line =~ /^(\S+)\s+(\d+)\s+(\d+)\s+(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^(\S+)\s+(\d+)\s+(\d+)\s+(\d+)$/) 0 0 0
249 0 return;
250 }
251  
252 0 my ($job, $queued, $running, $capable) = ($1, $2, $3, $4);
253 0 $js_status->{ $self->_js_str($js) }->{$job} = {
254 queued => $queued,
255 running => $running,
256 capable => $capable,
257 };
258 },
259 @_
260 0 );
261 0 return $js_status;
262 } ## end sub get_job_server_status
263  
264 =head2 get_job_server_jobs()
265  
266 supported only by L<Gearman::Server>
267  
268 B<return> C<< {job-server => {job => {address, listeners, key}}} >>
269  
270 =cut
271  
272 0 ▶sub get_job_server_jobs {
Subroutine get_job_server_jobs: not called CC=1 SCAR=6.9
Pod documented
273 0 my $self = shift;
274 0 my $js_jobs = {};
275 $self->_job_server_status_command(
276 "jobs\n",
277 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
278 0 my ($js, $line) = @_;
279  
280 # Yes, the unique key is sometimes omitted.
281 0 ▶ return unless $line =~ /^(\S+)\s+(\S*)\s+(\S+)\s+(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^(\S+)\s+(\S*)\s+(\S+)\s+(\d+)$/ 0 0 0
282  
283 0 my ($job, $key, $address, $listeners) = ($1, $2, $3, $4);
284 0 $js_jobs->{ $self->_js_str($js) }->{$job} = {
285 key => $key,
286 address => $address,
287 listeners => $listeners,
288 };
289 },
290 @_
291 0 );
292 0 return $js_jobs;
293 } ## end sub get_job_server_jobs
294  
295 =head2 get_job_server_clients()
296  
297 supported only by L<Gearman::Server>
298  
299 =cut
300  
301 0 ▶sub get_job_server_clients {
Subroutine get_job_server_clients: not called CC=1 SCAR=6.9
Pod documented
302 0 my $self = shift;
303  
304 0 my $js_clients = {};
305 0 my $client;
306 $self->_job_server_status_command(
307 "clients\n",
308 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
309 0 my ($js, $line) = @_;
310 0 my $js_str = $self->_js_str($js);
311 0 ▶ if ($line =~ /^(\S+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^(\S+)$/) { } 0 0 0
elsif ($client and $line =~ /^\s+(\S+)\s+(\S*)\s+(\S+)$/) { } 0 0 0
312 0 $client = $1;
313 0 ▶ $js_clients->{$js_str}->{$client} ||= {};
Condition0%0/2
$js_clients->{$js_str}{$client} ||= {}
l!l
00
MC/DC0%0/1
$js_clients->{$js_str}{$client} ||= {}
$js_clients->{$js_str}{$client}
Truth table0%0/2
A
A= $js_clients->{$js_str}{$client}
Aresult
11
00
314 }
315 ▶ elsif ($client && $line =~ /^\s+(\S+)\s+(\S*)\s+(\S+)$/) {
Condition0%0/3
$client and $line =~ /^\s+(\S+)\s+(\S*)\s+(\S+)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$client and $line =~ /^\s+(\S+)\s+(\S*)\s+(\S+)$/
$client $line =~ /^\s+(\S+)\s+(\S*)\s+(\S+)$/
Truth table0%0/3
A and B
A= $clientB= $line =~ /^\s+(\S+)\s+(\S*)\s+(\S+)$/
ABresult
0X0
100
111
316 0 my ($job, $key, $address) = ($1, $2, $3);
317 0 $js_clients->{$js_str}->{$client}->{$job} = {
318 key => $key,
319 address => $address,
320 };
321 } ## end elsif ($client && $line =~...)
322 },
323 @_
324 0 );
325  
326 0 return $js_clients;
327 } ## end sub get_job_server_clients
328  
329 #
330 # _get_task_from_args
331 #
332 10 ▶sub _get_task_from_args {
Subroutine _get_task_from_args: called CC=5 SCAR=16.3
333 10 my $self = shift;
334 10 my $task;
335 10 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 2 8 10
336 2 $task = shift;
337 2 ▶ $task->isa("Gearman::Task")
Branch: 1 branch
BranchTrueFalseTotal
$task->isa("Gearman::Task") or Carp::croak("Argument isn't a Gearman::Task") 2 0 2
338 || Carp::croak("Argument isn't a Gearman::Task");
339 }
340 else {
341 8 my $func = shift;
342 8 my $arg_p = shift;
343 8 my $opts = shift;
344 8 ▶ my $argref = ref $arg_p ? $arg_p : \$arg_p;
Branch: 1 branch
BranchTrueFalseTotal
ref $arg_p ? : 0 8 8
345 8 ▶ Carp::croak("Function argument must be scalar or scalarref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $argref eq "SCALAR" 0 8 8
346 unless ref $argref eq "SCALAR";
347  
348 8 $task = Gearman::Task->new($func, $argref, $opts);
349 } ## end else [ if (ref $_[0]) ]
350 9 return $task;
351  
352 } ## end sub _get_task_from_args
353  
354 =head2 do_task($task)
355  
356 =head2 do_task($funcname, $arg, \%options)
357  
358 Dispatches a task and waits on the results. May either provide a
359 L<Gearman::Task> object, or the 3 arguments that the L<Gearman::Task>
360 constructor takes.
361  
362 B<return> scalarref of WORK_COMPLETE result, or undef on failure.
363  
364 =cut
365  
366 0 ▶sub do_task {
Subroutine do_task: not called CC=1 SCAR=6.9
Pod documented
367 0 my $self = shift;
368 0 my $task = $self->_get_task_from_args(@_);
369 0 my ($ret, $sub) = (undef, $task->{on_complete});
370 0 ▶ $task->{on_complete} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
371 0 ($ret) = @_;
372 0 ▶ $sub && $sub->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$sub and &$sub(@_) 0 0 0
373 0 };
374  
375 0 my $ts = $self->new_task_set;
376 0 $ts->add_task($task);
377 0 $ts->wait(timeout => $task->timeout);
378  
379 0 return $ret;
380 } ## end sub do_task
381  
382 =head2 dispatch_background($func, $arg_p, $options_hr)
383  
384 =head2 dispatch_background($task)
385  
386 Dispatches a C<task> and doesn't wait for the result. Return value
387 is an opaque scalar that can be used to refer to the task with L<get_status>.
388  
389 B<It is strongly recommended to set> L<Gearman::Task> C<uniq> B<option>
390 to insure gearmand does not squash jobs if it store background jobs in a persistence backend.
391 See the issue L<#87|https://github.com/gearman/gearmand/issues/87#issuecomment-291119785>
392  
393 B<return> the handle from the jobserver, or undef on failure
394  
395 =cut
396  
397 0 ▶sub dispatch_background {
Subroutine dispatch_background: not called CC=1 SCAR=6.9
Pod documented
398 0 my $self = shift;
399 0 my $task = $self->_get_task_from_args(@_);
400  
401 0 $task->{background} = 1;
402  
403 0 my $ts = $self->new_task_set;
404 0 return $ts->add_task($task);
405 } ## end sub dispatch_background
406  
407 =head2 run_hook($name)
408  
409 run a hook callback if defined
410  
411 =cut
412  
413 1 ▶sub run_hook {
Subroutine run_hook: called CC=4 SCAR=14.3
Pod documented
414 1 my ($self, $hookname) = @_;
415 1 ▶ $hookname || return;
Branch: 1 branch
BranchTrueFalseTotal
$hookname or return 1 0 1
416  
417 1 my $hook = $self->{hooks}->{$hookname};
418 1 ▶ return unless $hook;
Branch: 1 branch
BranchTrueFalseTotal
unless $hook 0 1 1
419  
420 1 eval { $hook->(@_) };
421  
422 1 ▶ warn "Gearman::Client hook '$hookname' threw error: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
423 } ## end sub run_hook
424  
425 =head2 add_hook($name, $cb)
426  
427 add a hook
428  
429 =cut
430  
431 2 ▶sub add_hook {
Subroutine add_hook: called CC=3 SCAR=11.0
Pod documented
432 2 my ($self, $hookname) = (shift, shift);
433 2 ▶ $hookname || return;
Branch: 1 branch
BranchTrueFalseTotal
$hookname or return 2 0 2
434  
435 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 1 2
436 1 $self->{hooks}->{$hookname} = shift;
437 }
438 else {
439 1 delete $self->{hooks}->{$hookname};
440 }
441 } ## end sub add_hook
442  
443 =head2 get_status($handle)
444  
445 The Gearman Server will assign a scalar job handle when you request a
446 background job with L<dispatch_background>. Save this scalar, and use it later
447 in order to request the status of this job.
448  
449 B<return> L<Gearman::JobStatus> on success
450  
451 =cut
452  
453 0 ▶sub get_status {
Subroutine get_status: not called CC=9 SCAR=45.0
Pod documented
454 0 my ($self, $handle) = @_;
455 0 ▶ $handle || return;
Branch: 1 branch
BranchTrueFalseTotal
$handle or return 0 0 0
456  
457 0 my ($js_str, $shandle) = split(m!//!, $handle);
458  
459 #TODO simple check for $js_str in job_server doesn't work if
460 # $js_str is not contained in job_servers
461 # job_servers = ["localhost:4730"]
462 # handle = 127.0.0.1:4730//H:...
463 #
464 # hopefully commit 58e2aa5 solves this TODO
465  
466 0 my $js = $self->_js($js_str);
467 0 ▶ $js || return;
Branch: 1 branch
BranchTrueFalseTotal
$js or return 0 0 0
468  
469 0 my $sock = $self->_get_js_sock($js);
470 0 ▶ $sock || return;
Branch: 1 branch
BranchTrueFalseTotal
$sock or return 0 0 0
471  
472 0 my $req = Gearman::Util::pack_req_command("get_status", $shandle);
473 0 my $len = length($req);
474 0 my $rv = $sock->write($req, $len);
475 0 my $err;
476 0 my $res = Gearman::Util::read_res_packet($sock, \$err);
477  
478 0 ▶ if ($res && $res->{type} eq "error") {
Branch: 1 branch
BranchTrueFalseTotal
if ($res and $res->{"type"} eq "error") 0 0 0
Condition0%0/3
$res and $res->{"type"} eq "error"
!ll&&!rl&&r
000
MC/DC0%0/2
$res and $res->{"type"} eq "error"
$res $res->{"type"} eq "error"
Truth table0%0/3
A and B
A= $resB= $res->{"type"} eq "error"
ABresult
0X0
100
111
479 0 Carp::croak
480 0 "Error packet from server after get_status: ${$res->{blobref}}\n";
481 }
482  
483 0 ▶ return undef unless $res && $res->{type} eq "status_res";
Branch: 1 branch
BranchTrueFalseTotal
unless $res and $res->{"type"} eq "status_res" 0 0 0
Condition0%0/3
$res and $res->{"type"} eq "status_res"
!ll&&!rl&&r
000
MC/DC0%0/2
$res and $res->{"type"} eq "status_res"
$res $res->{"type"} eq "status_res"
Truth table0%0/3
A and B
A= $resB= $res->{"type"} eq "status_res"
ABresult
0X0
100
111
484  
485 0 my @args = split(/\0/, ${ $res->{blobref} });
486  
487 #FIXME returns on '', 0
488 0 ▶ $args[0] || return;
Branch: 1 branch
BranchTrueFalseTotal
$args[0] or return 0 0 0
489  
490 0 shift @args;
491 0 $self->_sock_cache($js_str, $sock);
492  
493 0 return Gearman::JobStatus->new(@args);
494 } ## end sub get_status
495  
496 #
497 # _option_request($sock, $option)
498 #
499 0 ▶sub _option_request {
Subroutine _option_request: not called CC=4 SCAR=30.0
500 0 my ($self, $sock, $option) = @_;
501  
502 0 my $req = Gearman::Util::pack_req_command("option_req", $option);
503 0 my $len = length($req);
504 0 my $rv = $sock->write($req, $len);
505  
506 0 my $err;
507 my $res = Gearman::Util::read_res_packet($sock, \$err,
508 0 $self->{command_timeout});
509  
510 0 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
511  
512 0 ▶ return 0 if $res->{type} eq "error";
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"type"} eq "error" 0 0 0
513 0 ▶ return 1 if $res->{type} eq "option_res";
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"type"} eq "option_res" 0 0 0
514  
515 0 warn "Got unknown response to option request: $res->{type}\n";
516 0 return;
517 } ## end sub _option_request
518  
519 #
520 # _get_js_sock($js)
521 #
522 # returns a socket from the cache. it should be returned to the
523 # cache with _sock_cache($js, $sock).
524 # The hostport isn't verified. the caller
525 # should verify that $js is in the set of jobservers.
526 0 ▶sub _get_js_sock {
Subroutine _get_js_sock: not called CC=10 SCAR=47.0
527 0 my ($self, $js) = @_;
528 0 ▶ if (my $sock = $self->_sock_cache($js, undef, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sock = $self->_sock_cache($js, undef, 1)) 0 0 0
529 0 ▶ return $sock if $sock->connected;
Branch: 1 branch
BranchTrueFalseTotal
if $sock->connected 0 0 0
530 }
531  
532 0 ▶ my $sockinfo = $self->{sock_info}{ $self->_js_str($js) } ||= {};
Condition0%0/2
$self->{"sock_info"}{$self->_js_str($js)} ||= {}
l!l
00
MC/DC0%0/1
$self->{"sock_info"}{$self->_js_str($js)} ||= {}
$self->{"sock_info"}{$self->_js_str($js)}
Truth table0%0/2
A
A= $self->{"sock_info"}{$self->_js_str($js)}
Aresult
11
00
533 0 my $disabled_until = $sockinfo->{disabled_until};
534 0 ▶ return if defined $disabled_until && $disabled_until > Time::HiRes::time();
Branch: 1 branch
BranchTrueFalseTotal
if defined $disabled_until and $disabled_until > &Time::HiRes::time() 0 0 0
Condition0%0/3
defined $disabled_until and $disabled_until > &Time::HiRes::time()
!ll&&!rl&&r
000
MC/DC0%0/2
defined $disabled_until and $disabled_until > &Time::HiRes::time()
defined $disabled_until $disabled_until > &Time::HiRes::time()
Truth table0%0/3
A and B
A= defined $disabled_untilB= $disabled_until > &Time::HiRes::time()
ABresult
0X0
100
111
535  
536 0 my $sock = $self->socket($js, 1);
537 0 ▶ unless ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 0 0 0
538 0 my $count = ++$sockinfo->{failed_connects};
539 0 my $disable_for = $count**2;
540 0 my $max = $self->{backoff_max};
541 0 ▶ $disable_for = $disable_for > $max ? $max : $disable_for;
Branch: 1 branch
BranchTrueFalseTotal
$disable_for > $max ? : 0 0 0
542 0 $sockinfo->{disabled_until} = $disable_for + Time::HiRes::time();
543 0 return;
544 } ## end unless ($sock)
545  
546 0 $self->sock_nodelay($sock);
547 0 $sock->autoflush(1);
548  
549 # If exceptions support is to be requested, and the request fails, disable
550 # exceptions for this client.
551 0 ▶ if ($self->{exceptions} && !$self->_option_request($sock, 'exceptions')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"exceptions"} and not $self->_option_request($sock, "exceptions")) 0 0 0
Condition0%0/3
$self->{"exceptions"} and not $self->_option_request($sock, "exceptions")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"exceptions"} and not $self->_option_request($sock, "exceptions")
$self->{"exceptions"} not $self->_option_request($sock, "exceptions")
Truth table0%0/3
A and not(B)
A= $self->{"exceptions"}B= not $self->_option_request($sock, "exceptions")
ABresult
0X0
100
111
552 0 warn "Exceptions support denied by server, disabling.\n";
553 0 $self->{exceptions} = 0;
554 }
555  
556 0 delete $sockinfo->{failed_connects}; # Success, mark the socket as such.
557 0 delete $sockinfo->{disabled_until};
558  
559 0 return $sock;
560 } ## end sub _get_js_sock
561  
562 8 ▶sub _get_random_js_sock {
Subroutine _get_random_js_sock: called CC=5 SCAR=30.3
563 8 my ($self, $getter) = @_;
564  
565 8 ▶ $self->{js_count} || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"js_count"} or return 0 8 8
566  
567 0 ▶ $getter ||= sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
568 0 my $js = shift;
569 0 return $self->_get_js_sock($js);
570 0 ▶ };
Condition0%0/3
$getter ||= sub { my $js = shift(); return $self->_get_js_sock($js); }
l!l&&r!l&&!r
000
MC/DC0%0/2
$getter ||= sub { my $js = shift(); return $self->_get_js_sock($js); }
$getter sub { my $js = shift(); return $self->_get_js_sock($js); }
Truth table0%0/3
A ||= B
A= $getterB= sub { my $js = shift(); return $self->_get_js_sock($js); }
ABresult
1X1
011
000
571  
572 0 my $ridx = int(rand($self->{js_count}));
573 0 ▶ for (my $try = 0; $try < $self->{js_count}; $try++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try < $self->{"js_count"}) 0 0 0
574 0 my $aidx = ($ridx + $try) % $self->{js_count};
575 0 my $js = $self->{job_servers}[$aidx];
576 0 ▶ my $sock = $getter->($js) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $sock = &$getter($js) or next 0 0 0
577 0 return ($js, $sock);
578 } ## end for (my $try = 0; $try ...)
579 0 return ();
580 } ## end sub _get_random_js_sock
581  
582 7 1;
583 __END__
« Taskset.pm ↑ Summary ResponseParser.pm »