-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJobQueue.php
186 lines (167 loc) · 4.36 KB
/
JobQueue.php
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
<?php
namespace yiicod\jobqueue;
use Illuminate\Queue\Capsule\Manager;
use Illuminate\Queue\Connectors\ConnectorInterface;
use yii\base\BootstrapInterface;
use yii\base\Component;
use yiicod\jobqueue\connectors\MongoThreadConnector;
/**
* Yii component for laravel 5 queues to work with mongodb
*
* @author Orlov Alexey <aaorlov88@gmail.com>
* @author Virchenko Maksim <muslim1992@gmail.com>
*/
class JobQueue extends Component implements BootstrapInterface
{
/**
* Available connections
*
* @var array
*/
public $connections = [
'thread' => [
'driver' => 'mongo-thread',
'table' => 'yii_jobs_thread',
'queue' => 'default',
'expire' => 60,
'limit' => 15, // Or 1
'connection' => 'mongodb',
],
];
/**
* Encryption key
*
* @var string
*/
public $privateKey = 'rc5lgpue80sr17nx';
/**
* Manager instance
*
* @var Manager
*/
private $manager = null;
/**
* Initialize
*
* @param \yii\base\Application $app
*/
public function bootstrap($app)
{
$this->connect();
}
/**
* Connect queue manager for mongo database
*
* @return Manager
*/
protected function connect()
{
/* @var Manager manager */
$this->manager = new Manager();
$this->addConnector('mongo-thread', new MongoThreadConnector());
foreach ($this->connections as $name => $params) {
$this->manager->addConnection($params, $name);
}
//Set as global to access
$this->manager->setAsGlobal();
}
/**
* Add connector
*
* @param string $name
* @param Closure $resolver
*/
public function addConnector(string $name, ConnectorInterface $connector)
{
$this->manager->addConnector($name, function () use ($connector) {
return $connector;
});
}
/**
* Get queue manager instance
*
* @return mixed
*/
public function getQueueManager()
{
return $this->manager->getQueueManager();
}
/**
* Push new job to queue
*
* @param mixed $job
* @param array $data
* @param string $queue
* @param string $connection
*
* @return mixed
*/
public static function push($job, $data = [], $queue = 'default', $connection = 'default')
{
return Manager::push($job, $data, $queue, $connection);
}
/**
* Push new job to queue if this job is not exist
*
* @param mixed $job
* @param array $data
* @param string $queue
* @param string $connection
*
* @return mixed
*/
public static function pushUnique($job, $data = [], $queue = 'default', $connection = 'default')
{
if (false === Manager::connection($connection)->exists($job, $data, $queue)) {
return Manager::push($job, $data, $queue, $connection);
}
return null;
}
/**
* Push a new an array of jobs onto the queue.
*
* @param array $jobs
* @param mixed $data
* @param string $queue
* @param string $connection
*
* @return mixed
*/
public static function bulk($jobs, $data = '', $queue = null, $connection = null)
{
return Manager::bulk($jobs, $data, $queue, $connection);
}
/**
* Push a new job onto the queue after a delay.
*
* @param \DateTime|int $delay
* @param string $job
* @param mixed $data
* @param string $queue
* @param string $connection
*
* @return mixed
*/
public static function later($delay, $job, $data = '', $queue = null, $connection = null)
{
return Manager::later($delay, $job, $data, $queue, $connection);
}
/**
* Push a new job into the queue after a delay if job does not exist.
*
* @param \DateTime|int $delay
* @param string $job
* @param mixed $data
* @param string $queue
* @param string $connection
*
* @return mixed
*/
public static function laterUnique($delay, $job, $data = '', $queue = null, $connection = null)
{
if (false === Manager::connection($connection)->exists($job, $data, $queue)) {
return Manager::later($delay, $job, $data, $queue, $connection);
}
return null;
}
}