.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Gearman::Worker 3pm" .TH Gearman::Worker 3pm "2018-08-30" "perl v5.26.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Gearman::Worker \- Worker for gearman distributed job system .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 10 \& use Gearman::Worker; \& my $worker = Gearman::Worker\->new; \& $worker\->job_servers( \& \*(Aq127.0.0.1\*(Aq, \& { \& host => \*(Aq10.0.0.1\*(Aq, \& 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; \& }, \& ); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\fIGearman::Worker\fR is a worker class for the Gearman distributed job system, providing a framework for receiving and serving jobs from a Gearman server. .PP Callers instantiate a \fIGearman::Worker\fR object, register a list of functions and capabilities that they can handle, then enter an event loop, waiting for the server to send jobs. .PP 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. .SH "USAGE" .IX Header "USAGE" .SS "Gearman::Worker\->new(%options)" .IX Subsection "Gearman::Worker->new(%options)" Creates a new \fIGearman::Worker\fR object, and returns the object. .PP If \fI\f(CI%options\fI\fR is provided, initializes the new worker object with the settings in \fI\f(CI%options\fI\fR, which can contain: .PP \&\fIGearman::Worker\fR is derived from Gearman::Objects .IP "\(bu" 4 job_servers .Sp 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. .IP "\(bu" 4 prefix .Sp Calls \fIprefix\fR (see below) to set the prefix / namespace. .IP "\(bu" 4 client_id .Sp Unique worker identifier for \f(CW\*(C`job_servers\*(C'\fR. .ie n .SS "$worker\->prefix($prefix)" .el .SS "\f(CW$worker\fP\->prefix($prefix)" .IX Subsection "$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). .PP The namespace is currently implemented as a simple tab separated concatenation of the prefix and the function name. .SH "EXAMPLES" .IX Header "EXAMPLES" .SS "Summation" .IX Subsection "Summation" This is an example worker that receives a request to sum up a list of integers. .PP .Vb 7 \& use Gearman::Worker; \& use Storable qw( thaw ); \& use List::Util qw( sum ); \& my $worker = Gearman::Worker\->new; \& $worker\->job_servers(\*(Aq127.0.0.1\*(Aq); \& $worker\->register_function(sum => sub { sum @{ thaw($_[0]\->arg) } }); \& $worker\->work while 1; .Ve .PP See the Gearman::Client documentation for a sample client sending the \&\fIsum\fR job. .SH "NOTE" .IX Header "NOTE" If you intend to send or receive \s-1UTF\-8\s0 data over \s-1SSL\s0 connections, beware that there is no \s-1UTF\-8\s0 support in the underlying Net::SSLeay. \&\*(L"Forcing\-Unicode\-in\-Perl\-(Or\-Unforcing\-Unicode\-in\-Perl)\*(R" in perlunicode describes proper workarounds. .SH "METHODS" .IX Header "METHODS" .SS "reset_abilities" .IX Subsection "reset_abilities" This tells all the job servers that this worker can no longer do any tasks. .PP \&\fBreturn\fR true if \f(CW\*(C`reset_abilities\*(C'\fR request successfully transmitted to \f(CW\*(C`job_servers\*(C'\fR .SS "work(%opts)" .IX Subsection "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 \*(L"stop_if\*(R", \*(L"on_start\*(R", \*(L"on_complete\*(R" and \*(L"on_fail\*(R" callbacks in \fI\f(CI%opts\fI\fR. See \*(L"\s-1SYNOPSIS\*(R"\s0 .ie n .SS "$worker\->register_function($funcname, $subref)" .el .SS "\f(CW$worker\fP\->register_function($funcname, \f(CW$subref\fP)" .IX Subsection "$worker->register_function($funcname, $subref)" .ie n .SS "$worker\->register_function($funcname, $timeout, $subref)" .el .SS "\f(CW$worker\fP\->register_function($funcname, \f(CW$timeout\fP, \f(CW$subref\fP)" .IX Subsection "$worker->register_function($funcname, $timeout, $subref)" Registers the function \f(CW$funcname\fR as being provided by the worker \&\f(CW$worker\fR, and advertises these capabilities to all of the job servers defined in this worker. .PP \&\f(CW$subref\fR 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. .PP \&\f(CW$timeout\fR 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. .PP \&\fBreturn\fR true if \f(CW$funcname\fR registration successfully transmitted to \f(CW\*(C`job_servers\*(C'\fR .SS "unregister_function($funcname)" .IX Subsection "unregister_function($funcname)" send cant_do \f(CW$funcname\fR request to job_servers .PP \&\fBreturn\fR true if \s-1CANT_DO\s0 \f(CW$funcname\fR request successfully transmitted to \f(CW\*(C`job_servers\*(C'\fR .SS "job_servers(@servers)" .IX Subsection "job_servers(@servers)" Override Gearman::Objects method to skip job server initialization if working with Gearman::Server. .PP Calling this method will do nothing in a worker that is running as a child process of a gearman server. .ie n .SS "send_work_complete($job, $v)" .el .SS "send_work_complete($job, \f(CW$v\fP)" .IX Subsection "send_work_complete($job, $v)" notify the server (and listening clients) that job completed successfully .ie n .SS "send_work_data($job, $data)" .el .SS "send_work_data($job, \f(CW$data\fP)" .IX Subsection "send_work_data($job, $data)" Use this method to update the client with data from a running job. .ie n .SS "send_work_warning($job, $message)" .el .SS "send_work_warning($job, \f(CW$message\fP)" .IX Subsection "send_work_warning($job, $message)" Use this method to send a warning \f(CW$message\fR to the server (and any listening clients) with regard to the running \f(CW\*(C`job\*(C'\fR. .ie n .SS "send_work_exception($job, $exception)" .el .SS "send_work_exception($job, \f(CW$exception\fP)" .IX Subsection "send_work_exception($job, $exception)" Use this method to notify the server (and any listening clients) that the \f(CW\*(C`job\*(C'\fR failed with the given \f(CW$exception\fR. .PP If you are using Gearman::Client, you have to set parameter exceptions properly to get worker exception notifications. .SS "send_work_fail($job)" .IX Subsection "send_work_fail($job)" Use this method to notify the server (and any listening clients) that the job failed. .ie n .SS "send_work_status($job, $numerator, $denominator)" .el .SS "send_work_status($job, \f(CW$numerator\fP, \f(CW$denominator\fP)" .IX Subsection "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. .ie n .SS "_uncache_sock($js, $reason)" .el .SS "_uncache_sock($js, \f(CW$reason\fP)" .IX Subsection "_uncache_sock($js, $reason)" close \s-1TCP\s0 connection .SH "WORKERS AS CHILD PROCESSES" .IX Header "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 \&\s-1GEARMAN_WORKER_USE_STDIO\s0 to true before launching the worker. If this variable is set to true, then the job_servers function and option for \&\fInew()\fR are ignored and the unix socket bound to \s-1STDIN/OUT\s0 are used instead as the \s-1IO\s0 path to the gearman server.