blib/lib/EV/Gearman/Job.pm

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