Protocol/Gearman/Client.pm

filter: Statementstmt 100% 70 / 70 Branchbran 66.6% 12 / 18 Conditioncond 58.3% 7 / 12 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 18 / 18 Podpod 20.0% 2 / 10 totaltotal 82.0% 110 / 134 CC 15 SCAR 14.6
CC
15
Cov
89%
submit_job
10.5
on_WORK_DATA
2.0
on_WORK_WARNING
2.0
CRAP 4.3
↑ Summary Protocol/Gearman.pm »
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::Client 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 3 ▶use Struct::Dumb;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 struct Job => [qw( f on_data on_warning on_status exception )];
21  
22 =head1 NAME
23  
24 C<Protocol::Gearman::Client> - implement a Gearman client
25  
26 =head1 DESCRIPTION
27  
28 =for highlighter language=perl
29  
30 A base class that implements a complete Gearman client. This abstract class
31 still requires the implementation methods as documented in
32 L<Protocol::Gearman>, but otherwise provides a full set of behaviour useful to
33 Gearman clients.
34  
35 As it is based on L<Future> it is suitable for both synchronous and
36 asynchronous use. When backed by an implementation capable of performing
37 asynchronously, this object fully supports asynchronous Gearman communication.
38 When backed by a synchronous implementation, it will still yield C<Future>
39 instances but the limitations of the synchronous implementation may limit how
40 much concurrency and asynchronous behaviour can be acheived.
41  
42 A simple concrete implementation suitable for synchronous use can be found in
43 L<Net::Gearman::Client>.
44  
45 =cut
46  
47 =head1 METHODS
48  
49 =cut
50  
51 =head2 option_request
52  
53 await $client->option_request( $option );
54  
55 Requests that the Gearman server enable the named option for this connection.
56  
57 The following options are defined by Gearman:
58  
59 =over 8
60  
61 =item * C<exceptions>
62  
63 Enables the use of the C<WORK_EXCEPTION> packet; meaning the client is happy
64 to receive them.
65  
66 =back
67  
68 =cut
69  
70 1 ▶sub option_request ( $self, $option )
Subroutine option_request: called CC=1 SCAR=0.0
Pod documented
71 {
72 1 my $state = $self->gearman_state;
73  
74 1 my $request_f = $self->new_future;
75 1 $state->{gearman_option_reqs}{$option} = $request_f;
76  
77 1 $self->send_packet( OPTION_REQ => $option );
78  
79 1 return $request_f;
80 }
81  
82 1 ▶sub on_OPTION_RES ( $self, $option )
Subroutine on_OPTION_RES: called CC=1 SCAR=0.0
Pod undocumented
83 {
84 1 my $state = $self->gearman_state;
85 1 ( delete $state->{gearman_option_reqs}{$option} )->done();
86 }
87  
88 =head2 submit_job
89  
90 $result = await $client->submit_job( %args );
91  
92 Submits a job request to the Gearman server, and returns a future that will
93 eventually yield the result of the job or its failure.
94  
95 Takes the following required arguments:
96  
97 =over 8
98  
99 =item func => STRING
100  
101 The name of the function to call
102  
103 =item arg => STRING
104  
105 An opaque bytestring containing the argument data for the function. Its exact
106 format should be specified by the registered function.
107  
108 =back
109  
110 Takes the following optional arguments;
111  
112 =over 8
113  
114 =item background => BOOL
115  
116 If true, the job is submitted as a background request. Such a request will not
117 yield any status or completion information from the server. Once submitted the
118 server will not communicate about it further. In this case, the returned
119 future will complete with an empty result as soon as the job is accepted by
120 the server.
121  
122 =item priority => "high" | "low" | ""
123  
124 Alters the job priority on the server. If present, must be either C<"high">,
125 C<"low"> or the empty string.
126  
127 =item on_data => CODE
128  
129 Invoked on receipt of more incremental data from the worker.
130  
131 $on_data->( $data )
132  
133 =item on_warning => CODE
134  
135 Invoked on receipt of a warning from the worker.
136  
137 $on_warning->( $warning )
138  
139 =item on_status => CODE
140  
141 Invoked on a status update from the worker.
142  
143 $on_status->( $numerator, $denominator )
144  
145 =back
146  
147 =cut
148  
149 5 ▶sub submit_job ( $self, %args )
Subroutine submit_job: called CC=10 SCAR=23.5
Pod documented
150 {
151 5 ▶ my $func = $args{func} // croak "Required 'func' is missing in submit_job";
Condition50%1/2
$args{"func"} // croak("Required 'func' is missing in submit_job")
l!l
50
MC/DC0%0/1
$args{"func"} // croak("Required 'func' is missing in submit_job")
$args{"func"}
Truth table50%1/2
A
A= $args{"func"}
Aresult
11
00
152 5 ▶ my $arg = $args{arg} // croak "Required 'arg' is missing in submit_job";
Condition50%1/2
$args{"arg"} // croak("Required 'arg' is missing in submit_job")
l!l
50
MC/DC0%0/1
$args{"arg"} // croak("Required 'arg' is missing in submit_job")
$args{"arg"}
Truth table50%1/2
A
A= $args{"arg"}
Aresult
11
00
153  
154 5 my $bg = !!$args{background};
155  
156 5 ▶ my $prio = $args{priority} // "";
Condition100%2/2
$args{"priority"} // ""
l!l
14
MC/DC100%1/1
$args{"priority"} // ""
$args{"priority"}
Truth table100%2/2
A
A= $args{"priority"}
Aresult
11
00
157 5 ▶ $prio eq "" or $prio eq "high" or $prio eq "low" or
Branch: 1 branch
BranchTrueFalseTotal
$prio eq "" or $prio eq "high" or $prio eq "low" or croak("Unrecognised 'priority' of '${prio}'") 5 0 5
Condition50%3/6
$prio eq "" or $prio eq "high" or $prio eq "low"
l!l&&r!l&&!r
500
$prio eq "" or $prio eq "high"
l!l&&r!l&&!r
410
MC/DC0%0/3
$prio eq "" or $prio eq "high" or $prio eq "low"
$prio eq "" $prio eq "high" $prio eq "low"
Truth table50%2/4
A or B or C
A= $prio eq ""B= $prio eq "high"C= $prio eq "low"
ABCresult
1XX1
01X1
0011
0000
158 croak "Unrecognised 'priority' of '$prio'";
159  
160 5 my $state = $self->gearman_state;
161  
162 5 my $submit_f = $self->new_future;
163 5 push $state->{gearman_submits}->@*, $submit_f;
164  
165 5 my $f = $self->new_future;
166 5 ▶ $submit_f->on_done( sub ( $job_handle ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
167 5 ▶ if( $bg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bg) { } 1 4 5
168 1 $f->done;
169 }
170 else {
171 $state->{gearman_job}{$job_handle} = Job(
172 $f, $args{on_data}, $args{on_warning}, $args{on_status}, undef
173 4 );
174 }
175 5 });
176  
177 5 my $type = "SUBMIT_JOB";
178  
179 5 ▶ $type .= "_LOW" if $prio eq "low";
Branch: 1 branch
BranchTrueFalseTotal
if $prio eq "low" 0 5 5
180 5 ▶ $type .= "_HIGH" if $prio eq "high";
Branch: 1 branch
BranchTrueFalseTotal
if $prio eq "high" 1 4 5
181  
182 5 ▶ $type .= "_BG" if $bg;
Branch: 1 branch
BranchTrueFalseTotal
if $bg 1 4 5
183  
184 5 $self->send_packet( $type => $func, $state->{gearman_next_id}++, $arg );
185  
186 5 return $f;
187 }
188  
189 5 ▶sub on_JOB_CREATED ( $self, $job_handle )
Subroutine on_JOB_CREATED: called CC=1 SCAR=0.0
Pod undocumented
190 {
191 5 my $state = $self->gearman_state;
192  
193 5 my $f = shift $state->{gearman_submits}->@*;
194 5 $f->done( $job_handle );
195 }
196  
197 1 ▶sub on_WORK_DATA ( $self, $job_handle, $data )
Subroutine on_WORK_DATA: called CC=2 SCAR=7.1
Pod undocumented
198 {
199 1 my $state = $self->gearman_state;
200  
201 1 my $job = $state->{gearman_job}{$job_handle};
202  
203 1 ▶ $job->on_data->( $data ) if $job->on_data;
Branch: 1 branch
BranchTrueFalseTotal
if $job->on_data 1 0 1
204 }
205  
206 1 ▶sub on_WORK_WARNING ( $self, $job_handle, $warning )
Subroutine on_WORK_WARNING: called CC=2 SCAR=7.1
Pod undocumented
207 {
208 1 my $state = $self->gearman_state;
209  
210 1 my $job = $state->{gearman_job}{$job_handle};
211  
212 1 ▶ $job->on_warning->( $warning ) if $job->on_warning;
Branch: 1 branch
BranchTrueFalseTotal
if $job->on_warning 1 0 1
213 }
214  
215 2 ▶sub on_WORK_STATUS ( $self, $job_handle, $num, $denom )
Subroutine on_WORK_STATUS: called CC=2 SCAR=7.1
Pod undocumented
216 {
217 2 my $state = $self->gearman_state;
218  
219 2 my $job = $state->{gearman_job}{$job_handle};
220  
221 2 ▶ $job->on_status->( $num, $denom ) if $job->on_status;
Branch: 1 branch
BranchTrueFalseTotal
if $job->on_status 2 0 2
222 }
223  
224 3 ▶sub on_WORK_COMPLETE ( $self, $job_handle, $result )
Subroutine on_WORK_COMPLETE: called CC=1 SCAR=0.0
Pod undocumented
225 {
226 3 my $state = $self->gearman_state;
227  
228 3 my $job = delete $state->{gearman_job}{$job_handle};
229  
230 3 $job->f->done( $result );
231 }
232  
233 1 ▶sub on_WORK_EXCEPTION ( $self, $job_handle, $exception )
Subroutine on_WORK_EXCEPTION: called CC=1 SCAR=0.0
Pod undocumented
234 {
235 1 my $state = $self->gearman_state;
236  
237 1 my $job = $state->{gearman_job}{$job_handle};
238 1 $job->exception = $exception;
239 }
240  
241 1 ▶sub on_WORK_FAIL ( $self, $job_handle )
Subroutine on_WORK_FAIL: called CC=2 SCAR=7.0
Pod undocumented
242 {
243 1 my $state = $self->gearman_state;
244  
245 1 my $job = delete $state->{gearman_job}{$job_handle};
246  
247 1 my $exception = $job->exception;
248 1 ▶ $job->f->fail( "Work failed", gearman => ( defined $exception ? ( $exception ) : () ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $exception ? : 1 0 1
249 }
250  
251 =head1 AUTHOR
252  
253 Paul Evans <leonerd@leonerd.org.uk>
254  
255 =cut
256  
257 3 0x55AA;
↑ Summary Protocol/Gearman.pm »