| 1 | package EV::Gearman::Job; | ||
| 2 | 33 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 33 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 33 | our $VERSION = '0.03'; | |
| 6 | |||
| 7 | # A job is a blessed hashref { handle, function, unique, workload }, | ||
| 8 | # built by EV::Gearman XS when a JOB_ASSIGN[_UNIQ] arrives. The | ||
| 9 | # connection back-pointer is carried as perl magic (PERL_MAGIC_ext) on | ||
| 10 | # the hash -- not a key -- together with a tombstone reference that keeps | ||
| 11 | # the connection's control block allocated until every job referencing | ||
| 12 | # it is released. Job methods validate the struct's magic word, so a | ||
| 13 | # destroyed connection croaks instead of dereferencing freed memory, | ||
| 14 | # and a forged hash has no magic to pass the check. | ||
| 15 | |||
| 16 | 0 | ▶ | sub handle { $_[0]{handle} } |
|
Subroutine
handle: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 17 | 3 | ▶ | sub function { $_[0]{function} } |
|
Subroutine
function: called CC=1 SCAR=0.0
Pod
documented
| |||
| 18 | 0 | ▶ | sub unique { $_[0]{unique} } |
|
Subroutine
unique: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 19 | 8 | ▶ | sub workload { $_[0]{workload} } |
|
Subroutine
workload: called CC=1 SCAR=0.0
Pod
documented
| |||
| 20 | 3 | ▶ | sub data { $_[0]{workload} } # alias |
|
Subroutine
data: called CC=1 SCAR=0.0
Pod
documented
| |||
| 21 | |||
| 22 | 6 | ▶ | sub complete { $_[0]->_send_event(0, $_[1]) } |
|
Subroutine
complete: called CC=1 SCAR=0.0
Pod
documented
| |||
| 23 | 2 | ▶ | sub fail { $_[0]->_send_event(1) } |
|
Subroutine
fail: called CC=1 SCAR=0.0
Pod
documented
| |||
| 24 | 2 | ▶ | sub exception { $_[0]->_send_event(2, $_[1]) } |
|
Subroutine
exception: called CC=1 SCAR=0.0
Pod
documented
| |||
| 25 | 1 | ▶ | sub send_data { $_[0]->_send_event(3, $_[1]) } |
|
Subroutine
send_data: called CC=1 SCAR=0.0
Pod
documented
| |||
| 26 | 1 | ▶ | sub warning { $_[0]->_send_event(4, $_[1]) } |
|
Subroutine
warning: called CC=1 SCAR=0.0
Pod
documented
| |||
| 27 | |||
| 28 | 2 | ▶ | sub status { |
|
Subroutine
status: called CC=1 SCAR=0.0
Pod
documented
| |||
| 29 | 2 | my ($self, $num, $denom) = @_; | |
| 30 | 2 | $self->_send_status($num, $denom); | |
| 31 | } | ||
| 32 | |||
| 33 | 33 | 1; | |
| 34 | |||
| 35 | =encoding utf8 | ||
| 36 | |||
| 37 | =head1 NAME | ||
| 38 | |||
| 39 | EV::Gearman::Job - a job dispatched to a worker callback | ||
| 40 | |||
| 41 | =head1 SYNOPSIS | ||
| 42 | |||
| 43 | $g->register_function(slow_compute => { async => 1 }, sub { | ||
| 44 | my ($job) = @_; | ||
| 45 | |||
| 46 | # introspect | ||
| 47 | my $h = $job->handle; # 'H:host:42' | ||
| 48 | my $f = $job->function; # 'slow_compute' | ||
| 49 | my $u = $job->unique; # set if grab_unique => 1 | ||
| 50 | my $w = $job->workload; # request bytes | ||
| 51 | |||
| 52 | # progress / partial-result events delivered to the client | ||
| 53 | $job->status(50, 100); | ||
| 54 | $job->send_data("partial chunk"); | ||
| 55 | $job->warning("non-fatal warning"); | ||
| 56 | $job->exception("rich error info"); # if exceptions option set | ||
| 57 | |||
| 58 | # terminal | ||
| 59 | $job->complete($result); # success (sends WORK_COMPLETE) | ||
| 60 | $job->fail; # failure (sends WORK_FAIL) | ||
| 61 | }); | ||
| 62 | |||
| 63 | =head1 DESCRIPTION | ||
| 64 | |||
| 65 | A job object is created by L<EV::Gearman> when a C<JOB_ASSIGN> / | ||
| 66 | C<JOB_ASSIGN_UNIQ> packet arrives, and passed as the sole argument | ||
| 67 | to the function callback registered with C<register_function>. | ||
| 68 | |||
| 69 | In B<sync> mode (default), you typically just C<return> a result | ||
| 70 | from your callback -- the worker translates that into | ||
| 71 | C<WORK_COMPLETE>. C<die> becomes C<WORK_FAIL>. The job methods | ||
| 72 | below are still available for sending intermediate events. | ||
| 73 | |||
| 74 | In B<async> mode, the callback returns immediately; you must | ||
| 75 | explicitly call C<complete>, C<fail>, or C<exception> later. The | ||
| 76 | job object can be stashed in a closure or any other long-lived | ||
| 77 | container -- it outlives the connection safely. | ||
| 78 | |||
| 79 | If the underlying L<EV::Gearman> connection has been destroyed by | ||
| 80 | the time you call a job method, the call C<croak>s with | ||
| 81 | C<"client destroyed">. If the client is alive but currently | ||
| 82 | disconnected (even with reconnect armed), the call C<croak>s with | ||
| 83 | C<"not connected">; after reconnect, it croaks C<"stale job">. | ||
| 84 | Gearmand forgets the job when the connection drops, so a packet | ||
| 85 | queued for the next session would only earn a | ||
| 86 | C<JOB_NOT_FOUND> error there. The job holds an internal tombstone | ||
| 87 | reference that keeps the connection's control block allocated | ||
| 88 | (but torn down) until every job referencing it is released, so | ||
| 89 | this check is sound -- it never reads freed memory. The | ||
| 90 | back-pointer is stored as perl magic, not a hash key: user code, | ||
| 91 | hash walkers, and serializers cannot see or clobber it, and a | ||
| 92 | job hash that did not come from a C<JOB_ASSIGN> croaks with | ||
| 93 | C<"stale job"> instead of dereferencing a forged pointer. Note | ||
| 94 | that job objects are not serializable; a L<Storable> round-trip | ||
| 95 | produces a job that croaks C<"stale job">. | ||
| 96 | |||
| 97 | =head1 ACCESSORS | ||
| 98 | |||
| 99 | =head2 handle | ||
| 100 | |||
| 101 | Server-assigned job handle (e.g. C<H:host:42>). | ||
| 102 | |||
| 103 | =head2 function | ||
| 104 | |||
| 105 | Function name as registered. | ||
| 106 | |||
| 107 | =head2 unique | ||
| 108 | |||
| 109 | Submitter-supplied unique key. Empty string if the worker did not | ||
| 110 | opt into C<grab_unique =E<gt> 1> (the server only sends the unique | ||
| 111 | key with C<JOB_ASSIGN_UNIQ>). | ||
| 112 | |||
| 113 | =head2 workload | ||
| 114 | |||
| 115 | The job payload bytes. | ||
| 116 | |||
| 117 | =head2 data | ||
| 118 | |||
| 119 | Alias for C<workload>. | ||
| 120 | |||
| 121 | =head1 EVENT METHODS | ||
| 122 | |||
| 123 | These methods send packets back to the job server; the foreground | ||
| 124 | client (if any) receives the corresponding C<WORK_*> events | ||
| 125 | demultiplexed by handle. | ||
| 126 | |||
| 127 | =head2 send_data($bytes) | ||
| 128 | |||
| 129 | Send a partial C<WORK_DATA> chunk. The client's C<on_data> fires. | ||
| 130 | |||
| 131 | =head2 warning($bytes) | ||
| 132 | |||
| 133 | Send C<WORK_WARNING>. The client's C<on_warning> fires. | ||
| 134 | |||
| 135 | =head2 status($numerator, $denominator) | ||
| 136 | |||
| 137 | Send progress as C<WORK_STATUS>. Both values are sent as strings, | ||
| 138 | so any printable form is accepted (C<"42">, C<"3.14">, ...). The | ||
| 139 | client's C<on_status> fires with the same two values. | ||
| 140 | |||
| 141 | =head1 TERMINAL METHODS | ||
| 142 | |||
| 143 | Exactly one of these should be called per job in async mode; in | ||
| 144 | sync mode the worker calls one for you based on your callback's | ||
| 145 | return value or thrown exception. Sending a second terminal | ||
| 146 | packet produces a C<JOB_NOT_FOUND> error from gearmand (which | ||
| 147 | arrives as a connection-level error), so don't follow | ||
| 148 | C<exception> with C<fail>. | ||
| 149 | |||
| 150 | =head2 complete([$result]) | ||
| 151 | |||
| 152 | Send C<WORK_COMPLETE>. C<$result> defaults to the empty string. | ||
| 153 | |||
| 154 | =head2 fail | ||
| 155 | |||
| 156 | Send C<WORK_FAIL>. | ||
| 157 | |||
| 158 | =head2 exception($bytes) | ||
| 159 | |||
| 160 | Send C<WORK_EXCEPTION>. Terminal at the server: gearmand | ||
| 161 | forwards the data to the foreground client and then marks the | ||
| 162 | job as failed, so do not also call C<fail>. Only delivered to | ||
| 163 | clients that requested the C<exceptions> option (either via the | ||
| 164 | constructor's C<exceptions =E<gt> 1> or via | ||
| 165 | C<< $cli->option('exceptions') >>); other clients see a plain | ||
| 166 | C<WORK_FAIL>. | ||
| 167 | |||
| 168 | =head1 SEE ALSO | ||
| 169 | |||
| 170 | L<EV::Gearman> | ||
| 171 | |||
| 172 | =cut | ||