Gearman/Worker.pm

filter: Statementstmt 30.3% 10 / 33 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 25.0% 3 / 12 Podpod 75.0% 3 / 4 totaltotal 31.3% 16 / 51 CC 3 SCAR 10.7
CC
3
Cov
29%
__ANON__
6.0
job_finished
6.0
add_function
2.0
CRAP 2.9
↑ Summary 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 Net::Async::Gearman::Worker 0.02;
7  
8 1 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use base qw( Net::Async::Gearman Protocol::Gearman::Worker );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 Protocol::Gearman::Worker->VERSION( '0.03' ); # job_finished
13  
14 =head1 NAME
15  
16 C<Net::Async::Gearman::Worker> - concrete Gearman worker over an L<IO::Async::Stream>
17  
18 =head1 DESCRIPTION
19  
20 =for highlighter language=perl
21  
22 This module combines the abstract L<Protocol::Gearman::Worker> with
23 L<Net::Async::Gearman> to provide an asynchronous concrete Gearman worker
24 implementation.
25  
26 =cut
27  
28 =head1 METHODS
29  
30 =cut
31  
32 =head2 add_function
33  
34 $worker->add_function( $name, $code );
35  
36 Adds a new function to the collection known by the worker. On connection to
37 the server, it will declare the names of all of these by using the C<can_do>
38 method.
39  
40 The code itself will be invoked with a Job object, and is expected to return
41 a Future that will give the eventual result of the function. It is not
42 necessary to invoke the C<complete> or C<fail> methods on the Job; that will
43 be done automatically when the Future becomes ready.
44  
45 $f = $code->( $job )
46  
47 =cut
48  
49 sub add_function
50 0 ▶{
Subroutine add_function: not called CC=1 SCAR=6.9
Pod documented
51 0 my $self = shift;
52 0 my ( $name, $code ) = @_;
53  
54 0 $self->gearman_state->{gearman_funcs}{$name} = $code;
55 }
56  
57 sub connect
58 0 ▶{
Subroutine connect: not called CC=1 SCAR=6.9
Pod documented
59 0 my $self = shift;
60 $self->SUPER::connect( @_ )
61 0 ▶ ->on_done( sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
62 0 my $funcs = $self->gearman_state->{gearman_funcs};
63  
64 0 $self->can_do( $_ ) for keys %$funcs;
65  
66 0 $self->start_grab_job;
67 0 });
68 }
69  
70 sub job_finished
71 0 ▶{
Subroutine job_finished: not called CC=2 SCAR=17.9
Pod documented
72 0 my $self = shift;
73  
74 0 my $state = $self->gearman_state;
75  
76 0 ▶ $self->start_grab_job unless $self->{gearman_grabf};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"gearman_grabf"} 0 0 0
77 }
78  
79 sub start_grab_job
80 0 ▶{
Subroutine start_grab_job: not called CC=1 SCAR=6.9
Pod undocumented
81 0 my $self = shift;
82  
83 0 my $state = $self->gearman_state;
84  
85 $state->{gearman_grabf} = $self->grab_job
86 0 ▶ ->on_done( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 my ( $job ) = @_;
88  
89 0 undef $state->{gearman_grabf};
90  
91 0 my $code = $state->{gearman_funcs}{$job->func};
92 0 my $job_f = $code->( $job );
93  
94 0 my $handle = $job->handle;
95  
96 $state->{gearman_jobs}{$handle} = $job_f
97 0 ▶ ->on_done( sub { $job->complete( $_[0] ) } )
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 0 ▶ ->on_fail( sub { $job->fail() } )
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 ▶ ->on_ready( sub { delete $state->{gearman_jobs}{$handle} } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 });
101 }
102  
103 =head1 TODO
104  
105 =over 4
106  
107 =item *
108  
109 Consider how much of this code can or should be moved into
110 L<Protocol::Gearman::Worker> itself.
111  
112 =back
113  
114 =head1 AUTHOR
115  
116 Paul Evans <leonerd@leonerd.org.uk>
117  
118 =cut
119  
120 1 0x55AA;
↑ Summary Gearman.pm »