blib/lib/Net/Async/Gearman/Worker.pm

filter: Statementstmt 34.2% 12 / 35 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 25.0% 3 / 12 Podpod 75.0% 3 / 4 totaltotal 33.9% 18 / 53 CC 3 SCAR 10.7
CC
3
Cov
32%
__ANON__
6.0
job_finished
6.0
add_function
2.0
CRAP 2.9
« Build ↑ Summary blib/lib/Net/Async/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 -- leonerd@leonerd.org.uk
5  
6 package Net::Async::Gearman::Worker;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.01';
12  
13 1 ▶use base qw( Net::Async::Gearman Protocol::Gearman::Worker );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 Protocol::Gearman::Worker->VERSION( '0.03' ); # job_finished
15  
16 =head1 NAME
17  
18 C<Net::Async::Gearman::Worker> - concrete Gearman worker over an L<IO::Async::Stream>
19  
20 =head1 SYNOPSIS
21  
22 =head1 DESCRIPTION
23  
24 This module combines the abstract L<Protocol::Gearman::Worker> with
25 L<Net::Async::Gearman> to provide an asynchronous concrete Gearman worker
26 implementation.
27  
28 =cut
29  
30 =head1 METHODS
31  
32 =cut
33  
34 =head2 $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 {
51 0 ▶ my $self = shift;
Subroutine add_function: not called CC=1 SCAR=6.9
Pod documented
52 0 my ( $name, $code ) = @_;
53  
54 0 $self->gearman_state->{gearman_funcs}{$name} = $code;
55 }
56  
57 sub connect
58 {
59 0 ▶ my $self = shift;
Subroutine connect: not called CC=1 SCAR=6.9
Pod documented
60 $self->SUPER::connect( @_ )
61 ->on_done( sub {
62 0 ▶ my $funcs = $self->gearman_state->{gearman_funcs};
Subroutine __ANON__: not called CC=2 SCAR=17.9
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 {
72 0 ▶ my $self = shift;
Subroutine job_finished: not called CC=2 SCAR=17.9
Pod documented
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 {
81 0 ▶ my $self = shift;
Subroutine start_grab_job: not called CC=1 SCAR=6.9
Pod undocumented
82  
83 0 my $state = $self->gearman_state;
84  
85 $state->{gearman_grabf} = $self->grab_job
86 ->on_done( sub {
87 0 ▶ my ( $job ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
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;
« Build ↑ Summary blib/lib/Net/Async/Gearman.pm »