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; }
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 @_.
->new
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.
$self->{slot}
$self->{data}
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.
$self
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.
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.
The parameters to $method and return value (which should be a scalar) from it are marshalled by Storable.
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')
To install Gearman::Spawner, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Gearman::Spawner
CPAN shell
perl -MCPAN -e shell install Gearman::Spawner
For more information on module installation, please visit the detailed CPAN module installation guide.