/usr/share/perl5/Gearman/Task.pm is in libgearman-client-perl 2.004.012-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 | package Gearman::Task;
use version;
$Gearman::Task::VERSION = version->declare("2.004.012");
use strict;
use warnings;
=head1 NAME
Gearman::Task - a task in Gearman, from the point of view of a client
=head1 SYNOPSIS
my $task = Gearman::Task->new("add", "1+2", {
...
});
$taskset->add_task($task);
$client->do_task($task);
$client->dispatch_background($task);
=head1 DESCRIPTION
I<Gearman::Task> is a Gearman::Client's representation of a task to be
done.
=head1 USAGE
=head2 Gearman::Task->new($func, $arg, \%options)
Creates a new I<Gearman::Task> object, and returns the object.
I<$func> is the function name to be run. (that you have a worker registered to process)
I<$arg> is an opaque scalar or scalarref representing the argument(s)
to pass to the distributed function. If you want to pass multiple
arguments, you must encode them somehow into this one. That's up to
you and your worker.
I<%options> can contain:
=over 4
=item
uniq
A key which indicates to the server that other tasks with the same
function name and key will be merged into one. That is, the task
will be run just once, but all the listeners waiting on that job
will get the response multiplexed back to them.
Uniq may also contain the magic value "-" (a single hyphen) which
means the uniq key is the contents of the args.
=item
on_complete
A subroutine reference to be invoked when the task is completed. The
subroutine will be passed a reference to the return value from the worker
process.
=item
on_fail
A subroutine reference to be invoked when the task fails (or fails for
the last time, if retries were specified). The reason could be passed
to this callback as an argument. This callback won't be called after a
failure if more retries are still possible.
=item
on_retry
A subroutine reference to be invoked when the task fails, but is about
to be retried.
Is passed one argument, what retry attempt number this is. (starts with 1)
=item
on_status
A subroutine reference to be invoked if the task emits status updates.
Arguments passed to the subref are ($numerator, $denominator), where those
are left up to the client and job to determine.
=item
on_warning
A subroutine reference to be invoked if the task emits status updates.
Arguments passed to the subref are ($numerator, $denominator), where those
are left up to the client and job to determine.
=item
retry_count
Number of times job will be retried if there are failures. Defaults to 0.
=item
high_priority
B<the option high_priority is deprecated>. Use C<< priority => high >> instead.
Boolean, whether this job should take priority over other jobs already
enqueued.
=item
priority
valid value:
=over
=item
high
=item
normal (defaul)
=item
low
=back
=item
timeout
Automatically fail, calling your on_fail callback, after this many
seconds have elapsed without an on_fail or on_complete being
called. Defaults to 0, which means never. Bypasses any retry_count
remaining.
=item
try_timeout
Automatically fail, calling your on_retry callback (or on_fail if out of
retries), after this many seconds have elapsed. Defaults to 0, which means
never.
=back
=cut
use Carp ();
use Gearman::Util ();
use Scalar::Util ();
use String::CRC32 ();
use Storable ();
use fields (
# from client:
'func',
'argref',
# opts from client:
'uniq',
'on_complete',
'on_data',
'on_fail',
'on_exception',
'on_warning',
'on_retry',
'on_status',
'on_post_hooks',
# used internally,
# when other hooks are done running,
# prior to cleanup
'retry_count',
'timeout',
'try_timeout',
'high_priority',
'background',
# from server:
'handle',
# maintained by this module:
'retries_done',
'is_finished',
'taskset',
# jobserver socket.
# shared by other tasks in the same taskset,
# but not w/ tasks in other tasksets using
# the same Gearman::Client
'jssock',
# hookname -> coderef
'hooks',
'priority',
);
# constructor, given: ($func, $argref, $opts);
sub new {
my $self = shift;
unless (ref $self) {
$self = fields::new($self);
}
$self->{func} = shift
or Carp::croak("No function given");
$self->{argref} = shift || do { my $empty = ""; \$empty; };
(ref $self->{argref} eq "SCALAR")
|| Carp::croak("Argref not a scalar reference");
my $opts = shift || {};
$self->{$_} = delete $opts->{$_} for qw/
background
high_priority
on_complete
on_data
on_exception
on_fail
on_retry
on_status
on_warning
retry_count
timeout
try_timeout
uniq
/;
$self->_priority(delete $opts->{priority});
$self->{retry_count} ||= 0;
# bool: if success or fail has been called yet on this.
$self->{is_finished} = 0;
if (%{$opts}) {
Carp::croak("Unknown option(s): " . join(", ", sort keys %$opts));
}
$self->{retries_done} = 0;
return $self;
} ## end sub new
#=head1 METHODS
#
#=head2 run_hook($name)
#
#run a hook callback if defined
#
#=cut
sub run_hook {
my ($self, $name) = (shift, shift);
($name && $self->{hooks}->{$name}) || return;
eval { $self->{hooks}->{$name}->(@_) };
warn "Gearman::Task hook '$name' threw error: $@\n" if $@;
} ## end sub run_hook
#=head2 add_hook($name, $cb)
#
#add a hook
#
#=cut
sub add_hook {
my ($self, $name) = (shift, shift);
$name || return;
if (@_) {
$self->{hooks}->{$name} = shift;
}
else {
delete $self->{hooks}->{$name};
}
} ## end sub add_hook
#=head2 is_finished()
#
#B<return> bool: whether or not task is totally done (on_failure or
#on_complete callback has been called)
#
#=cut
sub is_finished {
return shift->{is_finished};
}
#=head2 taskset()
#
#getter
#
#=head2 taskset($ts)
#
#setter
#
#B<return> Gearman::Taskset
#
#=cut
sub taskset {
my $self = shift;
# getter
return $self->{taskset} unless @_;
# setter
my $ts = shift;
(Scalar::Util::blessed($ts) && $ts->isa("Gearman::Taskset"))
|| Carp::croak("argument is not an instance of Gearman::Taskset");
$self->{taskset} = $ts;
if (my $hash_num = $self->hash()) {
$self->{jssock} = $ts->_get_hashed_sock($hash_num);
}
else {
$self->{jssock} = $ts->_get_default_sock;
}
return $self->{taskset};
} ## end sub taskset
#=head2 hash()
#
#B<return> undef on non-uniq packet, or the hash value (0-32767) if uniq
#
#=cut
sub hash {
my $self = shift;
my $merge_on = $self->{uniq}
&& $self->{uniq} eq "-" ? $self->{argref} : \$self->{uniq};
if (${$merge_on}) {
return (String::CRC32::crc32(${$merge_on}) >> 16) & 0x7fff;
}
else {
return;
}
} ## end sub hash
#=head2 pack_submit_packet([$client])
#
#B<return> Gearman::Util::pack_req_command(mode, func, uniq, argref)
#
#=cut
sub pack_submit_packet {
my ($self, $client) = @_;
# $client should be optional for sake of Gearman::Client::Async
# see https://github.com/p-alik/perl-Gearman/issues/10
my $func = $client ? $client->func($self->func) : $self->func;
return Gearman::Util::pack_req_command(
$self->mode,
join(
"\0", $func || '', $self->{uniq} || '', ${ $self->{argref} } || ''
)
);
} ## end sub pack_submit_packet
#=head2 fail($reason)
#
#=cut
sub fail {
my ($self, $reason) = @_;
return if $self->{is_finished};
# try to retry, if we can
if ($self->{retries_done} < $self->{retry_count}) {
$self->{retries_done}++;
$self->{on_retry}->($self->{retries_done}) if $self->{on_retry};
$self->handle(undef);
return $self->{taskset}->add_task($self);
} ## end if ($self->{retries_done...})
$self->final_fail($reason);
} ## end sub fail
#=head2 final_fail($reason)
#
#if C<< !is_finished >> runs the hooks
#
#=over
#
#=item
#
#on_fail
#
#=item
#
#on_post_hooks
#
#=back
#
#=cut
sub final_fail {
my ($self, $reason) = @_;
return if $self->{is_finished};
$self->{is_finished} = $reason || 1;
$self->run_hook('final_fail', $self);
$self->{on_fail}->($reason) if $self->{on_fail};
$self->{on_post_hooks}->() if $self->{on_post_hooks};
$self->wipe;
return;
} ## end sub final_fail
#=head2 exception($exc_ref)
#
#$exc_ref may be a Storable serialized value
#
#run on_exception if defined
#
#=cut
sub exception {
my ($self, $exc_ref) = @_;
#FIXME the only on_exception callback get dereferenced value
# could it be changed without damage?
my $exception = Storable::thaw($$exc_ref);
$self->{on_exception}->($$exception) if $self->{on_exception};
return;
} ## end sub exception
#=head2 complete($result)
#
#C<$result> a reference profided to on_complete cb
#
#=cut
sub complete {
my ($self, $result_ref) = @_;
return if $self->{is_finished};
$self->{is_finished} = 'complete';
$self->run_hook('complete', $self);
$self->{on_complete}->($result_ref) if $self->{on_complete};
$self->{on_post_hooks}->() if $self->{on_post_hooks};
$self->wipe;
} ## end sub complete
#=head2 status()
#
#=cut
sub status {
my $self = shift;
return if $self->{is_finished};
return unless $self->{on_status};
my ($nu, $de) = @_;
$self->{on_status}->($nu, $de);
} ## end sub status
#=head2 data()
#
#invokes C<on_data> callback if worker sends work_data notification.
#
#=cut
sub data {
my $self = shift;
return if $self->{is_finished};
my $result_ref = shift;
$self->{on_data}->($result_ref) if $self->{on_data};
} ## end sub data
#=head2 warning($message)
#
#invokes C<on_warning> callback if worker sends work_warning notification.
#
#=cut
sub warning {
my $self = shift;
$self->{is_finished} && return;
$self->{on_warning} || return;
my $msg = shift;
$self->{on_warning}->($msg);
} ## end sub warning
#=head2 handle()
#
#getter
#
#=head2 handle($handle)
#
#setter for the fully-qualified handle of form "IP:port//shandle" where
#
#shandle is an opaque handle specific to the job server running on IP:port
#
#=cut
sub handle {
my $self = shift;
if (@_) {
$self->{handle} = shift;
}
return $self->{handle};
} ## end sub handle
# Gearman::Client::Async is the only consumer of set_on_post_hooks
sub set_on_post_hooks {
my ($self, $code) = @_;
$self->{on_post_hooks} = $code;
}
#=head2 wipe()
#
#cleanup
#
#=over
#
#=item
#
#on_post_hooks
#
#=item
#
#on_complete
#
#=item
#
#on_fail
#
#=item
#
#on_retry
#
#=item
#
#on_status
#
#=item
#
#hooks
#
#=back
#
#=cut
sub wipe {
my $self = shift;
my @h = qw/
on_post_hooks
on_complete
on_fail
on_retry
on_status
hooks
/;
foreach my $f (@h) {
$self->{$f} = undef;
}
} ## end sub wipe
#=head2 func()
#
#=cut
sub func {
my $self = shift;
return $self->{func};
}
#=head2 timeout()
#
#getter
#
#=head2 timeout($t)
#
#setter
#
#B<return> timeout
#
#=cut
sub timeout {
my $self = shift;
if (@_) {
$self->{timeout} = shift;
}
return $self->{timeout};
} ## end sub timeout
#=head2 mode()
#
#B<return> mode in depends of background and priority
#
#=cut
sub mode {
my $self = shift;
my $mode = "submit_job";
if ($self->_priority() ne "normal") {
$mode .= "_" . $self->_priority();
}
if ($self->{background}) {
$mode .= "_bg";
}
return $mode;
} ## end sub mode
#=head2 _priority($priority)
#
#set/get priority
#
#valid C<$priority> value
#
#=over
#
#=item
#
#high
#
#=item
#
#normal (default)
#
#=item
#
#low
#
#=back
#
#=cut
sub _priority {
my ($self, $priority) = @_;
if ($self->{high_priority}) {
warn <<'HERE';
Gearman::Task key high_priority is deprecated.
Use priority => "high" instead
HERE
$self->{priority} = "high";
delete($self->{high_priority});
} ## end if ($self->{high_priority...})
if ($priority) {
$priority =~ /^(high|normal|low)$/
|| Carp::croak "unsupported priority value";
$self->{priority} = $priority;
}
$self->{priority} ||= "normal";
return $self->{priority};
} ## end sub _priority
1;
__END__
|