NAME
Gearman::WorkerSpawner::BaseWorker - Base class to simplify the process of creating a Gearman worker for use with Gearman::WorkerSpawner
SYNOPSIS
# create manager as normal. the contents of 'config' can be arbitrary; the
# special 'max_jobs' option instructs the worker to exit cleanly after
# performing that many jobs.
my $worker_manager = Gearman::WorkerSpawner->new;
$worker_manager->add_worker(
class => 'AdditionWorker',
config => {
left_hand => 5,
max_jobs => 100,
},
);
# invoke run_method instead of add_task for workers derived from
# BaseWorker. serialization of options is handled for you, and if you only care
# about the success case you can provide only that callback
$worker_manager->run_method(adder => { right_hand => 3 }, sub {
my $return = shift;
print $return->{sum};
});
Danga::Socket->EventLoop;
# in the worker:
package MethodWorker;
use base 'Gearman::WorkerSpawner::BaseWorker';
# Gearman::WorkerSpawner will instantiate your class; the object will
# contain populated 'config' and 'slot' fields
sub new {
my $class = shift;
my MethodWorker $self = bless $self->SUPER::new(@_), $class;
print "I am worker $self->{slot}\n";
$self->register_method(adder => \&add);
return $self;
}
sub add {
my MethodWorker $self = shift;
my $args = shift;
return { sum => $self->{config}{left_hand} + $args->{right_hand} };
}