blib/lib/EV/Gearman/Job.pm

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