File Coverage

blib/lib/Gearman/Spawner/Worker.pm
Criterion Covered Total %
statement 45 46 97.8
branch 3 6 50.0
condition n/a
subroutine 11 11 100.0
pod 3 3 100.0
total 62 66 93.9


line stmt bran cond sub pod time code
1             package Gearman::Spawner::Worker;
2              
3 11     11   20006 use strict;
  11         40  
  11         1049  
4 11     11   79 use warnings;
  11         20  
  11         437  
5              
6 11     11   82 use Carp qw( croak );
  11         28  
  11         1874  
7 11     11   2348 use Gearman::Spawner::Util;
  11         29  
  11         650  
8 11     11   5844 use Storable qw( nfreeze thaw );
  11         19792  
  11         1486  
9              
10 11     11   86 use base 'Gearman::Worker';
  11         23  
  11         16167  
11              
12 11         94 use fields qw(
13             data
14             slot
15 11     11   71131 );
  11         38  
16              
17             =head1 SYNOPSIS
18              
19             package IncrementWorker;
20             use base 'Gearman::Spawner::Worker';
21              
22             sub new {
23             # copy these three lines of boilerplate into your subclass's new:
24             my $self = shift;
25             $self = fields::new($self) unless ref $self;
26             $self->SUPER::new(@_);
27              
28             print "I am worker number $self->{slot}\n";
29             print "My configuration is $self->{data}\n";
30              
31             # ... and these two, with your own methods:
32             $self->register_method('increment');
33             return $self;
34             }
35              
36             sub increment {
37             my MethodWorker $self = shift; # slot and data available here too
38             my $arg = shift;
39             return $arg + 1;
40             }
41              
42             =head1 DESCRIPTION
43              
44             This is the base class for workers meant to be supervised by
45             L. For correct operation, the C<< ->new >> method of
46             descendant classes must call the C<< ->new >> method of this class with their
47             @_.
48              
49             Since this class is itself descended from the L-derived L, subclasses may declare additional object members using L, e.g.,
50              
51             use fields qw( foo bar ); # $self->{foo} = 1;
52              
53             Two object members are already available: C<< $self->{slot} >> and C<<
54             $self->{data} >>. The I member is whatever was passed in the
55             configuration for the worker in Gearman::Spawner->new. The I is a
56             sequential number (1-based) which identifies the worker within the set of those
57             it was spawned with in its class.
58              
59             =head1 METHODS
60              
61             =over 4
62              
63             =item Gearman::Spawner::Worker->new($method_name, [$timeout])
64              
65             Registers a method to be called via a Gearman::Spawner::Worker class. A Gearman
66             function will be registered with the server with a name based on the method
67             name. When the client module's run_method function is called, the argument will
68             be passed to the registered method as its first non-C<$self> argument.
69              
70             If $timeout is provided, the Gearman server may attempt to retry the function
71             if the job is not finished withint $timeout seconds.
72              
73             The parameters to $method and its return value are marshalled by Storable.
74              
75             =cut
76              
77             sub new {
78 5     5 1 47492 my $self = shift;
79 5 50       85 $self = fields::new($self) unless ref $self;
80              
81 5         53 my ($servers, $slot, $data) = @_;
82              
83 5         158 $self->SUPER::new(job_servers => $servers);
84              
85 5         931 $self->{data} = $data;
86 5         37 $self->{slot} = $slot;
87              
88 5         24 return $self;
89             }
90              
91             =item $self->register_method($method_name, [$timeout])
92              
93             Registers a method to be called via Gearman::Spawner::Worker. A
94             Gearman function named $function_name will be registered with the server. When
95             a client calls that function, the method named $method (may alternatively be a
96             coderef) will be called with the Gearman::Spawner::Worker object as
97             the first argument. $method defaults to be the same as $function_name if not
98             provided.
99              
100             If $timeout is provided, the Gearman server may attempt to retry the function
101             if the job is not finished withint $timeout seconds.
102              
103             The parameters to $method and return value (which should be a scalar) from it
104             are marshalled by Storable.
105              
106             =cut
107              
108             sub register_method {
109 20     20 1 178 my Gearman::Spawner::Worker $self = shift;
110 20         294 my $method = shift;
111 20         39 my $timeout = shift;
112              
113 20 50       181 croak "$self cannot execute $method" unless $self->can($method);
114              
115             my $do_work = sub {
116 21     21   18696 my $job = shift;
117 21         89 my $arg = $job->arg;
118              
119             # deserialize argument
120 21         347 my $params = thaw($job->arg);
121              
122             # call the method
123 21         1037 my @retvals = $self->$method(@$params);
124              
125             # serialize return value(s)
126 18         516 return \nfreeze(\@retvals);
127 20         188 };
128              
129 20         113 my $func_name = $self->function_name($method);
130 20 50       101 if ($timeout) {
131 0         0 $self->register_function($func_name, $timeout, $do_work);
132             }
133             else {
134 20         179 $self->register_function($func_name, $do_work);
135             }
136              
137 20         41530 return $func_name;
138              
139             }
140              
141             =item $self->function_name($method)
142              
143             Returns the name of the function with which the given method was registered
144             with the gearmand. Generally the Gearman::Spawner::Client modules handle
145             figuring this out from the class and method names on your behalf. If for some
146             reason you require the name, use e.g.:
147              
148             My::Gearman::Worker->function_name('mymethod')
149              
150             =back
151              
152             =cut
153              
154             sub function_name {
155 20     20 1 32 my $self = shift;
156 20         56 my $method = shift;
157 20         184 return Gearman::Spawner::Util::method2function(ref $self, $method);
158             }
159              
160             1;