Gearman::Driver::Worker - Base class for workers
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;
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.
This will register the method with gearmand.
Minimum number of childs working parallel on this job/method.
Maximum number of childs working parallel on this job/method.
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.
My::Worker::some_job
ref(shift . '::')
prefix
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
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.
Johannes Plunien <plu@cpan.org>
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.
Gearman::Driver
Gearman::Driver::Observer
Gearman::Driver::Job
To install Gearman::Driver, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Gearman::Driver
CPAN shell
perl -MCPAN -e shell install Gearman::Driver
For more information on module installation, please visit the detailed CPAN module installation guide.