Taskset.pm

filter: Statementstmt 63.1% 173 / 274 Branchbran 37.0% 40 / 108 Conditioncond 23.0% 9 / 39 MC/DCmcdc 12.0% 3 / 25 Subroutinesub 86.4% 32 / 37 Podpod 37.5% 3 / 8 totaltotal 52.9% 260 / 491 CC 75 SCAR 40.4
CC
75
Cov
53%
wait
306.0
_ip_port
70.4
add_task
21.2
CRAP 57.0
« Util.pm ↑ Summary Client.pm »
1 package Gearman::Taskset;
2 7 ▶use version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 $Gearman::Taskset::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::Taskset - a taskset in Gearman, from the point of view of a L<Gearman::Client>
11  
12 =head1 SYNOPSIS
13  
14 use Gearman::Client;
15 my $client = Gearman::Client->new;
16  
17 # waiting on a set of tasks in parallel
18 my $ts = $client->new_task_set;
19 $ts->add_task( "add" => "1+2", {...});
20 $ts->wait();
21  
22  
23 =head1 DESCRIPTION
24  
25 Gearman::Taskset is a L<Gearman::Client>'s representation of tasks queue
26  
27 =head1 METHODS
28  
29 =cut
30  
31 7 ▶use fields (
Subroutine BEGIN: called CC=1 SCAR=0.0
32 7 qw/
33 waiting
34 client
35 need_handle
36 default_sock
37 default_sockaddr
38 loaned_sock
39 cancelled
40 hooks
41 /
42 7 );
43  
44 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45 7 ▶use Gearman::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 7 ▶use Gearman::ResponseParser::Taskset;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 7 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 # i thought about weakening taskset's client, but might be too weak.
50 7 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51 7 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
52 7 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
53 7 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 =head2 new($client)
56  
57 =cut
58  
59 7 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
60 7 my ($self, $client) = @_;
61 7 ▶ (Scalar::Util::blessed($client) && $client->isa("Gearman::Client"))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($client) and $client->isa("Gearman::Client") or Carp::croak("provided client argument is not a Gearman::Client reference") 6 1 7
Condition66%2/3
builtin::blessed($client) and $client->isa("Gearman::Client")
!ll&&!rl&&r
106
MC/DC50%1/2
builtin::blessed($client) and $client->isa("Gearman::Client")
builtin::blessed($client) $client->isa("Gearman::Client")
Truth table66%2/3
A and B
A= builtin::blessed($client)B= $client->isa("Gearman::Client")
ABresult
0X0
100
111
62 || Carp::croak
63 "provided client argument is not a Gearman::Client reference";
64  
65 6 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 6 0 6
66 6 $self = fields::new($self);
67 }
68  
69 # { handle => [Task, ...] }
70 6 $self->{waiting} = {};
71 6 $self->{need_handle} = [];
72 6 $self->{client} = $client;
73  
74 # { hostport => socket }
75 6 $self->{loaned_sock} = {};
76  
77 # bool, if taskset has been cancelled mid-processing
78 6 $self->{cancelled} = 0;
79  
80 # { hookname => coderef }
81 6 $self->{hooks} = {};
82  
83 # default socket (non-merged requests)
84 6 $self->{default_sock} = undef;
85  
86 # $self->client()->_js_str($self->{default_sock});
87 6 $self->{default_sockaddr} = undef;
88  
89 6 return $self;
90 } ## end sub new
91  
92 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=14.6
93 4 my $self = shift;
94  
95 # During global cleanup this may be called out of order, and the client my not exist in the taskset.
96 4 ▶ return unless $self->client;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->client 1 3 4
97  
98 3 ▶ if ($self->{default_sock}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"default_sock"}) 0 3 3
99 $self->client->_sock_cache($self->{default_sockaddr},
100 0 $self->{default_sock});
101 }
102  
103 3 keys %{ $self->{loaned_sock} };
104 3 ▶ while (my ($hp, $sock) = each %{ $self->{loaned_sock} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"loaned_sock"};}) 0 3 3
105 0 $self->client->_sock_cache($hp, $sock);
106 }
107 } ## end sub DESTROY
108  
109 #=head2 run_hook($name)
110 #
111 #run a hook callback if defined
112 #
113 #=cut
114  
115 4 ▶sub run_hook {
Subroutine run_hook: called CC=4 SCAR=14.0
Pod undocumented
116 4 my ($self, $name) = (shift, shift);
117 4 ▶ ($name && $self->{hooks}->{$name}) || return;
Branch: 1 branch
BranchTrueFalseTotal
$name and $self->{"hooks"}{$name} or return 1 3 4
Condition66%2/3
$name and $self->{"hooks"}{$name}
!ll&&!rl&&r
031
MC/DC50%1/2
$name and $self->{"hooks"}{$name}
$name $self->{"hooks"}{$name}
Truth table66%2/3
A and B
A= $nameB= $self->{"hooks"}{$name}
ABresult
0X0
100
111
118  
119 1 eval { $self->{hooks}->{$name}->(@_) };
120  
121 1 ▶ warn "Gearman::Taskset hook '$name' threw error: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
122 } ## end sub run_hook
123  
124 #=head2 add_hook($name, [$cb])
125 #
126 #add a hook
127 #
128 #=cut
129  
130 2 ▶sub add_hook {
Subroutine add_hook: called CC=3 SCAR=11.0
Pod undocumented
131 2 my ($self, $name, $cb) = @_;
132 2 ▶ $name || return;
Branch: 1 branch
BranchTrueFalseTotal
$name or return 2 0 2
133  
134 2 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 1 1 2
135 1 $self->{hooks}->{$name} = $cb;
136 }
137 else {
138 1 delete $self->{hooks}->{$name};
139 }
140 } ## end sub add_hook
141  
142 #=head2 client ()
143 #
144 #B<return> L<Gearman::Client>
145 #
146 #=cut
147  
148 # this method is part of the "Taskset" interface, also implemented by
149 # Gearman::Client::Async, where no tasksets make sense, so instead the
150 # Gearman::Client::Async object itself is also its taskset. (the
151 # client tracks all tasks). so don't change this, without being aware
152 # of Gearman::Client::Async. similarly, don't access $ts->{client} without
153 # going via this accessor.
154  
155 28 ▶sub client {
Subroutine client: called CC=1 SCAR=0.0
Pod undocumented
156 28 return shift->{client};
157 }
158  
159 #=head2 cancel()
160 #
161 #Close sockets, cleanup internals.
162 #
163 #=cut
164  
165 1 ▶sub cancel {
Subroutine cancel: called CC=3 SCAR=11.0
Pod undocumented
166 1 my $self = shift;
167  
168 1 $self->{cancelled} = 1;
169  
170 1 ▶ if ($self->{default_sock}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"default_sock"}) 1 0 1
171 1 close($self->{default_sock});
172 1 $self->{default_sock} = undef;
173 }
174  
175 1 foreach my $sock (values %{ $self->{loaned_sock} }) {
176 1 $sock->close;
177 }
178  
179 1 $self->{client} = undef;
180 1 $self->{loaned_sock} = {};
181 1 $self->{need_handle} = [];
182 1 $self->{waiting} = {};
183 } ## end sub cancel
184  
185 #
186 # _get_loaned_sock($js)
187 #
188  
189 0 ▶sub _get_loaned_sock {
Subroutine _get_loaned_sock: not called CC=3 SCAR=24.8
190 0 my ($self, $js) = @_;
191 0 my $js_str = $self->client()->_js_str($js);
192  
193 0 ▶ if (my $sock = $self->{loaned_sock}{$js_str}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sock = $self->{"loaned_sock"}{$js_str}) 0 0 0
194 0 ▶ return $sock if $sock->connected;
Branch: 1 branch
BranchTrueFalseTotal
if $sock->connected 0 0 0
195 0 delete $self->{loaned_sock}{$js_str};
196 }
197  
198 0 my $sock = $self->client()->_get_js_sock($js);
199  
200 0 return $self->{loaned_sock}{$js_str} = $sock;
201 } ## end sub _get_loaned_sock
202  
203 =head2 wait(%opts)
204  
205 Waits for a response from the job server for any of the tasks listed
206 in the taskset. Will call the I<on_*> handlers for each of the tasks
207 that have been completed, updated, etc. Doesn't return until
208 everything has finished running or failing.
209  
210 =cut
211  
212 0 ▶sub wait {
Subroutine wait: not called CC=17 SCAR=57.2
Pod documented
213 0 my ($self, %opts) = @_;
214 0 my ($timeout, $given_timeout_s);
215 0 ▶ if (exists $opts{timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"timeout"}) 0 0 0
216 0 $timeout = delete $opts{timeout};
217 0 ▶ if (defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) 0 0 0
218 ## keep the given timeout value for the failure reason
219 # Handles issue #35
220 # https://github.com/p-alik/perl-Gearman/issues/35
221 0 $given_timeout_s = $timeout;
222 0 $timeout += Time::HiRes::time();
223 }
224 }
225  
226 0 ▶ Carp::carp "Unknown options: "
Branch: 1 branch
BranchTrueFalseTotal
if keys %opts 0 0 0
227 . join(',', keys %opts)
228 . " passed to Taskset->wait."
229 if keys %opts;
230  
231 # fd -> Gearman::ResponseParser object
232 0 my %parser;
233  
234 0 ▶ my $cb = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
235 0 my ($fd) = shift;
236  
237 0 ▶ my $parser = $parser{$fd} ||= Gearman::ResponseParser::Taskset->new(
Condition0%0/3
$parser{$fd} ||= "Gearman::ResponseParser::Taskset"->new("source", $fd, "taskset", $self)
l!l&&r!l&&!r
000
MC/DC0%0/2
$parser{$fd} ||= "Gearman::ResponseParser::Taskset"->new("source", $fd, "taskset", $self)
$parser{$fd} "Gearman::ResponseParser::Taskset"->new("source", $fd, "taskset", $self)
Truth table0%0/3
A ||= B
A= $parser{$fd}B= "Gearman::ResponseParser::Taskset"->new("source", $fd, "taskset", $self)
ABresult
1X1
011
000
238 source => $fd,
239 taskset => $self
240 );
241 eval {
242 0 $parser->parse_sock($fd);
243 0 1;
244 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $parser->parse_sock($fd); 1 } } or do { Carp::croak("Job server failure: $@") } 0 0 0
245  
246 # TODO this should remove the fd from the list, and reassign any tasks to other jobserver, or bail.
247 # We're not in an accessible place here, so if all job servers fail we must die to prevent hanging.
248 0 Carp::croak("Job server failure: $@");
249 } ## end do
250 0 };
251  
252 my $io = IO::Select->new($self->{default_sock},
253 0 values %{ $self->{loaned_sock} });
254  
255 0 my $pending_sock;
256 0 foreach ($io->handles) {
257 0 ▶ (ref($_) eq "IO::Socket::SSL" && $_->pending()) || next;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq "IO::Socket::SSL" and $_->pending or next 0 0 0
Condition0%0/3
ref $_ eq "IO::Socket::SSL" and $_->pending
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ eq "IO::Socket::SSL" and $_->pending
ref $_ eq "IO::Socket::SSL" $_->pending
Truth table0%0/3
A and B
A= ref $_ eq "IO::Socket::SSL"B= $_->pending
ABresult
0X0
100
111
258  
259 0 $pending_sock = $_;
260 0 last;
261 }
262  
263 0 ▶ if ($pending_sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending_sock) 0 0 0
264 0 return $cb->($pending_sock);
265 }
266  
267 0 ▶ while (!$self->{cancelled} && keys %{ $self->{waiting} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"cancelled"} and keys %{$$self{"waiting"};}) 0 0 0
Condition0%0/3
not $self->{"cancelled"} and keys %{$$self{"waiting"};}
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"cancelled"} and keys %{$$self{"waiting"};}
not $self->{"cancelled"} keys %{$$self{"waiting"};}
Truth table0%0/3
not(A) and B
A= not $self->{"cancelled"}B= keys %{$$self{"waiting"};}
ABresult
0X0
100
111
268 0 ▶ my $time_left = $timeout ? $timeout - Time::HiRes::time() : 0.5;
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 0 0 0
269 0 my $nfound = select($io->bits(), undef, undef, $time_left);
270 0 ▶ if ($timeout && $time_left <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout and $time_left <= 0) 0 0 0
Condition0%0/3
$timeout and $time_left <= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$timeout and $time_left <= 0
$timeout $time_left <= 0
Truth table0%0/3
A and B
A= $timeoutB= $time_left <= 0
ABresult
0X0
100
111
271 ## Attempt to fix
272 # https://github.com/p-alik/perl-Gearman/issues/33
273 # Mark all tasks of that taskset failed.
274 # Get all waiting tasks and call their "fail" method one by one
275 # with the failure reason.
276 0 for (values %{ $self->{waiting} }) {
277 0 for (@$_) {
278 0 my $func = $_->func;
279 ## use the given timeout here
280 # Handles issue #35
281 # https://github.com/p-alik/perl-Gearman/issues/35
282 0 $_->fail("Task $func elapsed timeout [${given_timeout_s}s]");
283 }
284 } ## end for (values %{ $self->{...}})
285 0 $self->cancel;
286 0 return;
287 } ## end if ($timeout && $time_left...)
288  
289 0 ▶ next if !$nfound;
Branch: 1 branch
BranchTrueFalseTotal
unless $nfound 0 0 0
290 0 foreach my $fd ($io->can_read()) {
291 0 $cb->($fd);
292 }
293 } ## end while (!$self->{cancelled...})
294 } ## end sub wait
295  
296 =head2 add_task(Gearman::Task)
297  
298 =head2 add_task($func, <$scalar | $scalarref>, <$uniq | $opts_hr>
299  
300 Adds a task to the taskset. Three different calling conventions are available.
301  
302 C<$opts_hr> see L<Gearman::Task>
303  
304 =cut
305  
306 4 ▶sub add_task {
Subroutine add_task: called CC=6 SCAR=30.5
Pod documented
307 4 my $self = shift;
308 4 my $task = $self->client()->_get_task_from_args(@_);
309  
310 3 $task->taskset($self);
311  
312 3 $self->run_hook('add_task', $self, $task);
313  
314 3 my $jssock = $task->{jssock};
315  
316 3 ▶ return $task->fail("undefined jssock") unless ($jssock);
Branch: 1 branch
BranchTrueFalseTotal
unless $jssock 3 0 3
317  
318 0 my $req = $task->pack_submit_packet($self->client);
319 0 ▶ Gearman::Util::send_req($jssock, \$req)
Branch: 1 branch
BranchTrueFalseTotal
Gearman::Util::send_req($jssock, \$req) or Carp::croak("Error sending data to job server") 0 0 0
320 || Carp::croak "Error sending data to job server";
321  
322 0 push @{ $self->{need_handle} }, $task;
323 0 ▶ while (@{ $self->{need_handle} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"need_handle"};}) 0 0 0
324 my $rv
325 = $self->_wait_for_packet($jssock,
326 0 $self->client()->{command_timeout});
327 0 ▶ if (!$rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 0 0 0
328  
329 # ditch it, it failed.
330 # this will resubmit it if it failed.
331 0 shift @{ $self->{need_handle} };
332 0 ▶ return $task->fail(
Branch: 1 branch
BranchTrueFalseTotal
defined $rv ? : 0 0 0
333 join(' ',
334 "no rv on waiting for packet",
335 defined($rv) ? $rv : $!)
336 );
337 } ## end if (!$rv)
338 } ## end while (@{ $self->{need_handle...}})
339  
340 0 return $task->handle;
341 } ## end sub add_task
342  
343 #
344 # _get_default_sock()
345 # used in Gearman::Task->taskset only
346 #
347 8 ▶sub _get_default_sock {
Subroutine _get_default_sock: called CC=4 SCAR=22.1
348 8 my $self = shift;
349 8 ▶ return $self->{default_sock} if $self->{default_sock};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"default_sock"} 0 8 8
350  
351 0 ▶ my $getter = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
352 0 my $js = shift;
353 0 ▶ return $self->{loaned_sock}{$js}
Condition0%0/3
$self->{'loaned_sock'}{$js} || $self->client->_get_js_sock($js)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'loaned_sock'}{$js} || $self->client->_get_js_sock($js)
$self->{'loaned_sock'}{$js} $self->client->_get_js_sock($js)
Truth table0%0/3
A || B
A= $self->{'loaned_sock'}{$js}B= $self->client->_get_js_sock($js)
ABresult
1X1
011
000
354 || $self->client()->_get_js_sock($js);
355 8 };
356  
357 8 my ($js, $jss) = $self->client()->_get_random_js_sock($getter);
358 8 ▶ return unless $jss;
Branch: 1 branch
BranchTrueFalseTotal
unless $jss 8 0 8
359  
360 0 my $js_str = $self->client()->_js_str($js);
361 0 ▶ $self->{loaned_sock}{$js_str} ||= $jss;
Condition0%0/3
$self->{"loaned_sock"}{$js_str} ||= $jss
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"loaned_sock"}{$js_str} ||= $jss
$self->{"loaned_sock"}{$js_str} $jss
Truth table0%0/3
A ||= B
A= $self->{"loaned_sock"}{$js_str}B= $jss
ABresult
1X1
011
000
362  
363 0 $self->{default_sock} = $jss;
364 0 $self->{default_sockaddr} = $js_str;
365  
366 0 return $jss;
367 } ## end sub _get_default_sock
368  
369 #
370 # _get_hashed_sock($hv)
371 #
372 # only used in Gearman::Task->taskset only
373 #
374 # return a socket
375 2 ▶sub _get_hashed_sock {
Subroutine _get_hashed_sock: called CC=2 SCAR=7.8
376 2 my $self = shift;
377 2 my $hv = shift;
378 my ($js_count, @job_servers)
379 2 = ($self->client()->{js_count}, $self->client()->job_servers());
380 2 my $sock;
381 2 ▶ for (my $off = 0; $off < $js_count; $off++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off < $js_count) 0 2 2
382 0 my $idx = ($hv + $off) % ($js_count);
383 0 $sock = $self->_get_loaned_sock($job_servers[$idx]);
384 0 last;
385 }
386  
387 2 return $sock;
388 } ## end sub _get_hashed_sock
389  
390 #
391 # _wait_for_packet($sock, $timeout)
392 #
393 # $sock socket to singularly read from
394 #
395 # returns boolean when given a sock to wait on.
396 # otherwise, return value is undefined.
397 1 ▶sub _wait_for_packet {
Subroutine _wait_for_packet: called CC=3 SCAR=19.2
398 1 my ($self, $sock, $timeout) = @_;
399 1 my $res = Gearman::Util::read_res_packet($sock, \my $err, $timeout);
400 0 ▶ $err && Carp::croak("reading response packet failed: $err");
Branch: 1 branch
BranchTrueFalseTotal
$err and Carp::croak("reading response packet failed: $err") 0 0 0
401  
402 0 ▶ return $res ? $self->process_packet($res, $sock) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 0 0 0
403 } ## end sub _wait_for_packet
404  
405 #
406 # _is_port($sock)
407 #
408 # return hostport || ipport
409 #
410 1 ▶sub _ip_port {
Subroutine _ip_port: called CC=9 SCAR=42.5
411 1 my ($self, $sock) = @_;
412 1 ▶ $sock || return;
Branch: 1 branch
BranchTrueFalseTotal
$sock or return 0 1 1
413  
414 0 my $pn = getpeername($sock);
415 0 ▶ $pn || return;
Branch: 1 branch
BranchTrueFalseTotal
$pn or return 0 0 0
416  
417 # look for a hostport in loaned_sock
418 0 my $hostport;
419 0 my @k = keys %{ $self->{loaned_sock} };
420 0 ▶ while (!$hostport && (my $hp = shift @k)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hostport and my $hp = shift @k) 0 0 0
Condition0%0/3
not $hostport and my $hp = shift @k
!ll&&!rl&&r
000
MC/DC0%0/2
not $hostport and my $hp = shift @k
not $hostport my $hp = shift @k
Truth table0%0/3
not(A) and B
A= not $hostportB= my $hp = shift @k
ABresult
0X0
100
111
421 0 my $s = $self->{loaned_sock}->{$hp};
422 0 ▶ $s || next;
Branch: 1 branch
BranchTrueFalseTotal
$s or next 0 0 0
423 0 ▶ if ($sock == $s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock == $s) 0 0 0
424 0 $hostport = $hp;
425  
426 # last;
427 }
428 } ## end while (!$hostport && (my ...))
429  
430 # hopefully it solves client->get_status mismatch
431 0 ▶ $hostport && return $hostport;
Branch: 1 branch
BranchTrueFalseTotal
$hostport and return $hostport 0 0 0
432  
433 0 my $fam = Socket::sockaddr_family($pn);
434 0 ▶ my ($port, $iaddr)
Branch: 1 branch
BranchTrueFalseTotal
$fam == 10 ? : 0 0 0
435 = ($fam == Socket::AF_INET6)
436 ? Socket::sockaddr_in6($pn)
437 : Socket::sockaddr_in($pn);
438  
439 0 my $addr = Socket::inet_ntop($fam, $iaddr);
440  
441 0 return join ':', $addr, $port;
442 } ## end sub _ip_port
443  
444 #
445 # _fail_jshandle($shandle, $type, [$message])
446 #
447 # note the failure of a task given by its jobserver-specific handle
448 #
449 4 ▶sub _fail_jshandle {
Subroutine _fail_jshandle: called CC=7 SCAR=20.2
450 4 my ($self, $shandle, $type, $msg) = @_;
451 4 ▶ $shandle
Branch: 1 branch
BranchTrueFalseTotal
$shandle or Carp::croak("_fail_jshandle() called without shandle parameter") 3 1 4
452 or Carp::croak "_fail_jshandle() called without shandle parameter";
453  
454 3 ▶ my $task_list = $self->{waiting}{$shandle}
Branch: 1 branch
BranchTrueFalseTotal
my $task_list = $self->{"waiting"}{$shandle} or Carp::croak("Got $type for unknown handle: $shandle") 2 1 3
455 or Carp::croak "Got $type for unknown handle: $shandle";
456  
457 2 my $task = shift @{$task_list};
458 2 ▶ (Scalar::Util::blessed($task) && $task->isa("Gearman::Task"))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($task) and $task->isa("Gearman::Task") or Carp::croak("task_list is empty on $type for handle $shandle\n") 2 0 2
Condition33%1/3
builtin::blessed($task) and $task->isa("Gearman::Task")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($task) and $task->isa("Gearman::Task")
builtin::blessed($task) $task->isa("Gearman::Task")
Truth table33%1/3
A and B
A= builtin::blessed($task)B= $task->isa("Gearman::Task")
ABresult
0X0
100
111
459 || Carp::croak "task_list is empty on $type for handle $shandle\n";
460  
461 2 ▶ $task->fail($msg || "jshandle fail");
Condition50%1/2
$msg || 'jshandle fail'
l!l
02
MC/DC0%0/1
$msg || 'jshandle fail'
$msg
Truth table50%1/2
A
A= $msg
Aresult
11
00
462  
463 2 ▶ delete $self->{waiting}{$shandle} unless @{$task_list};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$task_list;} 2 0 2
464 } ## end sub _fail_jshandle
465  
466 #=head2 process_packet($res, $sock)
467 #
468 # process response packet
469 #
470 #=cut
471  
472 14 ▶sub process_packet {
Subroutine process_packet: called CC=2 SCAR=7.2
Pod undocumented
473 14 my ($self, $res, $sock) = @_;
474  
475 14 my $qr = qr/(.+?)\0/;
476 my %assert = (
477 7 ▶ task => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
478 7 my ($task, $msg) = @_;
479 7 ▶ (Scalar::Util::blessed($task) && $task->isa("Gearman::Task"))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($task) and $task->isa("Gearman::Task") or Carp::croak($msg) 4 3 7
Condition66%2/3
builtin::blessed($task) and $task->isa("Gearman::Task")
!ll&&!rl&&r
304
MC/DC50%1/2
builtin::blessed($task) and $task->isa("Gearman::Task")
builtin::blessed($task) $task->isa("Gearman::Task")
Truth table66%2/3
A and B
A= builtin::blessed($task)B= $task->isa("Gearman::Task")
ABresult
0X0
100
111
480 || Carp::croak $msg;
481 }
482 14 );
483 my %type = (
484 2 ▶ job_created => sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
485 2 my ($blob) = shift;
486 2 my $task = shift @{ $self->{need_handle} };
487 $assert{task}
488 2 ->($task, "Got an unexpected job_created notification");
489 1 my $shandle = $blob;
490 1 my $ipport = $self->_ip_port($sock);
491  
492 # did sock become disconnected in the meantime?
493 1 ▶ if (!$ipport) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ipport) 1 0 1
494 1 $self->_fail_jshandle($shandle, "job_created");
495 1 return 1;
496 }
497  
498 0 $task->handle("$ipport//$shandle");
499 0 ▶ return 1 if $task->{background};
Branch: 1 branch
BranchTrueFalseTotal
if $task->{"background"} 0 0 0
500 0 ▶ push @{ $self->{waiting}{$shandle} ||= [] }, $task;
Condition0%0/2
$self->{"waiting"}{$shandle} ||= []
l!l
00
MC/DC0%0/1
$self->{"waiting"}{$shandle} ||= []
$self->{"waiting"}{$shandle}
Truth table0%0/2
A
A= $self->{"waiting"}{$shandle}
Aresult
11
00
501 0 return 1;
502 },
503 3 ▶ work_complete => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
504 3 my ($blob) = shift;
505 3 ▶ ($blob =~ /^$qr/)
Branch: 1 branch
BranchTrueFalseTotal
$blob =~ /^$qr/ or Carp::croak("Bogus work_complete from server") 2 1 3
506 or Carp::croak "Bogus work_complete from server";
507 2 $blob =~ s/^$qr//;
508 2 my $shandle = $1;
509  
510 2 my $task_list = $self->{waiting}{$shandle};
511 2 my $task = shift @{$task_list};
512 2 $assert{task}->(
513 $task,
514 "task_list is empty on work_complete for handle $shandle"
515 );
516  
517 1 $task->complete(\$blob);
518 1 ▶ delete $self->{waiting}{$shandle} unless @{$task_list};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$task_list;} 1 0 1
519  
520 1 return 1;
521 },
522 3 ▶ work_data => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
523 3 my ($blob) = shift;
524 3 ▶ $blob =~ s/^(.+?)\0//
Branch: 1 branch
BranchTrueFalseTotal
$blob =~ s/^(.+?)\0// or Carp::croak("Bogus work_data from server") 2 1 3
525 or Carp::croak "Bogus work_data from server";
526 2 my $shandle = $1;
527  
528 2 my $task_list = $self->{waiting}{$shandle};
529 2 my $task = $task_list->[0];
530 2 $assert{task}->($task,
531 "task_list is empty on work_data for handle $shandle");
532  
533 1 $task->data(\$blob);
534  
535 1 return 1;
536 },
537 0 ▶ work_warning => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
538 0 my ($blob) = shift;
539 0 ▶ $blob =~ s/^(.+?)\0//
Branch: 1 branch
BranchTrueFalseTotal
$blob =~ s/^(.+?)\0// or Carp::croak("Bogus work_warning from server") 0 0 0
540 or Carp::croak "Bogus work_warning from server";
541 0 my $shandle = $1;
542  
543 0 my $task_list = $self->{waiting}{$shandle};
544 0 my $task = $task_list->[0];
545 0 $assert{task}->(
546 $task, "task_list is empty on work_warning for handle $shandle"
547 );
548  
549 0 $task->warning(\$blob);
550  
551 0 return 1;
552 },
553 2 ▶ work_exception => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
554 2 my ($blob) = shift;
555 2 ▶ ($blob =~ /^$qr/)
Branch: 1 branch
BranchTrueFalseTotal
$blob =~ /^$qr/ or Carp::croak("Bogus work_exception from server") 1 1 2
556 or Carp::croak "Bogus work_exception from server";
557 1 $blob =~ s/^$qr//;
558 1 my $shandle = $1;
559 1 my $task_list = $self->{waiting}{$shandle};
560 1 my $task = shift @{$task_list};
561 1 $assert{task}->(
562 $task,
563 "task_list is empty on work_exception for handle $shandle"
564 );
565  
566 #FIXME we have to freeze $blob because Task->exception expected it in this form.
567 # The only reason I see to do it so, is Worker->work implementation. With Gearman::Server it uses nfreeze for exception value.
568 1 $task->exception(\Storable::freeze(\$blob));
569  
570 1 ▶ delete $self->{waiting}{$shandle} unless @{$task_list};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$task_list;} 1 0 1
571  
572 1 return 1;
573 },
574 1 ▶ work_fail => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
575 1 $self->_fail_jshandle(shift, "work_fail");
576 1 return 1;
577 },
578 2 ▶ work_status => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
579 2 my ($blob) = shift;
580 2 my ($shandle, $nu, $de) = split(/\0/, $blob);
581 2 my $task_list = $self->{waiting}{$shandle};
582 2 ▶ ref($task_list) eq "ARRAY" && scalar(@{$task_list})
Branch: 1 branch
BranchTrueFalseTotal
ref $task_list eq "ARRAY" and scalar @{$task_list;} or Carp::croak("Got work_status for unknown handle: $shandle") 1 1 2
Condition50%1/2
ref $task_list eq "ARRAY" and scalar @{$task_list;}
!ll
02
MC/DC0%0/1
ref $task_list eq "ARRAY" and scalar @{$task_list;}
ref $task_list eq "ARRAY"
Truth table50%1/2
A
A= ref $task_list eq "ARRAY"
Aresult
00
11
583 or Carp::croak "Got work_status for unknown handle: $shandle";
584  
585 # FIXME: the server is (probably) sending a work_status packet for each
586 # interested client, even if the clients are the same, so probably need
587 # to fix the server not to do that. just put this FIXME here for now,
588 # though really it's a server issue.
589 1 foreach my $task (@{$task_list}) {
590 1 $task->status($nu, $de);
591 }
592  
593 1 return 1;
594 },
595 14 );
596  
597 defined($type{ $res->{type} })
598 14 ▶ || Carp::croak
Branch: 1 branch
BranchTrueFalseTotal
defined $type{$res->{"type"}} or Carp::croak(qq[Unimplemented packet type: $res->{"type"} [${$res->{"blobref"};}]]) 13 1 14
599 1 "Unimplemented packet type: $res->{type} [${$res->{blobref}}]";
600  
601 13 return $type{ $res->{type} }->(${ $res->{blobref} });
602 } ## end sub process_packet
603  
604 7 1;
« Util.pm ↑ Summary Client.pm »