xing-gearman-ruby 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/LICENSE +20 -0
- data/README +9 -0
- data/Rakefile +40 -0
- data/VERSION.yml +4 -0
- data/examples/Net/Gearman/Client.php +290 -0
- data/examples/Net/Gearman/Connection.php +410 -0
- data/examples/Net/Gearman/Exception.php +42 -0
- data/examples/Net/Gearman/Job/Common.php +135 -0
- data/examples/Net/Gearman/Job/Exception.php +45 -0
- data/examples/Net/Gearman/Job/Sleep.php +33 -0
- data/examples/Net/Gearman/Job.php +85 -0
- data/examples/Net/Gearman/Manager.php +320 -0
- data/examples/Net/Gearman/Set.php +215 -0
- data/examples/Net/Gearman/Task.php +300 -0
- data/examples/Net/Gearman/Worker.php +455 -0
- data/examples/client.php +23 -0
- data/examples/client.rb +15 -0
- data/examples/scale_image.rb +32 -0
- data/examples/scale_image_worker.rb +34 -0
- data/examples/server.rb +13 -0
- data/examples/worker.php +15 -0
- data/examples/worker.rb +23 -0
- data/gearman-ruby.gemspec +81 -0
- data/lib/gearman/client.rb +139 -0
- data/lib/gearman/server.rb +94 -0
- data/lib/gearman/task.rb +128 -0
- data/lib/gearman/taskset.rb +241 -0
- data/lib/gearman/testlib.rb +96 -0
- data/lib/gearman/util.rb +212 -0
- data/lib/gearman/worker.rb +341 -0
- data/lib/gearman.rb +76 -0
- data/test/client_test.rb +111 -0
- data/test/mock_client_test.rb +431 -0
- data/test/mock_worker_test.rb +213 -0
- data/test/worker_test.rb +61 -0
- metadata +98 -0
@@ -0,0 +1,300 @@
|
|
1
|
+
<?php
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Interface for Danga's Gearman job scheduling system
|
5
|
+
*
|
6
|
+
* PHP version 5.1.0+
|
7
|
+
*
|
8
|
+
* LICENSE: This source file is subject to the New BSD license that is
|
9
|
+
* available through the world-wide-web at the following URI:
|
10
|
+
* http://www.opensource.org/licenses/bsd-license.php. If you did not receive
|
11
|
+
* a copy of the New BSD License and are unable to obtain it through the web,
|
12
|
+
* please send a note to license@php.net so we can mail you a copy immediately.
|
13
|
+
*
|
14
|
+
* @category Net
|
15
|
+
* @package Net_Gearman
|
16
|
+
* @author Joe Stump <joe@joestump.net>
|
17
|
+
* @copyright 2007-2008 Digg.com, Inc.
|
18
|
+
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
|
19
|
+
* @version CVS: $Id$
|
20
|
+
* @link http://pear.php.net/package/Net_Gearman
|
21
|
+
* @link http://www.danga.com/gearman/
|
22
|
+
*/
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Task class for creating Net_Gearman tasks
|
26
|
+
*
|
27
|
+
* @category Net
|
28
|
+
* @package Net_Gearman
|
29
|
+
* @author Joe Stump <joe@joestump.net>
|
30
|
+
* @copyright 2007-2008 Digg.com, Inc.
|
31
|
+
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
|
32
|
+
* @link http://www.danga.com/gearman/
|
33
|
+
* @see Net_Gearman_Set, Net_Gearman_Client
|
34
|
+
*/
|
35
|
+
class Net_Gearman_Task
|
36
|
+
{
|
37
|
+
/**
|
38
|
+
* The function/job to run
|
39
|
+
*
|
40
|
+
* @var string $func
|
41
|
+
*/
|
42
|
+
public $func = '';
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Arguments to pass to function/job
|
46
|
+
*
|
47
|
+
* @var array $arg
|
48
|
+
*/
|
49
|
+
public $arg = array();
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Type of job
|
53
|
+
*
|
54
|
+
* Which type of job you wish this task to be ran as. Keep in mind that
|
55
|
+
* background jobs are "fire and forget" and DO NOT return results to the
|
56
|
+
* job server in a manner that you can actually retrieve.
|
57
|
+
*
|
58
|
+
* @var integer $type
|
59
|
+
* @see Net_Gearman_Task::JOB_NORMAL
|
60
|
+
* @see Net_Gearman_Task::JOB_BACKGROUND
|
61
|
+
* @see Net_Gearman_Task::JOB_HIGH
|
62
|
+
*/
|
63
|
+
public $type = self::JOB_NORMAL;
|
64
|
+
|
65
|
+
/**
|
66
|
+
* Handle returned from job server
|
67
|
+
*
|
68
|
+
* @var string $handle
|
69
|
+
* @see Net_Gearman_Client
|
70
|
+
*/
|
71
|
+
public $handle = '';
|
72
|
+
|
73
|
+
/**
|
74
|
+
* The unique identifier for this job
|
75
|
+
*
|
76
|
+
* Keep in mind that a unique job is only unique to the job server it is
|
77
|
+
* submitted to. Gearman servers don't communicate with each other to
|
78
|
+
* ensure a job is unique across all workers.
|
79
|
+
*
|
80
|
+
* That being said, Gearman does group identical jobs sent to it and runs
|
81
|
+
* that job only once. If you send the job Sum with args 1, 2, 3 to the
|
82
|
+
* server 10 times in a second Gearman will only run that job once and then
|
83
|
+
* return the result 10 times.
|
84
|
+
*
|
85
|
+
* @var string $uniq
|
86
|
+
*/
|
87
|
+
public $uniq = '';
|
88
|
+
|
89
|
+
/**
|
90
|
+
* Is this task finished?
|
91
|
+
*
|
92
|
+
* @var boolean $finished
|
93
|
+
* @see Net_Gearman_Set::finished()
|
94
|
+
* @see Net_Gearman_Task::complete()
|
95
|
+
* @see Net_Gearman_Task::fail()
|
96
|
+
*/
|
97
|
+
public $finished = false;
|
98
|
+
|
99
|
+
/**
|
100
|
+
* The result returned from the worker
|
101
|
+
*
|
102
|
+
* @var object $result
|
103
|
+
*/
|
104
|
+
public $result = '';
|
105
|
+
|
106
|
+
/**
|
107
|
+
* Callbacks registered for each state
|
108
|
+
*
|
109
|
+
* @var array $callback
|
110
|
+
* @see Net_Gearman_Task::attachCallback()
|
111
|
+
* @see Net_Gearman_Task::complete()
|
112
|
+
* @see Net_Gearman_Task::status()
|
113
|
+
* @see Net_Gearman_Task::fail()
|
114
|
+
*/
|
115
|
+
protected $callback = array(
|
116
|
+
self::TASK_COMPLETE => array(),
|
117
|
+
self::TASK_FAIL => array(),
|
118
|
+
self::TASK_STATUS => array()
|
119
|
+
);
|
120
|
+
|
121
|
+
/**
|
122
|
+
* Normal job
|
123
|
+
*
|
124
|
+
* Normal jobs are ran against a worker with the result being returned
|
125
|
+
* all in the same thread (e.g. Your page will sit there waiting for the
|
126
|
+
* job to finish and return it's result).
|
127
|
+
*
|
128
|
+
* @var integer JOB_NORMAL
|
129
|
+
*/
|
130
|
+
const JOB_NORMAL = 1;
|
131
|
+
|
132
|
+
/**
|
133
|
+
* Background job
|
134
|
+
*
|
135
|
+
* Background jobs in Gearman are "fire and forget". You can check a job's
|
136
|
+
* status periodically, but you can't get a result back from it.
|
137
|
+
*
|
138
|
+
* @var integer JOB_BACKGROUND
|
139
|
+
*/
|
140
|
+
const JOB_BACKGROUND = 2;
|
141
|
+
|
142
|
+
/**
|
143
|
+
* High priority job
|
144
|
+
*
|
145
|
+
* @var integer JOB_HIGH
|
146
|
+
*/
|
147
|
+
const JOB_HIGH = 2;
|
148
|
+
|
149
|
+
/**
|
150
|
+
* Callback of type complete
|
151
|
+
*
|
152
|
+
* The callback provided should be ran when the task has been completed. It
|
153
|
+
* will be handed the result of the task as its only argument.
|
154
|
+
*
|
155
|
+
* @var integer TASK_COMPLETE
|
156
|
+
* @see Net_Gearman_Task::complete()
|
157
|
+
*/
|
158
|
+
const TASK_COMPLETE = 1;
|
159
|
+
|
160
|
+
/**
|
161
|
+
* Callback of type fail
|
162
|
+
*
|
163
|
+
* The callback provided should be ran when the task has been reported to
|
164
|
+
* have failed by Gearman. No arguments are provided.
|
165
|
+
*
|
166
|
+
* @var integer TASK_FAIL
|
167
|
+
* @see Net_Gearman_Task::fail()
|
168
|
+
*/
|
169
|
+
const TASK_FAIL = 2;
|
170
|
+
|
171
|
+
/**
|
172
|
+
* Callback of type status
|
173
|
+
*
|
174
|
+
* The callback provided should be ran whenever the status of the task has
|
175
|
+
* been updated. The numerator and denominator are passed as the only
|
176
|
+
* two arguments.
|
177
|
+
*
|
178
|
+
* @var integer TASK_STATUS
|
179
|
+
* @see Net_Gearman_Task::status()
|
180
|
+
*/
|
181
|
+
const TASK_STATUS = 3;
|
182
|
+
|
183
|
+
/**
|
184
|
+
* Constructor
|
185
|
+
*
|
186
|
+
* @param string $func Name of job to run
|
187
|
+
* @param array $arg List of arguments for job
|
188
|
+
* @param string $uniq The unique id of the job
|
189
|
+
* @param integer $type Type of job to run task as
|
190
|
+
*
|
191
|
+
* @return void
|
192
|
+
*/
|
193
|
+
public function __construct($func, $arg, $uniq = null,
|
194
|
+
$type = self::JOB_NORMAL)
|
195
|
+
{
|
196
|
+
$this->func = $func;
|
197
|
+
$this->arg = $arg;
|
198
|
+
|
199
|
+
if (is_null($uniq)) {
|
200
|
+
$this->uniq = md5($func . serialize($arg) . $type);
|
201
|
+
} else {
|
202
|
+
$this->uniq = $uniq;
|
203
|
+
}
|
204
|
+
|
205
|
+
$this->type = $type;
|
206
|
+
}
|
207
|
+
|
208
|
+
/**
|
209
|
+
* Attach a callback to this task
|
210
|
+
*
|
211
|
+
* @param callback $callback A valid PHP callback
|
212
|
+
* @param integer $type Type of callback
|
213
|
+
*
|
214
|
+
* @return void
|
215
|
+
* @throws Net_Gearman_Exception
|
216
|
+
*/
|
217
|
+
public function attachCallback($callback, $type = self::TASK_COMPLETE)
|
218
|
+
{
|
219
|
+
if (!is_callable($callback)) {
|
220
|
+
throw new Net_Gearman_Exception('Invalid callback specified');
|
221
|
+
}
|
222
|
+
|
223
|
+
$this->callback[$type][] = $callback;
|
224
|
+
}
|
225
|
+
|
226
|
+
/**
|
227
|
+
* Run the complete callbacks
|
228
|
+
*
|
229
|
+
* Complete callbacks are passed the name of the job, the handle of the
|
230
|
+
* job and the result of the job (in that order).
|
231
|
+
*
|
232
|
+
* @param object $result JSON decoded result passed back
|
233
|
+
*
|
234
|
+
* @return void
|
235
|
+
* @see Net_Gearman_Task::attachCallback()
|
236
|
+
*/
|
237
|
+
public function complete($result)
|
238
|
+
{
|
239
|
+
$this->finished = true;
|
240
|
+
$this->result = $result;
|
241
|
+
|
242
|
+
if (!count($this->callback[self::TASK_COMPLETE])) {
|
243
|
+
return;
|
244
|
+
}
|
245
|
+
|
246
|
+
foreach ($this->callback[self::TASK_COMPLETE] as $callback) {
|
247
|
+
call_user_func($callback, $this->func, $this->handle, $result);
|
248
|
+
}
|
249
|
+
}
|
250
|
+
|
251
|
+
/**
|
252
|
+
* Run the failure callbacks
|
253
|
+
*
|
254
|
+
* Failure callbacks are passed the name of the job and the handle of the
|
255
|
+
* job that failed (in that order).
|
256
|
+
*
|
257
|
+
* @return void
|
258
|
+
* @see Net_Gearman_Task::attachCallback()
|
259
|
+
*/
|
260
|
+
public function fail()
|
261
|
+
{
|
262
|
+
$this->finished = true;
|
263
|
+
if (!count($this->callback[self::TASK_FAIL])) {
|
264
|
+
return;
|
265
|
+
}
|
266
|
+
|
267
|
+
foreach ($this->callback[self::TASK_FAIL] as $callback) {
|
268
|
+
call_user_func($callback, $this);
|
269
|
+
}
|
270
|
+
}
|
271
|
+
|
272
|
+
/**
|
273
|
+
* Run the status callbacks
|
274
|
+
*
|
275
|
+
* Status callbacks are passed the name of the job, handle of the job and
|
276
|
+
* the numerator/denominator as the arguments (in that order).
|
277
|
+
*
|
278
|
+
* @param integer $numerator The numerator from the status
|
279
|
+
* @param integer $denominator The denominator from the status
|
280
|
+
*
|
281
|
+
* @return void
|
282
|
+
* @see Net_Gearman_Task::attachCallback()
|
283
|
+
*/
|
284
|
+
public function status($numerator, $denominator)
|
285
|
+
{
|
286
|
+
if (!count($this->callback[self::TASK_STATUS])) {
|
287
|
+
return;
|
288
|
+
}
|
289
|
+
|
290
|
+
foreach ($this->callback[self::TASK_STATUS] as $callback) {
|
291
|
+
call_user_func($callback,
|
292
|
+
$this->func,
|
293
|
+
$this->handle,
|
294
|
+
$numerator,
|
295
|
+
$denominator);
|
296
|
+
}
|
297
|
+
}
|
298
|
+
}
|
299
|
+
|
300
|
+
?>
|