Taskset.pm

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