File Coverage

blib/lib/Net/Async/Gearman/Worker.pm
Criterion Covered Total %
statement 9 32 28.1
branch 0 2 0.0
condition n/a
subroutine 3 9 33.3
pod 3 4 75.0
total 15 47 31.9


line stmt bran cond sub pod time code
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     1   963 use strict;
  1         2  
  1         31  
9 1     1   4 use warnings;
  1         3  
  1         39  
10              
11             our $VERSION = '0.01';
12              
13 1     1   4 use base qw( Net::Async::Gearman Protocol::Gearman::Worker );
  1         2  
  1         807  
14             Protocol::Gearman::Worker->VERSION( '0.03' ); # job_finished
15              
16             =head1 NAME
17              
18             C - concrete Gearman worker over an L
19              
20             =head1 SYNOPSIS
21              
22             =head1 DESCRIPTION
23              
24             This module combines the abstract L with
25             L 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
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 or C 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     0 1   my $self = shift;
52 0           my ( $name, $code ) = @_;
53              
54 0           $self->gearman_state->{gearman_funcs}{$name} = $code;
55             }
56              
57             sub connect
58             {
59 0     0 1   my $self = shift;
60             $self->SUPER::connect( @_ )
61             ->on_done( sub {
62 0     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             {
72 0     0 1   my $self = shift;
73              
74 0           my $state = $self->gearman_state;
75              
76 0 0         $self->start_grab_job unless $self->{gearman_grabf};
77             }
78              
79             sub start_grab_job
80             {
81 0     0 0   my $self = shift;
82              
83 0           my $state = $self->gearman_state;
84              
85             $state->{gearman_grabf} = $self->grab_job
86             ->on_done( sub {
87 0     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] ) } )
98 0           ->on_fail( sub { $job->fail() } )
99 0           ->on_ready( sub { delete $state->{gearman_jobs}{$handle} } );
  0            
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 itself.
111              
112             =back
113              
114             =head1 AUTHOR
115              
116             Paul Evans
117              
118             =cut
119              
120             0x55AA;