1 | <?php |
||
22 | class MongoQueue extends Queue |
||
23 | { |
||
24 | /** |
||
25 | * Job resolver |
||
26 | * |
||
27 | * @var JobResolverInterface |
||
28 | */ |
||
29 | protected $resolver; |
||
30 | |||
31 | /** |
||
32 | * The mongo connection instance. |
||
33 | * |
||
34 | * @var MongoDriver |
||
35 | */ |
||
36 | protected $mongo; |
||
37 | |||
38 | /** |
||
39 | * The mongo collection that holds the jobs. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $collection; |
||
44 | |||
45 | /** |
||
46 | * The name of the default queue. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $queue = 'default'; |
||
51 | |||
52 | /** |
||
53 | * The expiration time of a job. |
||
54 | * |
||
55 | * @var int|null |
||
56 | */ |
||
57 | protected $expire = 60; |
||
58 | |||
59 | /** |
||
60 | * @var int |
||
61 | */ |
||
62 | protected $limit = 15; |
||
63 | |||
64 | /** |
||
65 | * Create a new mongo queue instance. |
||
66 | * |
||
67 | * @param JobResolverInterface $resolver |
||
68 | * @param MongoDriver $mongo |
||
69 | * @param string $collection |
||
70 | * @param string $queue |
||
71 | * @param int $expire |
||
72 | * @param int $limit |
||
73 | */ |
||
74 | public function __construct( |
||
89 | |||
90 | /** |
||
91 | * Push a new job onto the queue. |
||
92 | * |
||
93 | * @param string $job |
||
94 | * @param mixed $data |
||
95 | * @param string|null $queue |
||
96 | * |
||
97 | * @return mixed |
||
98 | */ |
||
99 | public function push(string $job, array $data = [], ?string $queue = null) |
||
103 | |||
104 | /** |
||
105 | * Pop the next job off of the queue. |
||
106 | * |
||
107 | * @param string|null $queue |
||
108 | * |
||
109 | * @return JobContractInterface|null |
||
110 | */ |
||
111 | public function pop(?string $queue = null): ?JobContractInterface |
||
121 | |||
122 | /** |
||
123 | * Check if job exists in the queue. |
||
124 | * |
||
125 | * @param string $job |
||
126 | * @param array $data |
||
127 | * @param string|null $queue |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | public function exists(string $job, array $data = [], ?string $queue = null): bool |
||
138 | |||
139 | /** |
||
140 | * Push a raw payload onto the queue. |
||
141 | * |
||
142 | * @param string $payload |
||
143 | * @param string|null $queue |
||
144 | * @param array $options |
||
145 | * |
||
146 | * @return mixed |
||
147 | */ |
||
148 | public function pushRaw(string $payload, ?string $queue = null, array $options = []) |
||
152 | |||
153 | /** |
||
154 | * Push a new job onto the queue after a delay. |
||
155 | * |
||
156 | * @param DateInterval|int $delay |
||
157 | * @param string $job |
||
158 | * @param array $data |
||
159 | * @param string|null $queue |
||
160 | * |
||
161 | * @return mixed |
||
162 | */ |
||
163 | public function later($delay, string $job, array $data = [], ?string $queue = null) |
||
167 | |||
168 | /** |
||
169 | * Push an array of jobs onto the queue. |
||
170 | * |
||
171 | * @param array $jobs |
||
172 | * @param mixed $data |
||
173 | * @param string|null $queue |
||
174 | * |
||
175 | * @return mixed |
||
176 | */ |
||
177 | public function bulk(array $jobs, array $data = [], ?string $queue = null) |
||
189 | |||
190 | /** |
||
191 | * Release a reserved job back onto the queue. |
||
192 | * |
||
193 | * @param JobContractInterface $job |
||
194 | * @param DateInterval|int $delay |
||
195 | * |
||
196 | * @return mixed |
||
197 | */ |
||
198 | public function release(JobContractInterface $job, $delay) |
||
202 | |||
203 | /** |
||
204 | * Get the next available job for the queue. |
||
205 | * |
||
206 | * @param string $queue |
||
207 | * @param string $id |
||
208 | * |
||
209 | * @return JobContractInterface|null |
||
210 | */ |
||
211 | public function getJobById(string $queue, string $id): ?JobContractInterface |
||
221 | |||
222 | /** |
||
223 | * Delete a reserved job from the queue. |
||
224 | * |
||
225 | * @param string $queue |
||
226 | * @param string $id |
||
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function deleteReserved(string $queue, string $id): bool |
||
245 | |||
246 | /** |
||
247 | * Get the expiration time in seconds. |
||
248 | * |
||
249 | * @return int|null |
||
250 | */ |
||
251 | public function getExpire() |
||
255 | |||
256 | /** |
||
257 | * Set the expiration time in seconds. |
||
258 | * |
||
259 | * @param int $seconds |
||
260 | */ |
||
261 | public function setExpire(int $seconds) |
||
265 | |||
266 | /** |
||
267 | * Get the size of the queue. |
||
268 | * |
||
269 | * @param string|null $queue |
||
270 | * |
||
271 | * @return int |
||
272 | */ |
||
273 | public function size(?string $queue = null): int |
||
281 | |||
282 | /** |
||
283 | * Check if can run process depend on limits |
||
284 | * |
||
285 | * @param JobContractInterface $job |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | public function canRunJob(JobContractInterface $job): bool |
||
296 | |||
297 | /** |
||
298 | * Mark the given job ID as reserved. |
||
299 | * |
||
300 | * @param JobContractInterface $job |
||
301 | */ |
||
302 | public function markJobAsReserved(JobContractInterface $job) |
||
315 | |||
316 | /** |
||
317 | * Push a raw payload to the mongo with a given delay. |
||
318 | * |
||
319 | * @param DateInterval|int $delay |
||
320 | * @param string|null $queue |
||
321 | * @param string $payload |
||
322 | * @param int $attempts |
||
323 | * |
||
324 | * @return mixed |
||
325 | */ |
||
326 | protected function pushToDatabase($delay, $queue, $payload, $attempts = 0) |
||
332 | |||
333 | /** |
||
334 | * Get the "available at" UNIX timestamp. |
||
335 | * |
||
336 | * @param DateInterval|int $delay |
||
337 | * |
||
338 | * @return int |
||
339 | */ |
||
340 | protected function getAvailableAt($delay = 0) |
||
346 | |||
347 | /** |
||
348 | * Get the queue or return the default. |
||
349 | * |
||
350 | * @param string|null $queue |
||
351 | * |
||
352 | * @return string |
||
353 | */ |
||
354 | protected function getQueue($queue) |
||
358 | |||
359 | /** |
||
360 | * Get the next available job for the queue. |
||
361 | * |
||
362 | * @param string|null $queue |
||
363 | * |
||
364 | * @return JobContractInterface|null |
||
365 | */ |
||
366 | protected function getNextAvailableJob($queue) |
||
381 | |||
382 | /** |
||
383 | * Create an array to insert for the given job. |
||
384 | * |
||
385 | * @param string|null $queue |
||
386 | * @param string $payload |
||
387 | * @param int $availableAt |
||
388 | * @param int $attempts |
||
389 | * |
||
390 | * @return array |
||
391 | */ |
||
392 | protected function buildDatabaseRecord($queue, $payload, $availableAt, $attempts = 0) |
||
404 | |||
405 | /** |
||
406 | * Get available jobs |
||
407 | * |
||
408 | * @return array |
||
409 | */ |
||
410 | protected function isAvailable() |
||
417 | |||
418 | /** |
||
419 | * Get reserved but expired by time jobs |
||
420 | * |
||
421 | * @return array |
||
422 | */ |
||
423 | protected function isReservedButExpired() |
||
429 | |||
430 | /** |
||
431 | * Get queue collection |
||
432 | * |
||
433 | * @return Collection Mongo collection instance |
||
434 | */ |
||
435 | protected function getCollection(): Collection |
||
439 | |||
440 | /** |
||
441 | * Build job from database record |
||
442 | * |
||
443 | * @param $data |
||
444 | * |
||
445 | * @return Job |
||
446 | */ |
||
447 | protected function buildJob($data): Job |
||
459 | } |
||
460 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: