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
|
<?php namespace Illuminate\Queue;
use Illuminate\Queue\Jobs\Job; use Illuminate\Events\Dispatcher; use Illuminate\Queue\Failed\FailedJobProviderInterface;
class Worker {
/** * The queue manager instance. * * @var \Illuminate\Queue\QueueManager */ protected $manager;
/** * The failed job provider implementation. * * @var \Illuminate\Queue\Failed\FailedJobProviderInterface */ protected $failer;
/** * The event dispatcher instance. * * @var \Illuminate\Events\Dispatcher */ protected $events;
/** * Create a new queue worker. * * @param \Illuminate\Queue\QueueManager $manager * @param \Illuminate\Queue\Failed\FailedJobProviderInterface $failer * @param \Illuminate\Events\Dispatcher $events * @return void */ public function __construct(QueueManager $manager, FailedJobProviderInterface $failer = null, Dispatcher $events = null) { $this->failer = $failer; $this->events = $events; $this->manager = $manager; }
/** * Listen to the given queue. * * @param string $connectionName * @param string $queue * @param int $delay * @param int $memory * @param int $sleep * @param int $maxTries * @return void */ public function pop($connectionName, $queue = null, $delay = 0, $memory = 128, $sleep = 3, $maxTries = 0) { $connection = $this->manager->connection($connectionName);
$job = $this->getNextJob($connection, $queue);
// If we're able to pull a job off of the stack, we will process it and // then make sure we are not exceeding our memory limits for the run // which is to protect against run-away memory leakages from here. if ( ! is_null($job)) { $this->process( $this->manager->getName($connectionName), $job, $maxTries, $delay ); } else { $this->sleep($sleep); } }
/** * Get the next job from the queue connection. * * @param \Illuminate\Queue\Queue $connection * @param string $queue * @return \Illuminate\Queue\Jobs\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; } }
/** * Process a given job from the queue. * * @param string $connection * @param \Illuminate\Queue\Jobs\Job $job * @param int $maxTries * @param int $delay * @return void * * @throws \Exception */ 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();
if ($job->autoDelete()) $job->delete(); }
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; } }
/** * Log a failed job into storage. * * @param string $connection * @param \Illuminate\Queue\Jobs\Job $job * @return void */ protected function logFailedJob($connection, Job $job) { if ($this->failer) { $this->failer->log($connection, $job->getQueue(), $job->getRawBody());
$job->delete();
$this->raiseFailedJobEvent($connection, $job); } }
/** * Raise the failed queue job event. * * @param string $connection * @param \Illuminate\Queue\Jobs\Job $job * @return void */ protected function raiseFailedJobEvent($connection, Job $job) { if ($this->events) { $data = json_decode($job->getRawBody(), true);
$this->events->fire('illuminate.queue.failed', array($connection, $job, $data)); } }
/** * Sleep the script for a given number of seconds. * * @param int $seconds * @return void */ public function sleep($seconds) { sleep($seconds); }
/** * Get the queue manager instance. * * @return \Illuminate\Queue\QueueManager */ public function getManager() { return $this->manager; }
/** * Set the queue manager instance. * * @param \Illuminate\Queue\QueueManager $manager * @return void */ public function setManager(QueueManager $manager) { $this->manager = $manager; }
}
|