Provided by: libgearman-client-perl_2.004.015-1_all
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', { host => '10.0.0.1', port => 4730, socket_cb => sub {...}, use_ssl => 1, ca_file => ..., cert_file => ..., key_file => ..., } ); $worker->register_function($funcname => sub { ... } ); $worker->work( on_start => sub { my ($jobhandle) = @_; ... }, on_complete => sub { my ($jobhandle, $result) = @_; ... }, on_fail => sub { my ($jobhandle, $err) = @_; .. }, stop_if => sub { my ($is_idle, $last_job_time) = @_; # stop idle worker return $is_idle; }, );
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: Gearman::Worker is derived from Gearman::Objects • job_servers List of job servers. Value should be an array reference, hash reference or scalar. It will be ignored if this worker is running as a child process of a gearman server. • prefix Calls prefix (see below) to set the prefix / namespace. • client_id Unique worker identifier for "job_servers". $worker->prefix($prefix) Sets the namespace / prefix for the function names. This is useful for sharing job servers between different applications or different instances of the same application (different development sandboxes for example). The namespace is currently implemented as a simple tab separated concatenation of the prefix and the function name.
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.
NOTE
If you intend to send or receive UTF-8 data over SSL connections, beware that there is no UTF-8 support in the underlying Net::SSLeay. "Forcing-Unicode-in-Perl-(Or-Unforcing-Unicode-in-Perl)" in perlunicode describes proper workarounds.
METHODS
reset_abilities This tells all the job servers that this worker can no longer do any tasks. return true if "reset_abilities" request successfully transmitted to "job_servers" work(%opts) This endlessly loops. It takes an applicable job, if available, does the job, and then waits for the next one. You can pass "stop_if", "on_start", "on_complete" and "on_fail" callbacks in %opts. See "SYNOPSIS" $worker->register_function($funcname, $subref) $worker->register_function($funcname, $timeout, $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. $timeout is an optional parameter specifying how long the jobserver will wait for your subroutine to give an answer. Exceeding this time will result in the jobserver reassigning the task and ignoring your result. This prevents a gimpy worker from ruining the 'user experience' in many situations. return true if $funcname registration successfully transmitted to "job_servers" unregister_function($funcname) send cant_do $funcname request to job_servers return true if CANT_DO $funcname request successfully transmitted to "job_servers" job_servers(@servers) Override Gearman::Objects method to skip job server initialization if working with Gearman::Server. Calling this method will do nothing in a worker that is running as a child process of a gearman server. send_work_complete($job, $v) notify the server (and listening clients) that job completed successfully send_work_data($job, $data) Use this method to update the client with data from a running job. send_work_warning($job, $message) Use this method to send a warning $message to the server (and any listening clients) with regard to the running "job". send_work_exception($job, $exception) Use this method to notify the server (and any listening clients) that the "job" failed with the given $exception. If you are using Gearman::Client, you have to set parameter exceptions properly to get worker exception notifications. send_work_fail($job) Use this method to notify the server (and any listening clients) that the job failed. send_work_status($job, $numerator, $denominator) Use this method to send periodically to the server status update for long running jobs to update the percentage complete. _uncache_sock($js, $reason) close TCP connection
WORKERS AS CHILD PROCESSES
Gearman workers can be run as child processes of a parent process which embeds Gearman::Server. When such a parent process fork/execs a worker, it sets the environment variable GEARMAN_WORKER_USE_STDIO to true before launching the worker. If this variable is set to true, then the job_servers function and option for new() are ignored and the unix socket bound to STDIN/OUT are used instead as the IO path to the gearman server.