SYNOPSIS
package IncrementWorker;
use base 'Gearman::Spawner::Worker';
sub new {
# copy these three lines of boilerplate into your subclass's new:
my $self = shift;
$self = fields::new($self) unless ref $self;
$self->SUPER::new(@_);
print "I am worker number $self->{slot}\n";
print "My configuration is $self->{data}\n";
# ... and these two, with your own methods:
$self->register_method('increment');
return $self;
}
sub increment {
my MethodWorker $self = shift; # slot and data available here too
my $arg = shift;
return $arg + 1;
}
DESCRIPTION
This is the base class for workers meant to be supervised by Gearman::Spawner. For correct operation, the ->new
method of descendant classes must call the ->new
method of this class with their @_.
Since this class is itself descended from the fields-derived Gearman::Worker, subclasses may declare additional object members using fields, e.g.,
use fields qw( foo bar ); # $self->{foo} = 1;
Two object members are already available: $self->{slot}
and $self->{data}
. The data member is whatever was passed in the configuration for the worker in Gearman::Spawner->new. The slot is a sequential number (1-based) which identifies the worker within the set of those it was spawned with in its class.
METHODS
- Gearman::Spawner::Worker->new($method_name, [$timeout])
-
Registers a method to be called via a Gearman::Spawner::Worker class. A Gearman function will be registered with the server with a name based on the method name. When the client module's run_method function is called, the argument will be passed to the registered method as its first non-
$self
argument.If $timeout is provided, the Gearman server may attempt to retry the function if the job is not finished withint $timeout seconds.
The parameters to $method and its return value are marshalled by Storable.
- $self->register_method($method_name, [$timeout])
-
Registers a method to be called via Gearman::Spawner::Worker. A Gearman function named $function_name will be registered with the server. When a client calls that function, the method named $method (may alternatively be a coderef) will be called with the Gearman::Spawner::Worker object as the first argument. $method defaults to be the same as $function_name if not provided.
If $timeout is provided, the Gearman server may attempt to retry the function if the job is not finished withint $timeout seconds.
The parameters to $method and return value (which should be a scalar) from it are marshalled by Storable.
- $self->function_name($method)
-
Returns the name of the function with which the given method was registered with the gearmand. Generally the Gearman::Spawner::Client modules handle figuring this out from the class and method names on your behalf. If for some reason you require the name, use e.g.:
My::Gearman::Worker->function_name('mymethod')