NAME

Gearman::Driver::Worker - Base class for workers

SYNOPSIS

package My::Worker;

use base qw(Gearman::Driver::Worker);
use Moose;

sub begin {
    # called before each job
}

sub prefix {
    # default: return ref(shift) . '::';
    return join '_', split /::/, __PACKAGE__;
}

sub do_something : Job : MinChilds(2) : MaxChilds(15) {
    my ( $self, $job ) = @_;
    # $job => Gearman::XS::Job instance
}

sub end {
    # called after each job
}

sub spread_work : Job {
    my ( $self, $job ) = @_;

    my $gc = Gearman::XS::Client->new;
    $gc->add_servers( $self->server );

    $gc->do_background( 'some_job_1' => $job->workload );
    $gc->do_background( 'some_job_2' => $job->workload );
    $gc->do_background( 'some_job_3' => $job->workload );
    $gc->do_background( 'some_job_4' => $job->workload );
    $gc->do_background( 'some_job_5' => $job->workload );
}

1;

DESCRIPTION

ATTRIBUTES

server

Gearman::Driver connects to the server give to its constructor. This value is also stored in this class. This can be useful if a job uses Gearman::XS::Client to add another jobs. See 'spread_work' method in "SYNOPSIS" above.

METHODATTRIBUTES

Job

This will register the method with gearmand.

MinChilds

Minimum number of childs working parallel on this job/method.

MaxChilds

Maximum number of childs working parallel on this job/method.

METHODS

prefix

Having the same method name in two different classes would result in a clash when registering it with gearmand. To avoid this, all jobs are registered with the full package and method name (e.g. My::Worker::some_job). The default prefix is ref(shift . '::'), but this can be changed by overriding the prefix method in the subclass, see "SYNOPSIS" above.

begin

This method is called before a job method is called. In this base class this methods just does nothing, but can be overridden in a subclass.

The parameters are the same as in the job method:

  • $self

  • $job

end

This method is called after a job method has been called. In this base class this methods just does nothing, but can be overridden in a subclass.

The parameters are the same as in the job method:

  • $self

  • $job

AUTHOR

Johannes Plunien <plu@cpan.org>

COPYRIGHT AND LICENSE

Copyright 2009 by Johannes Plunien

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO