| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||
| 3 | # | ||||||||||
| 4 | # (C) Paul Evans, 2014,2026 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package Protocol::Gearman::Worker 0.05; | ||||||||||
| 7 | |||||||||||
| 8 | 3 | ▶ | use v5.20; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 3 | ▶ | use feature qw( postderef signatures ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | ▶ | no warnings qw( experimental::postderef experimental::signatures ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 3 | ▶ | use base qw( Protocol::Gearman ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | =head1 NAME | ||||||||||
| 19 | |||||||||||
| 20 | C<Protocol::Gearman::Worker> - implement a Gearman worker | ||||||||||
| 21 | |||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||
| 23 | |||||||||||
| 24 | =for highlighter language=perl | ||||||||||
| 25 | |||||||||||
| 26 | A base class that implements a complete Gearman worker. This abstract class | ||||||||||
| 27 | still requires the implementation methods as documented in | ||||||||||
| 28 | L<Protocol::Gearman>, but otherwise provides a full set of behaviour useful to | ||||||||||
| 29 | Gearman workers. | ||||||||||
| 30 | |||||||||||
| 31 | As it is based on L<Future> it is suitable for both synchronous and | ||||||||||
| 32 | asynchronous use. When backed by an implementation capable of performing | ||||||||||
| 33 | asynchronously, this object fully supports asynchronous Gearman communication. | ||||||||||
| 34 | When backed by a synchronous implementation, it will still yield C<Future> | ||||||||||
| 35 | instances but the limitations of the synchronous implementation may limit how | ||||||||||
| 36 | much concurrency and asynchronous behaviour can be acheived. | ||||||||||
| 37 | |||||||||||
| 38 | A simple concrete implementation suitable for synchronous use can be found in | ||||||||||
| 39 | L<Net::Gearman::Worker>. | ||||||||||
| 40 | |||||||||||
| 41 | =cut | ||||||||||
| 42 | |||||||||||
| 43 | =head1 METHODS | ||||||||||
| 44 | |||||||||||
| 45 | =cut | ||||||||||
| 46 | |||||||||||
| 47 | =head2 can_do | ||||||||||
| 48 | |||||||||||
| 49 | $worker->can_do( $name, %opts ); | ||||||||||
| 50 | |||||||||||
| 51 | Informs the server that the worker can perform a function of the given name. | ||||||||||
| 52 | |||||||||||
| 53 | The following named options are recognised: | ||||||||||
| 54 | |||||||||||
| 55 | =over 8 | ||||||||||
| 56 | |||||||||||
| 57 | =item timeout => INT | ||||||||||
| 58 | |||||||||||
| 59 | If specified, the function is registered using the C<CAN_DO_TIMEOUT> variant, | ||||||||||
| 60 | which sets a timeout on the Gearman server after which the function ought to | ||||||||||
| 61 | have completed. The timeout is specified in seconds. | ||||||||||
| 62 | |||||||||||
| 63 | =back | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | 2 | ▶ | sub can_do ( $self, $name, %opts ) | ||||||||
|
Subroutine
can_do: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 68 | { | ||||||||||
| 69 | 2 | my $timeout = $opts{timeout}; | |||||||||
| 70 | |||||||||||
| 71 | 2 | ▶ | if( defined $timeout ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 1 | $self->send_packet( CAN_DO_TIMEOUT => $name, int $timeout ); | |||||||||
| 73 | } | ||||||||||
| 74 | else { | ||||||||||
| 75 | 1 | $self->send_packet( CAN_DO => $name ); | |||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | =head2 grab_job | ||||||||||
| 80 | |||||||||||
| 81 | $job = await $worker->grab_job; | ||||||||||
| 82 | |||||||||||
| 83 | Returns a future that will eventually yield another job assignment from the | ||||||||||
| 84 | server as an instance of a job object; see below. | ||||||||||
| 85 | |||||||||||
| 86 | =cut | ||||||||||
| 87 | |||||||||||
| 88 | 3 | ▶ | sub grab_job ( $self ) | ||||||||
|
Subroutine
grab_job: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 89 | { | ||||||||||
| 90 | 3 | my $state = $self->gearman_state; | |||||||||
| 91 | |||||||||||
| 92 | 3 | push $state->{gearman_assigns}->@*, my $f = $self->new_future; | |||||||||
| 93 | |||||||||||
| 94 | 3 | $self->send_packet( GRAB_JOB => ); | |||||||||
| 95 | |||||||||||
| 96 | 3 | return $f; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 3 | ▶ | sub on_JOB_ASSIGN ( $self, @args ) | ||||||||
|
Subroutine
on_JOB_ASSIGN: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 100 | { | ||||||||||
| 101 | 3 | my $state = $self->gearman_state; | |||||||||
| 102 | |||||||||||
| 103 | 3 | my $f = shift $state->{gearman_assigns}->@*; | |||||||||
| 104 | 3 | $f->done( Protocol::Gearman::Worker::Job->new( $self, @args ) ); | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | # Manage Gearman's slightly odd sleep/wakeup job request loop | ||||||||||
| 108 | |||||||||||
| 109 | 1 | ▶ | sub on_NO_JOB ( $self ) | ||||||||
|
Subroutine
on_NO_JOB: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 110 | { | ||||||||||
| 111 | 1 | $self->send_packet( PRE_SLEEP => ); | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 1 | ▶ | sub on_NOOP ( $self ) | ||||||||
|
Subroutine
on_NOOP: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||
| 115 | { | ||||||||||
| 116 | 1 | my $state = $self->gearman_state; | |||||||||
| 117 | |||||||||||
| 118 | 1 | ▶ | $self->send_packet( GRAB_JOB => ) if $state->{gearman_assigns}->@*; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | =head2 job_finished | ||||||||||
| 122 | |||||||||||
| 123 | $worker->job_finished( $job ); | ||||||||||
| 124 | |||||||||||
| 125 | Invoked by the C<complete> and C<fail> methods on a job object, after the | ||||||||||
| 126 | server has been informed of the final status of the job. By default this | ||||||||||
| 127 | method does nothing, but it is provided for subclasses to override, to be | ||||||||||
| 128 | informed when a job is finished. | ||||||||||
| 129 | |||||||||||
| 130 | =cut | ||||||||||
| 131 | |||||||||||
| 132 | sub job_finished ( $self, $ ) { } | ||||||||||
| 133 | |||||||||||
| 134 | package # hide from CPAN | ||||||||||
| 135 | Protocol::Gearman::Worker::Job; | ||||||||||
| 136 | |||||||||||
| 137 | =head1 JOB OBJECTS | ||||||||||
| 138 | |||||||||||
| 139 | Objects of this type are returned by the C<grab_job> method. They represent | ||||||||||
| 140 | individual job assignments from the server, and can be used to obtain details | ||||||||||
| 141 | of the work to perform, and report on its result. | ||||||||||
| 142 | |||||||||||
| 143 | =cut | ||||||||||
| 144 | |||||||||||
| 145 | 3 | ▶ | sub new ( $class, $worker, $handle, $func, $arg ) | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||
| 146 | { | ||||||||||
| 147 | 3 | return bless { | |||||||||
| 148 | worker => $worker, | ||||||||||
| 149 | handle => $handle, | ||||||||||
| 150 | func => $func, | ||||||||||
| 151 | arg => $arg, | ||||||||||
| 152 | }, $class; | ||||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | =head2 worker | ||||||||||
| 156 | |||||||||||
| 157 | $worker = $job->worker; | ||||||||||
| 158 | |||||||||||
| 159 | Returns the C<Protocol::Gearman::Worker> object the job was received by. | ||||||||||
| 160 | |||||||||||
| 161 | =head2 handle | ||||||||||
| 162 | |||||||||||
| 163 | $handle = $job->handle; | ||||||||||
| 164 | |||||||||||
| 165 | Returns the job handle assigned by the server. Most implementations should not | ||||||||||
| 166 | need to use this directly. | ||||||||||
| 167 | |||||||||||
| 168 | =head2 func | ||||||||||
| 169 | |||||||||||
| 170 | =head2 arg | ||||||||||
| 171 | |||||||||||
| 172 | $func = $job->func; | ||||||||||
| 173 | |||||||||||
| 174 | $arg = $job->arg; | ||||||||||
| 175 | |||||||||||
| 176 | The function name and opaque argument data bytes sent by the requesting | ||||||||||
| 177 | client. | ||||||||||
| 178 | |||||||||||
| 179 | =cut | ||||||||||
| 180 | |||||||||||
| 181 | 11 | ▶ | sub worker ( $self ) { $self->{worker} } | ||||||||
|
Subroutine
worker: called CC=1 SCAR=0.0
| |||||||||||
| 182 | 9 | ▶ | sub handle ( $self ) { $self->{handle} } | ||||||||
|
Subroutine
handle: called CC=1 SCAR=0.0
| |||||||||||
| 183 | 2 | ▶ | sub func ( $self ) { $self->{func} } | ||||||||
|
Subroutine
func: called CC=1 SCAR=0.0
| |||||||||||
| 184 | 2 | ▶ | sub arg ( $self ) { $self->{arg} } | ||||||||
|
Subroutine
arg: called CC=1 SCAR=0.0
| |||||||||||
| 185 | |||||||||||
| 186 | =head2 data | ||||||||||
| 187 | |||||||||||
| 188 | $job->data( $data ); | ||||||||||
| 189 | |||||||||||
| 190 | Sends more data back to the client. Intended for long-running jobs with | ||||||||||
| 191 | incremental output. | ||||||||||
| 192 | |||||||||||
| 193 | =cut | ||||||||||
| 194 | |||||||||||
| 195 | 1 | ▶ | sub data ( $self, $data ) | ||||||||
|
Subroutine
data: called CC=1 SCAR=0.0
| |||||||||||
| 196 | { | ||||||||||
| 197 | 1 | $self->worker->send_packet( WORK_DATA => $self->handle, $data ); | |||||||||
| 198 | } | ||||||||||
| 199 | |||||||||||
| 200 | =head2 warning | ||||||||||
| 201 | |||||||||||
| 202 | $job->warning( $warning ); | ||||||||||
| 203 | |||||||||||
| 204 | Sends a warning to the client. | ||||||||||
| 205 | |||||||||||
| 206 | =cut | ||||||||||
| 207 | |||||||||||
| 208 | 1 | ▶ | sub warning ( $self, $warning ) | ||||||||
|
Subroutine
warning: called CC=1 SCAR=0.0
| |||||||||||
| 209 | { | ||||||||||
| 210 | 1 | $self->worker->send_packet( WORK_WARNING => $self->handle, $warning ); | |||||||||
| 211 | } | ||||||||||
| 212 | |||||||||||
| 213 | =head2 status | ||||||||||
| 214 | |||||||||||
| 215 | $job->status( $numerator, $denominator ); | ||||||||||
| 216 | |||||||||||
| 217 | Sets the current progress of the job. | ||||||||||
| 218 | |||||||||||
| 219 | =cut | ||||||||||
| 220 | |||||||||||
| 221 | 2 | ▶ | sub status ( $self, $num, $denom ) | ||||||||
|
Subroutine
status: called CC=1 SCAR=0.0
| |||||||||||
| 222 | { | ||||||||||
| 223 | 2 | $self->worker->send_packet( WORK_STATUS => $self->handle, $num, $denom ); | |||||||||
| 224 | } | ||||||||||
| 225 | |||||||||||
| 226 | =head2 complete | ||||||||||
| 227 | |||||||||||
| 228 | $job->complete( $result ); | ||||||||||
| 229 | |||||||||||
| 230 | Informs the server that the job is now complete, and sets its result. | ||||||||||
| 231 | |||||||||||
| 232 | =cut | ||||||||||
| 233 | |||||||||||
| 234 | 2 | ▶ | sub complete ( $self, $result ) | ||||||||
|
Subroutine
complete: called CC=1 SCAR=0.0
| |||||||||||
| 235 | { | ||||||||||
| 236 | 2 | $self->worker->send_packet( WORK_COMPLETE => $self->handle, $result ); | |||||||||
| 237 | 2 | $self->worker->job_finished( $self ); | |||||||||
| 238 | } | ||||||||||
| 239 | |||||||||||
| 240 | =head2 fail | ||||||||||
| 241 | |||||||||||
| 242 | $job->fail( $exception ); | ||||||||||
| 243 | |||||||||||
| 244 | Informs the server that the job has failed. | ||||||||||
| 245 | |||||||||||
| 246 | Optionally an exception value can be supplied; if given this will be sent to | ||||||||||
| 247 | the server using a C<WORK_EXCEPTION> message. Note that not all clients will | ||||||||||
| 248 | receive this; it is an optional feature. | ||||||||||
| 249 | |||||||||||
| 250 | =cut | ||||||||||
| 251 | |||||||||||
| 252 | 1 | ▶ | sub fail ( $self, $exception ) | ||||||||
|
Subroutine
fail: called CC=2 SCAR=7.0
| |||||||||||
| 253 | { | ||||||||||
| 254 | 1 | ▶ | if( defined $exception ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 255 | 1 | $self->worker->send_packet( WORK_EXCEPTION => $self->handle, $exception ); | |||||||||
| 256 | } | ||||||||||
| 257 | |||||||||||
| 258 | 1 | $self->worker->send_packet( WORK_FAIL => $self->handle ); | |||||||||
| 259 | 1 | $self->worker->job_finished( $self ); | |||||||||
| 260 | } | ||||||||||
| 261 | |||||||||||
| 262 | =head1 AUTHOR | ||||||||||
| 263 | |||||||||||
| 264 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 265 | |||||||||||
| 266 | =cut | ||||||||||
| 267 | |||||||||||
| 268 | 3 | 0x55AA; | |||||||||