NAME
Gearman::Worker - Worker for gearman distributed job system
SYNOPSIS
use Gearman::Worker;
my $worker = Gearman::Worker->new;
$worker->job_servers('127.0.0.1');
$worker->register_function($funcname => $subref);
$worker->work while 1;
DESCRIPTION
Gearman::Worker is a worker class for the Gearman distributed job system, providing a framework for receiving and serving jobs from a Gearman server.
Callers instantiate a Gearman::Worker object, register a list of functions and capabilities that they can handle, then enter an event loop, waiting for the server to send jobs.
The worker can send a return value back to the server, which then gets sent back to the client that requested the job; or it can simply execute silently.
USAGE
Gearman::Worker->new(\%options)
Creates a new Gearman::Worker object, and returns the object.
If %options is provided, initializes the new worker object with the settings in %options, which can contain:
job_servers
Calls job_servers (see below) to initialize the list of job servers.
$worker->job_servers(@servers)
Initializes the worker $worker with the list of job servers in @servers. @servers should contain a list of IP addresses, with optional port numbers. For example:
$worker->job_servers('127.0.0.1', '192.168.1.100:7003');
If the port number is not provided, 7003 is used as the default.
$worker->register_function($funcname, $subref)
Registers the function $funcname as being provided by the worker $worker, and advertises these capabilities to all of the job servers defined in this worker.
$subref must be a subroutine reference that will be invoked when the worker receives a request for this function. It will be passed a Gearman::Job object representing the job that has been received by the worker.
The subroutine reference can return a return value, which will be sent back to the job server.
Gearman::Job->arg
Returns the scalar argument that the client sent to the job server.
Gearman::Job->set_status($numerator, $denominator)
Updates the status of the job (most likely, a long-running job) and sends it back to the job server. $numerator and $denominator should represent the percentage completion of the job.
EXAMPLES
Summation
This is an example worker that receives a request to sum up a list of integers.
use Gearman::Worker;
use Storable qw( thaw );
use List::Util qw( sum );
my $worker = Gearman::Worker->new;
$worker->job_servers('127.0.0.1');
$worker->register_function(sum => sub { sum @{ thaw($_[0]->arg) } });
$worker->work while 1;
See the Gearman::Client documentation for a sample client sending the sum job.