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