1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\QueueBundle\Service; |
4
|
|
|
|
5
|
|
|
use SfCod\QueueBundle\Base\JobQueueInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* JobQueue service |
9
|
|
|
* |
10
|
|
|
* @author Virchenko Maksim <[email protected]> |
11
|
|
|
* @author Orlov Alexey <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class JobQueue implements JobQueueInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* QueueManager instance |
17
|
|
|
* |
18
|
|
|
* @var QueueManager |
19
|
|
|
*/ |
20
|
|
|
protected $manager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* JobQueue constructor. |
24
|
|
|
* |
25
|
|
|
* @param QueueManager $manager |
26
|
|
|
* |
27
|
|
|
* @internal param array $config |
28
|
|
|
*/ |
29
|
|
|
public function __construct(QueueManager $manager) |
30
|
|
|
{ |
31
|
|
|
$this->manager = $manager; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Push new job to queue |
36
|
|
|
* |
37
|
|
|
* @author Virchenko Maksim <[email protected]> |
38
|
|
|
* |
39
|
|
|
* @param string $job |
40
|
|
|
* @param array $data |
41
|
|
|
* @param string $queue |
42
|
|
|
* @param string $connection |
43
|
|
|
* |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function push(string $job, array $data = [], string $queue = 'default', string $connection = 'default') |
47
|
|
|
{ |
48
|
|
|
return $this->manager->push($job, $data, $queue, $connection); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Push new job to queue if this job is not exist |
53
|
|
|
* |
54
|
|
|
* @author Virchenko Maksim <[email protected]> |
55
|
|
|
* |
56
|
|
|
* @param string $job |
57
|
|
|
* @param array $data |
58
|
|
|
* @param string $queue |
59
|
|
|
* @param string $connection |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function pushUnique(string $job, array $data = [], string $queue = 'default', string $connection = 'default') |
64
|
|
|
{ |
65
|
|
|
if (false === $this->manager->connection($connection)->exists($job, $data, $queue)) { |
66
|
|
|
return $this->manager->push($job, $data, $queue, $connection); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Push a new an array of jobs onto the queue. |
74
|
|
|
* |
75
|
|
|
* @param array $jobs |
76
|
|
|
* @param mixed $data |
77
|
|
|
* @param string $queue |
78
|
|
|
* @param string $connection |
79
|
|
|
* |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
public function bulk(array $jobs, array $data = [], string $queue = 'default', string $connection = 'default') |
83
|
|
|
{ |
84
|
|
|
return $this->manager->bulk($jobs, $data, $queue, $connection); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Push a new job onto the queue after a delay. |
89
|
|
|
* |
90
|
|
|
* @param \DateInterval|int $delay |
91
|
|
|
* @param string $job |
92
|
|
|
* @param mixed $data |
93
|
|
|
* @param string $queue |
94
|
|
|
* @param string $connection |
95
|
|
|
* |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
public function later($delay, string $job, array $data = [], string $queue = 'default', string $connection = 'default') |
99
|
|
|
{ |
100
|
|
|
return $this->manager->later($delay, $job, $data, $queue, $connection); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Push a new job into the queue after a delay if job does not exist. |
105
|
|
|
* |
106
|
|
|
* @param \DateInterval|int $delay |
107
|
|
|
* @param string $job |
108
|
|
|
* @param mixed $data |
109
|
|
|
* @param string $queue |
110
|
|
|
* @param string $connection |
111
|
|
|
* |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
|
|
public function laterUnique($delay, string $job, array $data = [], string $queue = 'default', string $connection = 'default') |
115
|
|
|
{ |
116
|
|
|
if (false === $this->manager->connection($connection)->exists($job, $data, $queue)) { |
117
|
|
|
return $this->manager->later($delay, $job, $data, $queue, $connection); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return null; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: