Adam Thomason > Gearman-WorkerSpawner > Gearman::WorkerSpawner::BaseWorker

Download:
Gearman-WorkerSpawner-2.16.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Source  

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

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

METHODS ^

register_method($function_name)
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.

method_suffix([$suffix])

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

syntax highlighting: