Gearman::Driver::Worker - Base class for workers
package My::Worker; use base qw(Gearman::Driver::Worker); use Moose; sub begin { my ( $self, $job, $workload ) = @_; # 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, $workload ) = @_; # $job => Gearman::XS::Job instance } sub end { my ( $self, $job, $workload ) = @_; # called after each job } sub spread_work : Job { my ( $self, $job, $workload ) = @_; 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 passed 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.
This will automatically look for a method encode in this object which has to be defined in the subclass. It will call the encode method passing the return value from the job method. The return value of the encode method will be returned to the Gearman client. This is useful to serialize Perl datastructures to JSON before sending them back to the client.
encode
sub do_some_job : Job : Encode : Decode { my ( $self, $job, $workload ) = @_; return { message => 'OK', status => 1 }; # calls 'encode' and returns JSON string: {"status":1,"message":"OK"} } sub custom_encoder : Job : Encode(enc_yaml) : Decode(dec_yaml) { my ( $self, $job, $workload ) = @_; return { message => 'OK', status => 1 }; # calls 'enc_yaml' and returns YAML string: # --- # message: OK # status: 1 } sub encode { my ( $self, $result ) = @_; return JSON::XS::encode_json($result); } sub decode { my ( $self, $workload ) = @_; return JSON::XS::decode_json($workload); } sub enc_yaml { my ( $self, $result ) = @_; return YAML::XS::Dump($result); } sub dec_yaml { my ( $self, $workload ) = @_; return YAML::XS::Load($workload); }
This will automatically look for a method decode in this object which has to be defined in the subclass. It will call the decode method passing the workload value ($job->workload). The return value of the decode method will be passed as 3rd argument to the job method. This is useful to deserialize JSON workload to Perl datastructures for example. If this attribute is not set, $job->workload and $workload is the same.
decode
$job->workload
$workload
Example, workload is this string: {"status":1,"message":"OK"}
{"status":1,"message":"OK"}
sub decode { my ( $self, $workload ) = @_; return JSON::XS::decode_json($workload); } sub job1 : Job { my ( $self, $job, $workload ) = @_; # $workload eq $job->workload eq '{"status":1,"message":"OK"}' } sub job2 : Job : Decode { my ( $self, $job, $workload ) = @_; # $workload ne $job->workload # $job->workload eq '{"status":1,"message":"OK"}' # $workload = { status => 1, message => 'OK' } }
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.
If this method is overridden in the subclass it will change the process name after a job has been forked.
The following parameters are passed to this method:
$orig - the original process name ( $0 )
$orig
$0
$job_name - the name of the job
$job_name
Example:
sub process_name { my ( $self, $orig, $job_name ) = @_; return "$orig ($job_name)"; }
This may look like:
plu 2034 0.0 1.7 22392 17948 pts/2 S 21:17 0:00 ./examples/test.pl (GDExamples::Sleeper::ZzZzZzzz) plu 2035 0.0 1.7 22392 17944 pts/2 S 21:17 0:00 ./examples/test.pl (GDExamples::Sleeper::ZzZzZzzz) plu 2036 0.0 1.7 22392 17948 pts/2 S 21:17 0:00 ./examples/test.pl (GDExamples::Sleeper::ZzZzZzzz) plu 2037 0.0 1.7 22392 17956 pts/2 S 21:17 0:00 ./examples/test.pl (GDExamples::Sleeper::long_running_ZzZzZzzz)
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.