View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Adam Thomason > Gearman-WorkerSpawner > Gearman::WorkerSpawner::BaseWorker



Annotate this POD


Open  1
View/Report Bugs


Gearman::WorkerSpawner::BaseWorker - Base class to simplify the process of creating a Gearman worker for use with Gearman::WorkerSpawner


    # 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;
        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};


    # 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 MethodWorker $self = shift;
        $self = fields::new($self) unless ref $self;

        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} };


register_method($function_name, $method)
register_method($function_name, $method, $timeout)

Registers a method to be called via Gearman::WorkerSpawner->run_method. 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::WorkerSpawner::BaseWorker 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.


Accessor for the suffix which is appended to the method name. Defaults to '_m'.

syntax highlighting: