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.014");
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 =back
92  
93 =head1 EXAMPLES
94  
95 =head2 Summation
96  
97 This is an example client that sends off a request to sum up a list of
98 integers.
99  
100 use Gearman::Client;
101 use Storable qw( freeze );
102 my $client = Gearman::Client->new;
103 $client->job_servers('127.0.0.1');
104 my $tasks = $client->new_task_set;
105 my $handle = $tasks->add_task(sum => freeze([ 3, 5 ]), {
106 on_complete => sub { print ${ $_[0] }, "\n" }
107 });
108 $tasks->wait;
109  
110 See the L<Gearman::Worker> documentation for the worker for the I<sum>
111 function.
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 =cut
120  
121 7 ▶use base 'Gearman::Objects';
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 use fields (
124 7 'sock_info', # hostport -> hashref
125 'hooks', # hookname -> coderef
126 'exceptions',
127 'backoff_max',
128  
129 # maximum time a gearman command should take to get a result (not a job timeout)
130 'command_timeout',
131 7 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 7 ▶use Gearman::Task;
Subroutine BEGIN: called CC=1 SCAR=0.0
135 7 ▶use Gearman::Taskset;
Subroutine BEGIN: called CC=1 SCAR=0.0
136 7 ▶use Gearman::JobStatus;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 7 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
138  
139 sub new {
140 9 ▶ my ($self, %opts) = @_;
Subroutine new: called CC=5 SCAR=16.5
Pod documented
141 9 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 9 0 9
142 9 $self = fields::new($self);
143 }
144  
145 9 $self->SUPER::new(%opts);
146  
147 9 $self->{hooks} = {};
148 9 $self->{exceptions} = 0;
149 9 $self->{backoff_max} = 90;
150 9 $self->{command_timeout} = 30;
151  
152 $self->{exceptions} = delete $opts{exceptions}
153 9 ▶ if exists $opts{exceptions};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"exceptions"} 0 9 9
154  
155 $self->{backoff_max} = $opts{backoff_max}
156 9 ▶ if defined $opts{backoff_max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"backoff_max"} 0 9 9
157  
158 $self->{command_timeout} = $opts{command_timeout}
159 9 ▶ if defined $opts{command_timeout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"command_timeout"} 0 9 9
160  
161 9 return $self;
162 } ## end sub new
163  
164 =head1 METHODS
165  
166 =head2 new_task_set()
167  
168 Creates and returns a new L<Gearman::Taskset> object.
169  
170 =cut
171  
172 sub new_task_set {
173 1 ▶ my $self = shift;
Subroutine new_task_set: called CC=1 SCAR=0.0
Pod documented
174 1 my $taskset = Gearman::Taskset->new($self);
175 1 $self->run_hook('new_task_set', $self, $taskset);
176 1 return $taskset;
177 } ## end sub new_task_set
178  
179 #
180 # _job_server_status_command($command, $each_line_sub)
181 # $command e.g. "status\n".
182 # $each_line_sub A sub to be called on each line of response;
183 # takes $hostport and the $line as args.
184 #
185 sub _job_server_status_command {
186 0 ▶ my ($self, $command, $each_line_sub) = (shift, shift, shift);
Subroutine _job_server_status_command: not called CC=8 SCAR=42.8
187  
188 0 ▶ my $list
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 0 0 0
189 = scalar(@_)
190 ? $self->canonicalize_job_servers(@_)
191 : $self->job_servers();
192 0 my %js_map = map { $self->_js_str($_) => 1 } $self->job_servers();
193  
194 0 foreach my $js (@{$list}) {
195 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
196  
197 0 ▶ my $sock = $self->_get_js_sock($js)
Branch: 1 branch
BranchTrueFalseTotal
my $sock = $self->_get_js_sock($js) or next 0 0 0
198 or next;
199  
200 0 my $rv = $sock->write($command);
201  
202 0 my $err;
203 0 my @lines = Gearman::Util::read_text_status($sock, \$err);
204 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
205  
206 0 ▶ $self->debug() && warn $err;
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn $err 0 0 0
207 0 next;
208 }
209  
210 0 foreach my $l (@lines) {
211 0 $each_line_sub->($js, $l);
212 }
213  
214 0 $self->_sock_cache($js, $sock);
215 } ## end foreach my $js (@{$list})
216 } ## end sub _job_server_status_command
217  
218 =head2 get_job_server_status()
219  
220 B<return> C<< {job_server => {job => {capable, queued, running}}} >>
221  
222 =cut
223  
224 sub get_job_server_status {
225 0 ▶ my $self = shift;
Subroutine get_job_server_status: not called CC=1 SCAR=6.9
Pod documented
226  
227 0 my $js_status = {};
228 $self->_job_server_status_command(
229 "status\n",
230 sub {
231 0 ▶ my ($js, $line) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
232 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
233 0 return;
234 }
235  
236 0 my ($job, $queued, $running, $capable) = ($1, $2, $3, $4);
237 0 $js_status->{ $self->_js_str($js) }->{$job} = {
238 queued => $queued,
239 running => $running,
240 capable => $capable,
241 };
242 },
243 @_
244 0 );
245 0 return $js_status;
246 } ## end sub get_job_server_status
247  
248 =head2 get_job_server_jobs()
249  
250 supported only by L<Gearman::Server>
251  
252 B<return> C<< {job-server => {job => {address, listeners, key}}} >>
253  
254 =cut
255  
256 sub get_job_server_jobs {
257 0 ▶ my $self = shift;
Subroutine get_job_server_jobs: not called CC=1 SCAR=6.9
Pod documented
258 0 my $js_jobs = {};
259 $self->_job_server_status_command(
260 "jobs\n",
261 sub {
262 0 ▶ my ($js, $line) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
263  
264 # Yes, the unique key is sometimes omitted.
265 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
266  
267 0 my ($job, $key, $address, $listeners) = ($1, $2, $3, $4);
268 0 $js_jobs->{ $self->_js_str($js) }->{$job} = {
269 key => $key,
270 address => $address,
271 listeners => $listeners,
272 };
273 },
274 @_
275 0 );
276 0 return $js_jobs;
277 } ## end sub get_job_server_jobs
278  
279 =head2 get_job_server_clients()
280  
281 supported only by L<Gearman::Server>
282  
283 =cut
284  
285 sub get_job_server_clients {
286 0 ▶ my $self = shift;
Subroutine get_job_server_clients: not called CC=1 SCAR=6.9
Pod documented
287  
288 0 my $js_clients = {};
289 0 my $client;
290 $self->_job_server_status_command(
291 "clients\n",
292 sub {
293 0 ▶ my ($js, $line) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
294 0 my $js_str = $self->_js_str($js);
295 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
296 0 $client = $1;
297 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
298 }
299 ▶ 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
300 0 my ($job, $key, $address) = ($1, $2, $3);
301 0 $js_clients->{$js_str}->{$client}->{$job} = {
302 key => $key,
303 address => $address,
304 };
305 } ## end elsif ($client && $line =~...)
306 },
307 @_
308 0 );
309  
310 0 return $js_clients;
311 } ## end sub get_job_server_clients
312  
313 #
314 # _get_task_from_args
315 #
316 sub _get_task_from_args {
317 10 ▶ my $self = shift;
Subroutine _get_task_from_args: called CC=5 SCAR=16.3
318 10 my $task;
319 10 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 2 8 10
320 2 $task = shift;
321 2 ▶ $task->isa("Gearman::Task")
Branch: 1 branch
BranchTrueFalseTotal
$task->isa("Gearman::Task") or Carp::croak("Argument isn't a Gearman::Task") 0 2 2
322 || Carp::croak("Argument isn't a Gearman::Task");
323 }
324 else {
325 8 my $func = shift;
326 8 my $arg_p = shift;
327 8 my $opts = shift;
328 8 ▶ my $argref = ref $arg_p ? $arg_p : \$arg_p;
Branch: 1 branch
BranchTrueFalseTotal
ref $arg_p ? : 0 8 8
329 8 ▶ Carp::croak("Function argument must be scalar or scalarref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $argref eq "SCALAR" 0 8 8
330 unless ref $argref eq "SCALAR";
331  
332 8 $task = Gearman::Task->new($func, $argref, $opts);
333 } ## end else [ if (ref $_[0]) ]
334 9 return $task;
335  
336 } ## end sub _get_task_from_args
337  
338 =head2 do_task($task)
339  
340 =head2 do_task($funcname, $arg, \%options)
341  
342 Dispatches a task and waits on the results. May either provide a
343 L<Gearman::Task> object, or the 3 arguments that the L<Gearman::Task>
344 constructor takes.
345  
346 B<return> scalarref of WORK_COMPLETE result, or undef on failure.
347  
348 =cut
349  
350 sub do_task {
351 0 ▶ my $self = shift;
Subroutine do_task: not called CC=1 SCAR=6.9
Pod documented
352 0 my $task = $self->_get_task_from_args(@_);
353 0 my ($ret, $sub) = (undef, $task->{on_complete});
354 $task->{on_complete} = sub {
355 0 ▶ ($ret) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
356 0 ▶ $sub && $sub->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$sub and &$sub(@_) 0 0 0
357 0 };
358  
359 0 my $ts = $self->new_task_set;
360 0 $ts->add_task($task);
361 0 $ts->wait(timeout => $task->timeout);
362  
363 0 return $ret;
364 } ## end sub do_task
365  
366 =head2 dispatch_background($func, $arg_p, $options_hr)
367  
368 =head2 dispatch_background($task)
369  
370 Dispatches a C<task> and doesn't wait for the result. Return value
371 is an opaque scalar that can be used to refer to the task with L<get_status>.
372  
373 B<It is strongly recommended to set> L<Gearman::Task> C<uniq> B<option>
374 to insure gearmand does not squash jobs if it store background jobs in a persistence backend.
375 See the issue L<#87|https://github.com/gearman/gearmand/issues/87#issuecomment-291119785>
376  
377 B<return> the handle from the jobserver, or undef on failure
378  
379 =cut
380  
381 sub dispatch_background {
382 0 ▶ my $self = shift;
Subroutine dispatch_background: not called CC=1 SCAR=6.9
Pod documented
383 0 my $task = $self->_get_task_from_args(@_);
384  
385 0 $task->{background} = 1;
386  
387 0 my $ts = $self->new_task_set;
388 0 return $ts->add_task($task);
389 } ## end sub dispatch_background
390  
391 =head2 run_hook($name)
392  
393 run a hook callback if defined
394  
395 =cut
396  
397 sub run_hook {
398 1 ▶ my ($self, $hookname) = @_;
Subroutine run_hook: called CC=4 SCAR=14.3
Pod documented
399 1 ▶ $hookname || return;
Branch: 1 branch
BranchTrueFalseTotal
$hookname or return 0 1 1
400  
401 1 my $hook = $self->{hooks}->{$hookname};
402 1 ▶ return unless $hook;
Branch: 1 branch
BranchTrueFalseTotal
unless $hook 0 1 1
403  
404 1 eval { $hook->(@_) };
405  
406 1 ▶ warn "Gearman::Client hook '$hookname' threw error: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
407 } ## end sub run_hook
408  
409 =head2 add_hook($name, $cb)
410  
411 add a hook
412  
413 =cut
414  
415 sub add_hook {
416 2 ▶ my ($self, $hookname) = (shift, shift);
Subroutine add_hook: called CC=3 SCAR=11.0
Pod documented
417 2 ▶ $hookname || return;
Branch: 1 branch
BranchTrueFalseTotal
$hookname or return 0 2 2
418  
419 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 1 2
420 1 $self->{hooks}->{$hookname} = shift;
421 }
422 else {
423 1 delete $self->{hooks}->{$hookname};
424 }
425 } ## end sub add_hook
426  
427 =head2 get_status($handle)
428  
429 The Gearman Server will assign a scalar job handle when you request a
430 background job with L<dispatch_background>. Save this scalar, and use it later
431 in order to request the status of this job.
432  
433 B<return> L<Gearman::JobStatus> on success
434  
435 =cut
436  
437 sub get_status {
438 0 ▶ my ($self, $handle) = @_;
Subroutine get_status: not called CC=9 SCAR=45.0
Pod documented
439 0 ▶ $handle || return;
Branch: 1 branch
BranchTrueFalseTotal
$handle or return 0 0 0
440  
441 0 my ($js_str, $shandle) = split(m!//!, $handle);
442  
443 #TODO simple check for $js_str in job_server doesn't work if
444 # $js_str is not contained in job_servers
445 # job_servers = ["localhost:4730"]
446 # handle = 127.0.0.1:4730//H:...
447 #
448 # hopefully commit 58e2aa5 solves this TODO
449  
450 0 my $js = $self->_js($js_str);
451 0 ▶ $js || return;
Branch: 1 branch
BranchTrueFalseTotal
$js or return 0 0 0
452  
453 0 my $sock = $self->_get_js_sock($js);
454 0 ▶ $sock || return;
Branch: 1 branch
BranchTrueFalseTotal
$sock or return 0 0 0
455  
456 0 my $req = Gearman::Util::pack_req_command("get_status", $shandle);
457 0 my $len = length($req);
458 0 my $rv = $sock->write($req, $len);
459 0 my $err;
460 0 my $res = Gearman::Util::read_res_packet($sock, \$err);
461  
462 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
463 0 Carp::croak
464 0 "Error packet from server after get_status: ${$res->{blobref}}\n";
465 }
466  
467 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
468  
469 0 my @args = split(/\0/, ${ $res->{blobref} });
470  
471 #FIXME returns on '', 0
472 0 ▶ $args[0] || return;
Branch: 1 branch
BranchTrueFalseTotal
$args[0] or return 0 0 0
473  
474 0 shift @args;
475 0 $self->_sock_cache($js_str, $sock);
476  
477 0 return Gearman::JobStatus->new(@args);
478 } ## end sub get_status
479  
480 #
481 # _option_request($sock, $option)
482 #
483 sub _option_request {
484 0 ▶ my ($self, $sock, $option) = @_;
Subroutine _option_request: not called CC=4 SCAR=30.0
485  
486 0 my $req = Gearman::Util::pack_req_command("option_req", $option);
487 0 my $len = length($req);
488 0 my $rv = $sock->write($req, $len);
489  
490 0 my $err;
491 my $res = Gearman::Util::read_res_packet($sock, \$err,
492 0 $self->{command_timeout});
493  
494 0 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
495  
496 0 ▶ return 0 if $res->{type} eq "error";
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"type"} eq "error" 0 0 0
497 0 ▶ return 1 if $res->{type} eq "option_res";
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"type"} eq "option_res" 0 0 0
498  
499 0 warn "Got unknown response to option request: $res->{type}\n";
500 0 return;
501 } ## end sub _option_request
502  
503 #
504 # _get_js_sock($js)
505 #
506 # returns a socket from the cache. it should be returned to the
507 # cache with _sock_cache($js, $sock).
508 # The hostport isn't verified. the caller
509 # should verify that $js is in the set of jobservers.
510 sub _get_js_sock {
511 0 ▶ my ($self, $js) = @_;
Subroutine _get_js_sock: not called CC=10 SCAR=47.0
512 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
513 0 ▶ return $sock if $sock->connected;
Branch: 1 branch
BranchTrueFalseTotal
if $sock->connected 0 0 0
514 }
515  
516 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
517 0 my $disabled_until = $sockinfo->{disabled_until};
518 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
519  
520 0 my $sock = $self->socket($js, 1);
521 0 ▶ unless ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 0 0 0
522 0 my $count = ++$sockinfo->{failed_connects};
523 0 my $disable_for = $count**2;
524 0 my $max = $self->{backoff_max};
525 0 ▶ $disable_for = $disable_for > $max ? $max : $disable_for;
Branch: 1 branch
BranchTrueFalseTotal
$disable_for > $max ? : 0 0 0
526 0 $sockinfo->{disabled_until} = $disable_for + Time::HiRes::time();
527 0 return;
528 } ## end unless ($sock)
529  
530 0 $self->sock_nodelay($sock);
531 0 $sock->autoflush(1);
532  
533 # If exceptions support is to be requested, and the request fails, disable
534 # exceptions for this client.
535 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
536 0 warn "Exceptions support denied by server, disabling.\n";
537 0 $self->{exceptions} = 0;
538 }
539  
540 0 delete $sockinfo->{failed_connects}; # Success, mark the socket as such.
541 0 delete $sockinfo->{disabled_until};
542  
543 0 return $sock;
544 } ## end sub _get_js_sock
545  
546 sub _get_random_js_sock {
547 8 ▶ my ($self, $getter) = @_;
Subroutine _get_random_js_sock: called CC=5 SCAR=30.3
548  
549 8 ▶ $self->{js_count} || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"js_count"} or return 8 0 8
550  
551 $getter ||= sub {
552 0 ▶ my $js = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
553 0 return $self->_get_js_sock($js);
554 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
555  
556 0 my $ridx = int(rand($self->{js_count}));
557 0 ▶ for (my $try = 0; $try < $self->{js_count}; $try++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try < $self->{"js_count"}) 0 0 0
558 0 my $aidx = ($ridx + $try) % $self->{js_count};
559 0 my $js = $self->{job_servers}[$aidx];
560 0 ▶ my $sock = $getter->($js) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $sock = &$getter($js) or next 0 0 0
561 0 return ($js, $sock);
562 } ## end for (my $try = 0; $try ...)
563 0 return ();
564 } ## end sub _get_random_js_sock
565  
566 7 1;
567 __END__
« Taskset.pm ↑ Summary ResponseParser.pm »