This repository has been 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
/
JobQueue.php
175 lines (158 loc) · 4.34 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
<?php
namespace yiicod\laravel5queue;
use CApplicationComponent;
use Illuminate\Encryption\Encrypter;
use Illuminate\Queue\Capsule\Manager;
use Yii;
use yiicod\laravel5queue\connectors\AsyncMongoConnector;
use yiicod\laravel5queue\connectors\MongoConnector;
/**
* Yii component for laravel 5 queues to work with mongodb
*
* @author Virchenko Maksim <[email protected]>
*/
class JobQueue extends CApplicationComponent
{
/**
* Available connections
*
* @var array
*/
public $connections = [
'default' => [
'driver' => 'mongoQueue',
'table' => 'YiiJobs',
'queue' => 'default',
'expire' => 60,
],
'async' => [
'driver' => 'asyncMongoQueue',
'table' => 'YiiJobsAsync',
'queue' => 'default',
'expire' => 60,
'limit' => 15,
'yiicAlias' => 'application',
'connectionName' => 'async'
]
];
/**
* Private key
*
* @var string
*/
public $privateKey = 'rc5lgpue80sr17nx';
/**
* Manager instance
*
* @var
*/
private $queueManager;
/**
* Initialize
*
* @author Virchenko Maksim <[email protected]>
*/
public function init()
{
parent::init();
$this->connect();
}
/**
* Connect queue manager for mongo database
*/
protected function connect()
{
$this->queueManager = new Manager();
//Some drivers need it
$this->queueManager->getContainer()->bind('encrypter', function () {
return new Encrypter($this->privateKey);
});
//One more bind for closure functions
$this->queueManager->getContainer()->bind('Illuminate\Contracts\Encryption\Encrypter', 'encrypter');
//Connector to successful jobs
$this->queueManager->addConnector('mongoQueue', function () {
return new MongoConnector(Yii::app()->mongodb, 'YiiJobsSuccessed');
});
$this->queueManager->addConnector('asyncMongoQueue', function () {
return new AsyncMongoConnector(Yii::app()->mongodb, 'YiiJobsSuccessed');
});
foreach ($this->connections as $name => $params) {
$this->queueManager->addConnection($params, $name);
}
//Set as global to access
$this->queueManager->setAsGlobal();
}
/**
* Get queue manager
*
* @return mixed
*/
public function getManager()
{
return $this->queueManager;
}
/**
* Push new job to queue
*
* @author Virchenko Maksim <[email protected]>
*
* @param mixed $handler
* @param array $data
* @param string $queue
* @param string $connection
*
* @return mixed
*/
public static function push($handler, $data = [], $queue = 'default', $connection = 'default')
{
return Manager::push($handler, $data, $queue, $connection);
}
/**
* Push new job to queue if this job is not exist
*
* @author Virchenko Maksim <[email protected]>
*
* @param mixed $handler
* @param array $data
* @param string $queue
* @param string $connection
*
* @return mixed
*/
public static function pushUnique($handler, $data = [], $queue = 'default', $connection = 'default')
{
if (false === Manager::connection($connection)->exists($handler, $data, $queue)) {
return Manager::push($handler, $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);
}
}