Server.pm

filter: Statementstmt 15.8% 35 / 221 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 32.3% 11 / 34 Podpod 15.0% 3 / 20 totaltotal 12.2% 49 / 401 CC 55 SCAR 35.8
CC
55
Cov
11%
new
90.0
start_worker
72.0
_wake_up_some
56.0
CRAP 35.9
« Server/Client.pm ↑ Summary Server/Listener.pm »
1 package Gearman::Server;
2 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Gearman::Server::VERSION = qv("v1.130.1");
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Gearman::Server - function call "router" and load balancer
11  
12 =head1 DESCRIPTION
13  
14 You run a Gearman server (or more likely, many of them for both
15 high-availability and load balancing), then have workers (using
16 L<Gearman::Worker> from the Gearman module, or libraries for other
17 languages) register their ability to do certain functions to all of
18 them, and then clients (using L<Gearman::Client>,
19 L<Gearman::Client::Async>, etc) request work to be done from one of
20 the Gearman servers.
21  
22 The servers connect them, routing function call requests to the
23 appropriate workers, multiplexing responses to duplicate requests as
24 requested, etc.
25  
26 More than likely, you want to use the provided L<bin/gearmand> wrapper
27 script, and not use Gearman::Server directly.
28  
29 =cut
30  
31 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Gearman::Server::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Gearman::Server::Listener;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use Gearman::Server::Job;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use Socket qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
37 IPPROTO_TCP
38 SOL_SOCKET
39 SOCK_STREAM
40 AF_UNIX
41 SOCK_STREAM
42 PF_UNSPEC
43 1 /;
44 1 ▶use Sys::Hostname ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 1 ▶use fields (
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 'client_map', # fd -> Client
48 'sleepers', # func -> { "Client=HASH(0xdeadbeef)" => Client }
49 'sleepers_list', # func -> [ Client, ... ], ...
50 'job_queue', # job_name -> [Job, Job*] (key only exists if non-empty)
51 'job_of_handle', # handle -> Job
52 'max_queue', # func -> configured max jobqueue size
53 'job_of_uniq', # func -> uniq -> Job
54 'handle_ct', # atomic counter
55 'handle_base', # atomic counter
56 'listeners', # arrayref of listener objects
57 'wakeup', # number of workers to wake
58 'wakeup_delay', # seconds to wait before waking more workers
59 'wakeup_timers', # func -> timer, timer to be canceled or adjusted
60 # when job grab/inject is called
61 1 );
62  
63 =head1 METHODS
64  
65 =head2 new
66  
67 $server_object = Gearman::Server->new( %options )
68  
69 Creates and returns a new Gearman::Server object, which attaches itself to the Danga::Socket event loop. The server will begin operating when the Danga::Socket runloop is started. This means you need to start up the runloop before anything will happen.
70  
71 Options:
72  
73 =over
74  
75 =item port
76  
77 Specify a port which you would like the Gearman::Server to listen on for TCP connections (not necessary, but useful)
78  
79 =back
80  
81 =cut
82  
83 0 ▶sub new {
Subroutine new: not called CC=9 SCAR=45.0
Pod documented
84 0 my ($class, %opts) = @_;
85 0 ▶ my $self = ref $class ? $class : fields::new($class);
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 0 0
86  
87 0 $self->{client_map} = {};
88 0 $self->{sleepers} = {};
89 0 $self->{sleepers_list} = {};
90 0 $self->{job_queue} = {};
91 0 $self->{job_of_handle} = {};
92 0 $self->{max_queue} = {};
93 0 $self->{job_of_uniq} = {};
94 0 $self->{listeners} = [];
95 0 $self->{wakeup} = 3;
96 0 $self->{wakeup_delay} = .1;
97 0 $self->{wakeup_timers} = {};
98  
99 0 $self->{handle_ct} = 0;
100 0 $self->{handle_base} = "H:" . Sys::Hostname::hostname() . ":";
101  
102 0 my $port = delete $opts{port};
103  
104 0 my $wakeup = delete $opts{wakeup};
105  
106 0 ▶ if (defined $wakeup) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wakeup) 0 0 0
107 0 ▶ die "Invalid value passed in wakeup option"
Branch: 1 branch
BranchTrueFalseTotal
if $wakeup < 0 and $wakeup != -1 0 0 0
Condition0%0/3
$wakeup < 0 and $wakeup != -1
!ll&&!rl&&r
000
MC/DC0%0/2
$wakeup < 0 and $wakeup != -1
$wakeup < 0 $wakeup != -1
Truth table0%0/3
A and B
A= $wakeup < 0B= $wakeup != -1
ABresult
0X0
100
111
108 if $wakeup < 0 && $wakeup != -1;
109 0 $self->{wakeup} = $wakeup;
110 }
111  
112 0 my $wakeup_delay = delete $opts{wakeup_delay};
113  
114 0 ▶ if (defined $wakeup_delay) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wakeup_delay) 0 0 0
115 0 ▶ die "Invalid value passed in wakeup_delay option"
Branch: 1 branch
BranchTrueFalseTotal
if $wakeup_delay < 0 and $wakeup_delay != -1 0 0 0
Condition0%0/3
$wakeup_delay < 0 and $wakeup_delay != -1
!ll&&!rl&&r
000
MC/DC0%0/2
$wakeup_delay < 0 and $wakeup_delay != -1
$wakeup_delay < 0 $wakeup_delay != -1
Truth table0%0/3
A and B
A= $wakeup_delay < 0B= $wakeup_delay != -1
ABresult
0X0
100
111
116 if $wakeup_delay < 0 && $wakeup_delay != -1;
117 0 $self->{wakeup_delay} = $wakeup_delay;
118 }
119  
120 0 ▶ croak("Unknown options") if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
121 0 $self->create_listening_sock($port);
122  
123 0 return $self;
124 } ## end sub new
125  
126 0 ▶sub debug {
Subroutine debug: not called CC=1 SCAR=6.9
Pod undocumented
127 0 my ($self, $msg) = @_;
128  
129 #warn "$msg\n";
130 }
131  
132 =head2 create_listening_sock
133  
134 $server_object->create_listening_sock( $portnum, \%options )
135  
136 Add a TCP port listener for incoming Gearman worker and client connections. Options:
137  
138 =over 4
139  
140 =item accept_per_loop
141  
142 =item local_addr
143  
144 Bind socket to only this address.
145  
146 =back
147  
148 =cut
149  
150 0 ▶sub create_listening_sock {
Subroutine create_listening_sock: not called CC=4 SCAR=30.0
Pod documented
151 0 my ($self, $portnum, %opts) = @_;
152  
153 0 my $accept_per_loop = delete $opts{accept_per_loop};
154 0 my $local_addr = delete $opts{local_addr};
155  
156 0 ▶ warn "Extra options passed into create_listening_sock: "
Branch: 1 branch
BranchTrueFalseTotal
if keys %opts 0 0 0
157 . join(', ', keys %opts) . "\n"
158 if keys %opts;
159  
160 0 ▶ my $ssock = IO::Socket::INET->new(
Branch: 2 branches
BranchTrueFalseTotal
my $ssock = "IO::Socket::INET"->new("LocalPort", $portnum, "Type", 1, "Proto", 6, "Blocking", 0, "Reuse", 1, "Listen", 1024, $local_addr ? ("LocalAddr", $local_addr) : ()) or die "Error creating socket: $@\n" 0 0 0
$local_addr ? : 0 0 0
161 LocalPort => $portnum,
162 Type => SOCK_STREAM,
163 Proto => IPPROTO_TCP,
164 Blocking => 0,
165 Reuse => 1,
166 Listen => 1024,
167 ($local_addr ? (LocalAddr => $local_addr) : ())
168 ) or die "Error creating socket: $@\n";
169  
170 0 my $listeners = $self->{listeners};
171 0 push @$listeners,
172 Gearman::Server::Listener->new($ssock, $self,
173 accept_per_loop => $accept_per_loop);
174  
175 0 return $ssock;
176 } ## end sub create_listening_sock
177  
178 0 ▶sub new_client {
Subroutine new_client: not called CC=1 SCAR=6.9
Pod undocumented
179 0 my ($self, $sock) = @_;
180 0 my $client = Gearman::Server::Client->new($sock, $self);
181 0 $client->watch_read(1);
182 0 $self->{client_map}{ $client->{fd} } = $client;
183 } ## end sub new_client
184  
185 0 ▶sub note_disconnected_client {
Subroutine note_disconnected_client: not called CC=1 SCAR=6.9
Pod undocumented
186 0 my ($self, $client) = @_;
187 0 delete $self->{client_map}{ $client->{fd} };
188 }
189  
190 0 ▶sub clients {
Subroutine clients: not called CC=1 SCAR=6.9
Pod undocumented
191 0 my $self = shift;
192 0 return values %{ $self->{client_map} };
193 }
194  
195 # Returns a socket that is connected to the server, we can then use this
196 # socket with a Gearman::Client::Async object to run clients and servers in the
197 # same thread.
198 0 ▶sub to_inprocess_server {
Subroutine to_inprocess_server: not called CC=2 SCAR=17.9
Pod undocumented
199 0 my $self = shift;
200  
201 0 my ($psock, $csock);
202 0 ▶ socketpair($csock, $psock, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
Branch: 1 branch
BranchTrueFalseTotal
socketpair $csock, $psock, 1, 1, 0 or die "socketpair: $!" 0 0 0
203 or die "socketpair: $!";
204  
205 0 $csock->autoflush(1);
206 0 $psock->autoflush(1);
207  
208 0 IO::Handle::blocking($csock, 0);
209 0 IO::Handle::blocking($psock, 0);
210  
211 0 my $client = Gearman::Server::Client->new($csock, $self);
212  
213 0 my ($package, $file, $line) = caller;
214 0 $client->{peer_ip} = "[$package|$file|$line]";
215 0 $client->watch_read(1);
216 0 $self->{client_map}{ $client->{fd} } = $client;
217  
218 0 return $psock;
219 } ## end sub to_inprocess_server
220  
221 =head2 start_worker
222  
223 $pid = $server_object->start_worker( $prog )
224  
225 ($pid, $client) = $server_object->start_worker( $prog )
226  
227 Fork and start a worker process named by C<$prog> and returns the pid (or pid and client object).
228  
229 =cut
230  
231 0 ▶sub start_worker {
Subroutine start_worker: not called CC=8 SCAR=42.8
Pod documented
232 0 my ($self, $prog) = @_;
233  
234 0 my ($psock, $csock);
235 0 ▶ socketpair($csock, $psock, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
Branch: 1 branch
BranchTrueFalseTotal
socketpair $csock, $psock, 1, 1, 0 or die "socketpair: $!" 0 0 0
236 or die "socketpair: $!";
237  
238 0 $csock->autoflush(1);
239 0 $psock->autoflush(1);
240  
241 0 my $pid = fork;
242 0 ▶ unless (defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
243 0 warn "fork failed: $!\n";
244 0 return undef;
245 }
246  
247 # child process
248 0 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
249 0 local $ENV{'GEARMAN_WORKER_USE_STDIO'} = 1;
250 0 close(STDIN);
251 0 close(STDOUT);
252 0 ▶ open(STDIN, '<&', $psock)
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", $psock or die "Unable to dup socketpair to STDIN: $!" 0 0 0
253 or die "Unable to dup socketpair to STDIN: $!";
254 0 ▶ open(STDOUT, '>&', $psock)
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $psock or die "Unable to dup socketpair to STDOUT: $!" 0 0 0
255 or die "Unable to dup socketpair to STDOUT: $!";
256 0 ▶ if (UNIVERSAL::isa($prog, "CODE")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($prog, "CODE")) 0 0 0
257 0 $prog->();
258 0 exit 0; # shouldn't get here. subref should exec.
259 }
260 0 exec $prog;
261 0 die "Exec failed: $!";
262 } ## end unless ($pid)
263  
264 0 close($psock);
265  
266 0 IO::Handle::blocking($csock, 0);
267 0 my $sock = $csock;
268  
269 0 my $client = Gearman::Server::Client->new($sock, $self);
270  
271 0 $client->{peer_ip} = "[gearman_child]";
272 0 $client->watch_read(1);
273 0 $self->{client_map}{ $client->{fd} } = $client;
274 0 ▶ return wantarray ? ($pid, $client) : $pid;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
275 } ## end sub start_worker
276  
277 0 ▶sub enqueue_job {
Subroutine enqueue_job: not called CC=5 SCAR=34.0
Pod undocumented
278 0 my ($self, $job, $highpri) = @_;
279 0 ▶ my $jq = ($self->{job_queue}{ $job->{func} } ||= []);
Condition0%0/2
$self->{"job_queue"}{$job->{"func"}} ||= []
l!l
00
MC/DC0%0/1
$self->{"job_queue"}{$job->{"func"}} ||= []
$self->{"job_queue"}{$job->{"func"}}
Truth table0%0/2
A
A= $self->{"job_queue"}{$job->{"func"}}
Aresult
11
00
280  
281 0 ▶ if (defined(my $max_queue_size = $self->{max_queue}{ $job->{func} })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $max_queue_size = $self->{"max_queue"}{$job->{"func"}})) 0 0 0
282 0 $max_queue_size
283 --; # Subtract one, because we're about to add one more below.
284 0 ▶ while (@$jq > $max_queue_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$jq > $max_queue_size) 0 0 0
285 0 my $delete_job = pop @$jq;
286 0 my $msg = Gearman::Util::pack_res_command("work_fail",
287 $delete_job->handle);
288 0 $delete_job->relay_to_listeners($msg);
289 0 $delete_job->note_finished;
290 } ## end while (@$jq > $max_queue_size)
291 } ## end if (defined(my $max_queue_size...))
292  
293 0 ▶ if ($highpri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($highpri) { } 0 0 0
294 0 unshift @$jq, $job;
295 }
296 else {
297 0 push @$jq, $job;
298 }
299  
300 0 $self->{job_of_handle}{ $job->{'handle'} } = $job;
301 } ## end sub enqueue_job
302  
303 0 ▶sub wake_up_sleepers {
Subroutine wake_up_sleepers: not called CC=5 SCAR=34.0
Pod undocumented
304 0 my ($self, $func) = @_;
305  
306 0 ▶ if (my $existing_timer = delete($self->{wakeup_timers}->{$func})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $existing_timer = delete $self->{"wakeup_timers"}{$func}) 0 0 0
307 0 $existing_timer->cancel();
308 }
309  
310 0 ▶ return unless $self->_wake_up_some($func);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_wake_up_some($func) 0 0 0
311  
312 0 my $delay = $self->{wakeup_delay};
313  
314 # -1 means don't setup a timer. 0 actually means go as fast as we can, cooperatively.
315 0 ▶ return if $delay == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $delay == -1 0 0 0
316  
317 # If we're only going to wakeup 0 workers anyways, don't set up a timer.
318 0 ▶ return if $self->{wakeup} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"wakeup"} == 0 0 0 0
319  
320 my $timer = Danga::Socket->AddTimer(
321 $delay,
322 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
323 # Be sure to not wake up more sleepers if we have no jobs in the queue.
324 # I know the object definition above says I can trust the func element to determine
325 # if there are items in the list, but I'm just gonna be safe, rather than sorry.
326 0 ▶ return unless @{ $self->{job_queue}{$func} || [] };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"job_queue"}{$func} or [];} 0 0 0
Condition0%0/2
$self->{'job_queue'}{$func} || []
l!l
00
MC/DC0%0/1
$self->{'job_queue'}{$func} || []
$self->{'job_queue'}{$func}
Truth table0%0/2
A
A= $self->{'job_queue'}{$func}
Aresult
11
00
327 0 $self->wake_up_sleepers($func);
328 }
329 0 );
330 0 $self->{wakeup_timers}->{$func} = $timer;
331 } ## end sub wake_up_sleepers
332  
333 # Returns true when there are still more workers to wake up
334 # False if there are no sleepers
335 0 ▶sub _wake_up_some {
Subroutine _wake_up_some: not called CC=7 SCAR=40.3
336 0 my ($self, $func) = @_;
337 0 ▶ my $sleepmap = $self->{sleepers}{$func} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sleepmap = $self->{"sleepers"}{$func} or return 0 0 0
338 0 ▶ my $sleeporder = $self->{sleepers_list}{$func} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sleeporder = $self->{"sleepers_list"}{$func} or return 0 0 0
339  
340 # TODO SYNC UP STATE HERE IN CASE TWO LISTS END UP OUT OF SYNC
341  
342 0 my $max = $self->{wakeup};
343  
344 0 ▶ while (@$sleeporder) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$sleeporder) 0 0 0
345 0 my Gearman::Server::Client $c = shift @$sleeporder;
346 0 ▶ next if $c->{closed} || !$c->{sleeping};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"closed"} or not $c->{"sleeping"} 0 0 0
Condition0%0/3
$c->{"closed"} or not $c->{"sleeping"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{"closed"} or not $c->{"sleeping"}
$c->{"closed"} not $c->{"sleeping"}
Truth table0%0/3
A or not(B)
A= $c->{"closed"}B= not $c->{"sleeping"}
ABresult
1X1
011
000
347 0 ▶ if ($max-- <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max-- <= 0) 0 0 0
348 0 unshift @$sleeporder, $c;
349 0 return 1;
350 }
351 0 delete $sleepmap->{"$c"};
352 0 $c->res_packet("noop");
353 0 $c->{sleeping} = 0;
354 } ## end while (@$sleeporder)
355  
356 0 delete $self->{sleepers}{$func};
357 0 delete $self->{sleepers_list}{$func};
358 0 return;
359 } ## end sub _wake_up_some
360  
361 0 ▶sub on_client_sleep {
Subroutine on_client_sleep: not called CC=7 SCAR=40.3
Pod undocumented
362 0 my $self = shift;
363 0 my Gearman::Server::Client $cl = shift;
364  
365 0 foreach my $cd (@{ $cl->{can_do_list} }) {
366  
367 # immediately wake the sleeper up if there are things to be done
368 0 ▶ if ($self->{job_queue}{$cd}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"job_queue"}{$cd}) 0 0 0
369 0 $cl->res_packet("noop");
370 0 $cl->{sleeping} = 0;
371 0 return;
372 }
373  
374 0 ▶ my $sleepmap = ($self->{sleepers}{$cd} ||= {});
Condition0%0/2
$self->{"sleepers"}{$cd} ||= {}
l!l
00
MC/DC0%0/1
$self->{"sleepers"}{$cd} ||= {}
$self->{"sleepers"}{$cd}
Truth table0%0/2
A
A= $self->{"sleepers"}{$cd}
Aresult
11
00
375 0 my $count = $sleepmap->{"$cl"}++;
376  
377 0 ▶ next if $count >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $count >= 2 0 0 0
378  
379 0 ▶ my $sleeporder = ($self->{sleepers_list}{$cd} ||= []);
Condition0%0/2
$self->{"sleepers_list"}{$cd} ||= []
l!l
00
MC/DC0%0/1
$self->{"sleepers_list"}{$cd} ||= []
$self->{"sleepers_list"}{$cd}
Truth table0%0/2
A
A= $self->{"sleepers_list"}{$cd}
Aresult
11
00
380  
381 # The idea here is to keep workers at the head of the list if they are doing work, hopefully
382 # this will allow extra workers that aren't needed to actually go 'idle' safely.
383 0 my $jobs_done = $cl->{jobs_done_since_sleep};
384  
385 0 ▶ if ($jobs_done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jobs_done) { } 0 0 0
386 0 unshift @$sleeporder, $cl;
387 }
388 else {
389 0 push @$sleeporder, $cl;
390 }
391  
392 0 $cl->{jobs_done_since_sleep} = 0;
393  
394 } ## end foreach my $cd (@{ $cl->{can_do_list...}})
395 } ## end sub on_client_sleep
396  
397 0 ▶sub jobs_outstanding {
Subroutine jobs_outstanding: not called CC=1 SCAR=6.9
Pod undocumented
398 0 my Gearman::Server $self = shift;
399 0 return scalar keys %{ $self->{job_queue} };
400 }
401  
402 0 ▶sub jobs {
Subroutine jobs: not called CC=1 SCAR=6.9
Pod undocumented
403 0 my Gearman::Server $self = shift;
404 0 return values %{ $self->{job_of_handle} };
405 }
406  
407 0 ▶sub job_by_handle {
Subroutine job_by_handle: not called CC=1 SCAR=6.9
Pod undocumented
408 0 my ($self, $handle) = @_;
409 0 return $self->{job_of_handle}{$handle};
410 }
411  
412 0 ▶sub note_job_finished {
Subroutine note_job_finished: not called CC=3 SCAR=24.8
Pod undocumented
413 0 my Gearman::Server $self = shift;
414 0 my Gearman::Server::Job $job = shift;
415  
416 0 ▶ if (my Gearman::Server::Client $worker = $job->worker) {
Branch: 1 branch
BranchTrueFalseTotal
if (my Gearman::Server::Client $worker = $job->worker) 0 0 0
417 0 $worker->{jobs_done_since_sleep}++;
418 }
419  
420 0 ▶ if (length($job->{uniq})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $job->{"uniq"}) 0 0 0
421 0 delete $self->{job_of_uniq}{ $job->{func} }{ $job->{uniq} };
422 }
423 0 delete $self->{job_of_handle}{ $job->{handle} };
424 } ## end sub note_job_finished
425  
426 # <0/undef/"" to reset. else integer max depth.
427 0 ▶sub set_max_queue {
Subroutine set_max_queue: not called CC=4 SCAR=30.0
Pod undocumented
428 0 my ($self, $func, $max) = @_;
429 0 ▶ if (defined $max && length $max && $max >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max and length $max and $max >= 0) { } 0 0 0
Condition0%0/6
defined $max and length $max and $max >= 0
!ll&&!rl&&r
000
defined $max and length $max
!ll&&!rl&&r
000
MC/DC0%0/3
defined $max and length $max and $max >= 0
defined $max length $max $max >= 0
Truth table0%0/4
A and B and C
A= defined $maxB= length $maxC= $max >= 0
ABCresult
0XX0
10X0
1100
1111
430 0 $self->{max_queue}{$func} = int($max);
431 }
432 else {
433 0 delete $self->{max_queue}{$func};
434 }
435 } ## end sub set_max_queue
436  
437 0 ▶sub new_job_handle {
Subroutine new_job_handle: not called CC=1 SCAR=6.9
Pod undocumented
438 0 my $self = shift;
439 0 return $self->{handle_base} . (++$self->{handle_ct});
440 }
441  
442 0 ▶sub job_of_unique {
Subroutine job_of_unique: not called CC=2 SCAR=17.9
Pod undocumented
443 0 my ($self, $func, $uniq) = @_;
444 0 ▶ return undef unless $self->{job_of_uniq}{$func};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"job_of_uniq"}{$func} 0 0 0
445 0 return $self->{job_of_uniq}{$func}{$uniq};
446 }
447  
448 0 ▶sub set_unique_job {
Subroutine set_unique_job: not called CC=2 SCAR=17.9
Pod undocumented
449 0 my ($self, $func, $uniq, $job) = @_;
450 0 ▶ $self->{job_of_uniq}{$func} ||= {};
Condition0%0/2
$self->{"job_of_uniq"}{$func} ||= {}
l!l
00
MC/DC0%0/1
$self->{"job_of_uniq"}{$func} ||= {}
$self->{"job_of_uniq"}{$func}
Truth table0%0/2
A
A= $self->{"job_of_uniq"}{$func}
Aresult
11
00
451 0 $self->{job_of_uniq}{$func}{$uniq} = $job;
452 }
453  
454 0 ▶sub grab_job {
Subroutine grab_job: not called CC=7 SCAR=40.3
Pod undocumented
455 0 my ($self, $func) = @_;
456 0 ▶ return undef unless $self->{job_queue}{$func};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"job_queue"}{$func} 0 0 0
457  
458 0 ▶ my $empty = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
459 0 delete $self->{job_queue}{$func};
460 0 return undef;
461 0 };
462  
463 0 my Gearman::Server::Job $job;
464 0 while (1) {
465 0 $job = shift @{ $self->{job_queue}{$func} };
466 0 ▶ return $empty->() unless $job;
Branch: 1 branch
BranchTrueFalseTotal
unless $job 0 0 0
467 0 ▶ return $job unless $job->require_listener;
Branch: 1 branch
BranchTrueFalseTotal
unless $job->require_listener 0 0 0
468  
469 0 foreach my Gearman::Server::Client $c (@{ $job->{listeners} }) {
470 0 ▶ return $job if $c && !$c->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $c and not $c->{"closed"} 0 0 0
Condition0%0/3
$c and not $c->{"closed"}
!ll&&!rl&&r
000
MC/DC0%0/2
$c and not $c->{"closed"}
$c not $c->{"closed"}
Truth table0%0/3
A and not(B)
A= $cB= not $c->{"closed"}
ABresult
0X0
100
111
471 }
472 0 $job->note_finished(0);
473 } ## end while (1)
474 } ## end sub grab_job
475  
476 1 1;
477 __END__
« Server/Client.pm ↑ Summary Server/Listener.pm »