-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathJobHandler.php
More file actions
executable file
·362 lines (311 loc) · 8.06 KB
/
JobHandler.php
File metadata and controls
executable file
·362 lines (311 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
namespace Resque;
use Resque\Job\PID;
use Resque\Job\Status;
use Resque\Exceptions\DoNotPerformException;
use Resque\Exceptions\ResqueException;
use Resque\Job\FactoryInterface;
use Resque\Job\Factory;
use Resque\Job\Job;
use Error;
/**
* Resque job.
*
* @package Resque/JobHandler
* @author Chris Boulton <chris@bigcommerce.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class JobHandler
{
/**
* @var string The name of the queue that this job belongs to.
*/
public $queue;
/**
* @var \Resque\Worker\ResqueWorker Instance of the Resque worker running this job.
*/
public $worker;
/**
* @var array Array containing details of the job.
*/
public $payload;
/**
* @var float Timestamp of when the data was popped from redis.
*/
public $popTime;
/**
* @var float Timestamp of when the job started processing.
*/
public $startTime;
/**
* @var float Timestamp of when the job finished processing.
*/
public $endTime;
/**
* @var Job Instance of the class performing work for this job.
*/
private $instance;
/**
* @var \Resque\Job\FactoryInterface
*/
private $jobFactory;
/**
* Instantiate a new instance of a job.
*
* @param string $queue The queue that the job belongs to.
* @param array $payload array containing details of the job.
*/
public function __construct($queue, $payload)
{
$this->queue = $queue;
$this->payload = $payload;
$this->popTime = microtime(true);
if (!isset($this->payload['id'])) {
$this->payload['id'] = Resque::generateJobId();
}
}
/**
* Create a new job and save it to the specified queue.
*
* @param string $queue The name of the queue to place the job in.
* @param class-string<Job> $class The name of the class that contains the code to execute the job.
* @param array $args Any optional arguments that should be passed when the job is executed.
* @param boolean $monitor Set to true to be able to monitor the status of a job.
* @param string $id Unique identifier for tracking the job. Generated if not supplied.
* @param string $prefix The prefix needs to be set for the status key
*
* @return string
*/
public static function create($queue, $class, array $args = [], $monitor = false, $id = null, $prefix = "")
{
if (is_null($id)) {
$id = Resque::generateJobId();
}
Resque::push($queue, array(
'class' => $class,
'args' => array($args),
'id' => $id,
'prefix' => $prefix,
'queue_time' => microtime(true),
));
if ($monitor) {
Status::create($id, $prefix);
}
return $id;
}
/**
* Find the next available job from the specified queue and return an
* instance of JobHandler for it.
*
* @param string $queue The name of the queue to check for a job in.
* @return false|object Null when there aren't any waiting jobs, instance of Resque\JobHandler when a job was found.
*/
public static function reserve($queue)
{
$payload = Resque::pop($queue);
if (!is_array($payload)) {
return false;
}
return new JobHandler($queue, $payload);
}
/**
* Find the next available job from the specified queues using blocking list pop
* and return an instance of JobHandler for it.
*
* @param array $queues
* @param int $timeout
* @return false|object Null when there aren't any waiting jobs, instance of Resque\JobHandler when a job was found.
*/
public static function reserveBlocking(array $queues, $timeout = null)
{
$item = Resque::blpop($queues, $timeout);
if (!is_array($item)) {
return false;
}
return new JobHandler($item['queue'], $item['payload']);
}
/**
* Update the status of the current job.
*
* @param int $status Status constant from Resque\Job\Status indicating the current status of a job.
*/
public function updateStatus($status, $result = null)
{
if (empty($this->payload['id'])) {
return;
}
$statusInstance = new Status($this->payload['id'], $this->getPrefix());
$statusInstance->update($status, $result);
}
/**
* Return the status of the current job.
*
* @return int|null The status of the job as one of the Resque\Job\Status constants
* or null if job is not being tracked.
*/
public function getStatus()
{
if (empty($this->payload['id'])) {
return null;
}
$status = new Status($this->payload['id'], $this->getPrefix());
return $status->get();
}
/**
* Get the arguments supplied to this job.
*
* @return array Array of arguments.
*/
public function getArguments(): array
{
if (!isset($this->payload['args'])) {
return array();
}
return $this->payload['args'][0];
}
/**
* Get the instantiated object for this job that will be performing work.
* @return Job Instance of the object that this job belongs to.
* @throws ResqueException
*/
public function getInstance(): Job
{
if (isset($this->instance)) {
return $this->instance;
}
$this->instance = $this->getJobFactory()
->create($this->payload['class'], $this->getArguments(), $this->queue);
$this->instance->job = $this;
$this->instance->jobID = $this->payload['id'];
return $this->instance;
}
/**
* Actually execute a job by calling the perform method on the class
* associated with the job with the supplied arguments.
*
* @return mixed Return of perform, or false if DoNotPerformException was thrown
* @throws ResqueException When the job's class could not be found.
*/
public function perform()
{
$result = true;
try {
Event::trigger('beforePerform', $this);
$this->startTime = microtime(true);
$instance = $this->getInstance();
$instance->setUp();
$result = $instance->perform();
$instance->tearDown();
$this->endTime = microtime(true);
Event::trigger('afterPerform', $this);
} catch (DoNotPerformException $e) {
// beforePerform/setUp have said don't perform this job. Return.
$result = false;
}
return $result;
}
/**
* Mark the current job as having failed.
*
* @param $exception
*/
public function fail($exception)
{
$this->endTime = microtime(true);
Event::trigger('onFailure', array(
'exception' => $exception,
'job' => $this,
));
$this->updateStatus(Status::STATUS_FAILED);
if ($exception instanceof Error) {
FailureHandler::createFromError(
$this->payload,
$exception,
$this->worker,
$this->queue
);
} else {
FailureHandler::create(
$this->payload,
$exception,
$this->worker,
$this->queue
);
}
if (!empty($this->payload['id'])) {
PID::del($this->payload['id']);
}
Stat::incr('failed');
Stat::incr('failed:' . $this->worker);
}
/**
* Re-queue the current job.
* @return string
*/
public function recreate()
{
$monitor = false;
if (!empty($this->payload['id'])) {
$status = new Status($this->payload['id'], $this->getPrefix());
if ($status->isTracking()) {
$monitor = true;
}
}
return self::create(
$this->queue,
$this->payload['class'],
$this->getArguments(),
$monitor,
null,
$this->getPrefix()
);
}
/**
* Generate a string representation used to describe the current job.
*
* @return string The string representation of the job.
*/
public function __toString()
{
$name = array(
'Job{' . $this->queue . '}'
);
if (!empty($this->payload['id'])) {
$name[] = 'ID: ' . $this->payload['id'];
}
$name[] = $this->payload['class'];
if (!empty($this->payload['args'])) {
$name[] = json_encode($this->payload['args']);
}
return '(' . implode(' | ', $name) . ')';
}
/**
* @param \Resque\Job\FactoryInterface $jobFactory
* @return \Resque\JobHandler
*/
public function setJobFactory(FactoryInterface $jobFactory)
{
$this->jobFactory = $jobFactory;
return $this;
}
/**
* @return \Resque\Job\FactoryInterface
*/
public function getJobFactory(): FactoryInterface
{
if ($this->jobFactory === null) {
$this->jobFactory = new Factory();
}
return $this->jobFactory;
}
/**
* @return string
*/
private function getPrefix()
{
if (isset($this->payload['prefix'])) {
return $this->payload['prefix'];
}
return '';
}
}