This repository was archived by the owner on Oct 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorker.php
More file actions
392 lines (348 loc) · 9.36 KB
/
Worker.php
File metadata and controls
392 lines (348 loc) · 9.36 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
namespace yiicod\laravel5queue;
use Exception;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Contracts\Cache\Repository as CacheContract;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Contracts\Queue\Queue;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\WorkerStopping;
use Illuminate\Queue\Failed\FailedJobProviderInterface;
use Illuminate\Queue\QueueManager;
use Throwable;
use yiicod\laravel5queue\handlers\FatalThrowableError;
/**
* Worker for laravel queues
*
* @author Virchenko Maksim <muslim1992@gmail.com>
*/
class Worker
{
/**
* The queue manager instance.
*
* @var QueueManager
*/
protected $manager;
/**
* The failed job provider implementation.
*
* @var FailedJobProviderInterface
*/
protected $failer;
/**
* The event dispatcher instance.
*
* @var Dispatcher
*/
protected $events;
/**
* The cache repository implementation.
*
* @var Repository
*/
protected $cache;
/**
* The exception handler instance.
*
* @var \Illuminate\Foundation\Exceptions\Handler
*/
protected $exceptions;
/**
* Create a new queue worker.
*
* @param QueueManager $manager
* @param FailedJobProviderInterface $failer
* @param ExceptionHandler $exceptions
*/
public function __construct(QueueManager $manager,
FailedJobProviderInterface $failer,
ExceptionHandler $exceptions)
{
$this->manager = $manager;
$this->failer = $failer;
$this->exceptions = $exceptions;
}
/**
* Listen to the given queue in a loop.
*
* @param string $connectionName
* @param string $queue
* @param int $delay
* @param int $memory
* @param int $sleep
* @param int $maxTries
*/
public function daemon($connectionName, $queue = null, $delay = 0, $memory = 128, $sleep = 3, $maxTries = 0)
{
while (true) {
$this->runNextJobForDaemon(
$connectionName, $queue, $delay, $sleep, $maxTries
);
if ($this->memoryExceeded($memory)) {
$this->stop();
}
}
}
/**
* Run the next job for the daemon worker.
*
* @param string $connectionName
* @param string $queue
* @param int $delay
* @param int $sleep
* @param int $maxTries
*
* @return void
*/
protected function runNextJobForDaemon($connectionName, $queue, $delay, $sleep, $maxTries)
{
try {
$this->pop($connectionName, $queue, $delay, $sleep, $maxTries);
} catch (Exception $e) {
if ($this->exceptions) {
$this->exceptions->report($e);
}
} catch (Throwable $e) {
if ($this->exceptions) {
$this->exceptions->report(new FatalThrowableError($e));
}
}
}
/**
* Listen to the given queue.
*
* @param string $connectionName
* @param string $queue
* @param int $delay
* @param int $sleep
* @param int $maxTries
*
* @return array
*/
public function pop($connectionName, $queue = null, $delay = 0, $sleep = 3, $maxTries = 0)
{
$connection = $this->manager->connection($connectionName);
$job = $this->getNextJob($connection, $queue);
//If job equal TRUE this this is async job was run
if (true === $job) {
return;
}
// If we're able to pull a job off of the stack, we will process it and
// then immediately return back out. If there is no job on the queue
// we will "sleep" the worker for the specified number of seconds.
if (false === is_null($job)) {
return $this->process(
$this->manager->getName($connectionName), $job, $maxTries, $delay
);
}
$this->sleep($sleep);
return ['job' => null, 'failed' => false];
}
/**
* Get the next job from the queue connection.
*
* @param Queue $connection
* @param string $queue
*
* @return Job|null
*/
protected function getNextJob($connection, $queue)
{
if (is_null($queue)) {
return $connection->pop();
}
foreach (explode(',', $queue) as $queue) {
if (!is_null($job = $connection->pop($queue))) {
return $job;
}
}
return null;
}
/**
* Process a given job from the queue.
*
* @param string $connection
* @param Job $job
* @param int $maxTries
* @param int $delay
*
* @return array|null
*
* @throws \Throwable
*/
public function process($connection, Job $job, $maxTries = 0, $delay = 0)
{
if ($maxTries > 0 && $job->attempts() > $maxTries) {
return $this->logFailedJob($connection, $job);
}
try {
// First we will fire off the job. Once it is done we will see if it will
// be auto-deleted after processing and if so we will go ahead and run
// the delete method on the job. Otherwise we will just keep moving.
$job->fire();
$this->raiseAfterJobEvent($connection, $job);
return ['job' => $job, 'failed' => false];
} catch (Exception $e) {
// If we catch an exception, we will attempt to release the job back onto
// the queue so it is not lost. This will let is be retried at a later
// time by another listener (or the same one). We will do that here.
if (!$job->isDeleted()) {
$job->release($delay);
}
throw $e;
} catch (Throwable $e) {
if (!$job->isDeleted()) {
$job->release($delay);
}
throw $e;
}
}
/**
* Raise the after queue job event.
*
* @param string $connection
* @param Job $job
*
* @return void
*/
protected function raiseAfterJobEvent($connection, Job $job)
{
if ($this->events) {
$data = json_decode($job->getRawBody(), true);
$this->events->fire(new JobProcessed($connection, $job, $data));
}
}
/**
* Log a failed job into storage.
*
* @param string $connection
* @param Job $job
*
* @return array
*/
protected function logFailedJob($connection, Job $job)
{
if ($this->failer) {
$this->failer->log($connection, $job->getQueue(), $job->getRawBody());
$job->delete();
$job->failed();
$this->raiseFailedJobEvent($connection, $job);
}
return ['job' => $job, 'failed' => true];
}
/**
* Raise the failed queue job event.
*
* @param string $connection
* @param Job $job
*
* @return void
*/
protected function raiseFailedJobEvent($connection, Job $job)
{
if ($this->events) {
$data = json_decode($job->getRawBody(), true);
$this->events->fire(new JobFailed($connection, $job, $data));
}
}
/**
* Determine if the memory limit has been exceeded.
*
* @param int $memoryLimit
*
* @return bool
*/
public function memoryExceeded($memoryLimit)
{
return (memory_get_usage() / 1024 / 1024) >= $memoryLimit;
}
/**
* Stop listening and bail out of the script.
*
* @return void
*/
public function stop()
{
$this->events->fire(new WorkerStopping);
die;
}
/**
* Sleep the script for a given number of seconds.
*
* @param int $seconds
*
* @return void
*/
public function sleep($seconds)
{
sleep($seconds);
}
/**
* Get the last queue restart timestamp, or null.
*
* @return int|null
*/
protected function getTimestampOfLastQueueRestart()
{
if ($this->cache) {
return $this->cache->get('illuminate:queue:restart');
}
}
/**
* Determine if the queue worker should restart.
*
* @param int|null $lastRestart
*
* @return bool
*/
protected function queueShouldRestart($lastRestart)
{
return $this->getTimestampOfLastQueueRestart() != $lastRestart;
}
/**
* Set the exception handler to use in Daemon mode.
*
* @param ExceptionHandler $handler
*
* @return void
*/
public function setDaemonExceptionHandler(ExceptionHandler $handler)
{
$this->exceptions = $handler;
}
/**
* Set the cache repository implementation.
*
* @param Repository $cache
*
* @return void
*/
public function setCache(CacheContract $cache)
{
$this->cache = $cache;
}
/**
* Get the queue manager instance.
*
* @return QueueManager
*/
public function getManager()
{
return $this->manager;
}
/**
* Set the queue manager instance.
*
* @param QueueManager $manager
*
* @return void
*/
public function setManager(QueueManager $manager)
{
$this->manager = $manager;
}
}