1Gearman::Worker(3) User Contributed Perl Documentation Gearman::Worker(3)
2
3
4
6 Gearman::Worker - Worker for gearman distributed job system
7
9 use Gearman::Worker;
10 my $worker = Gearman::Worker->new;
11 $worker->job_servers('127.0.0.1');
12 $worker->register_function($funcname => $subref);
13 $worker->work while 1;
14
16 Gearman::Worker is a worker class for the Gearman distributed job
17 system, providing a framework for receiving and serving jobs from a
18 Gearman server.
19
20 Callers instantiate a Gearman::Worker object, register a list of
21 functions and capabilities that they can handle, then enter an event
22 loop, waiting for the server to send jobs.
23
24 The worker can send a return value back to the server, which then gets
25 sent back to the client that requested the job; or it can simply
26 execute silently.
27
29 Gearman::Worker->new(%options)
30 Creates a new Gearman::Worker object, and returns the object.
31
32 If %options is provided, initializes the new worker object with the
33 settings in %options, which can contain:
34
35 · job_servers
36
37 Calls job_servers (see below) to initialize the list of job
38 servers. It will be ignored if this worker is running as a child
39 process of a gearman server.
40
41 · prefix
42
43 Calls prefix (see below) to set the prefix / namespace.
44
45 $worker->job_servers(@servers)
46 Initializes the worker $worker with the list of job servers in
47 @servers. @servers should contain a list of IP addresses, with
48 optional port numbers. For example:
49
50 $worker->job_servers('127.0.0.1', '192.168.1.100:7003');
51
52 If the port number is not provided, 7003 is used as the default.
53
54 Calling this method will do nothing in a worker that is running as a
55 child process of a gearman server.
56
57 $worker->register_function($funcname, $subref)
58 $worker->register_function($funcname, $timeout, $subref)
59 Registers the function $funcname as being provided by the worker
60 $worker, and advertises these capabilities to all of the job servers
61 defined in this worker.
62
63 $subref must be a subroutine reference that will be invoked when the
64 worker receives a request for this function. It will be passed a
65 Gearman::Job object representing the job that has been received by the
66 worker.
67
68 $timeout is an optional parameter specifying how long the jobserver
69 will wait for your subroutine to give an answer. Exceeding this time
70 will result in the jobserver reassigning the task and ignoring your
71 result. This prevents a gimpy worker from ruining the 'user experience'
72 in many situations.
73
74 The subroutine reference can return a return value, which will be sent
75 back to the job server.
76
77 $client->prefix($prefix)
78 Sets the namespace / prefix for the function names. This is useful for
79 sharing job servers between different applications or different
80 instances of the same application (different development sandboxes for
81 example).
82
83 The namespace is currently implemented as a simple tab separated
84 concatentation of the prefix and the function name.
85
86 Gearman::Job->arg
87 Returns the scalar argument that the client sent to the job server.
88
89 Gearman::Job->set_status($numerator, $denominator)
90 Updates the status of the job (most likely, a long-running job) and
91 sends it back to the job server. $numerator and $denominator should
92 represent the percentage completion of the job.
93
94 Gearman::Job->work(%opts)
95 Do one job and returns (no value returned). You can pass "on_start"
96 "on_complete" and "on_fail" callbacks in %opts.
97
99 Gearman workers can be run run as child processes of a parent process
100 which embeds Gearman::Server. When such a parent process fork/execs a
101 worker, it sets the environment variable GEARMAN_WORKER_USE_STDIO to
102 true before launching the worker. If this variable is set to true, then
103 the jobservers function and option for new() are ignored and the unix
104 socket bound to STDIN/OUT are used instead as the IO path to the
105 gearman server.
106
108 Summation
109 This is an example worker that receives a request to sum up a list of
110 integers.
111
112 use Gearman::Worker;
113 use Storable qw( thaw );
114 use List::Util qw( sum );
115 my $worker = Gearman::Worker->new;
116 $worker->job_servers('127.0.0.1');
117 $worker->register_function(sum => sub { sum @{ thaw($_[0]->arg) } });
118 $worker->work while 1;
119
120 See the Gearman::Client documentation for a sample client sending the
121 sum job.
122
123
124
125perl v5.12.1 2009-10-05 Gearman::Worker(3)