-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRedisQueue.php
More file actions
351 lines (281 loc) · 8.43 KB
/
RedisQueue.php
File metadata and controls
351 lines (281 loc) · 8.43 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* Created by PhpStorm.
* User: woojean
* Date: 2018/2/28
* Time: 10:04
*/
namespace Look\Queue\RedisQueue;
/**
* @property \Redis redis
*/
class RedisQueue
{
const PREFIX = 'RQ';
const ERROR_QUEUE_NAME_EMPTY = 'Queue name can not be empty!';
const PROCESSING_INDEX = 'PI';
const DATA_KEY = 'DK';
public $queueName = '';
public $redis = null;
public $retryTimes = 3;
public $waitTime = 3;
/**
* RedisQueue constructor.
* @param $queueName
* @param $redisConfig
* [
* 'host' => '127.0.0.1',
* 'port' => '6379',
* 'index' => 0,
* ]
* @param $retryTimes
* @param $waitTime
* @throws RedisQueueException
*/
public function __construct($queueName, $redisConfig, $retryTimes = 3, $waitTime = 3)
{
if (empty($queueName)) {
throw new RedisQueueException('getCurrentIndex:' . self::ERROR_QUEUE_NAME_EMPTY);
}
if (empty($redisConfig)) {
throw new RedisQueueException('Redis config name can not be empty!');
}
$this->queueName = $queueName;
$this->retryTimes = $retryTimes;
$this->waitTime = $waitTime;
$ret = $this->init($redisConfig);
if (false === $ret) {
throw new RedisQueueException('Queue init failed!');
}
}
public function getIndexListName()
{
// index list
return self::PREFIX . ':IL:' . $this->queueName;
}
public function getBlockedListName()
{
// blocked list
return self::PREFIX . ':BL:' . $this->queueName;
}
public function getDataHashName()
{
// data hash
return self::PREFIX . ':DH:' . $this->queueName;
}
public function getBlockedTimesHashName()
{
// blocked times hash
return self::PREFIX . ':BTH' . $this->queueName;
}
public function getDataKey($data)
{
return $data[self::DATA_KEY];
}
public function setDataKey($data, $index)
{
$data[self::DATA_KEY] = $index;
return $data;
}
public function getProcessingIndexName($data)
{
$dataKey = $this->getDataKey($data);
$keyName = self::PREFIX . ':' . self::PROCESSING_INDEX . ':' . $this->queueName . ':' . $dataKey;
return $keyName;
}
public function getProcessingIndex($data)
{
$keyName = $this->getProcessingIndexName($data);
return $this->redis->get($keyName);
}
public function setProcessingIndex($data)
{
$keyName = $this->getProcessingIndexName($data);
$index = $this->getDataKey($data);
return $this->redis->set($keyName, $index);
}
public function removeProcessingIndex($data)
{
$keyName = $this->getProcessingIndexName($data);
return $this->redis->del($keyName);
}
public function removeAllProcessingIndex()
{
$pattern = self::PREFIX . ':' . self::PROCESSING_INDEX . ':' . $this->queueName . ':*';
$keys = $this->redis->keys($pattern);
foreach ($keys as $key) {
$this->redis->del($key);
}
}
public function init($redisConfig)
{
$this->redis = new \Redis();
$this->redis->connect($redisConfig['host'], $redisConfig['port']);
if(!empty($redisConfig['auth'])){
$this->redis->auth($redisConfig['auth']);
}
if(!empty($redisConfig['index'])){
$this->redis->select($redisConfig['index']);
}
return True;
}
public function genGuid()
{
return uniqid($this->queueName . '_');
}
public function addIndex($index)
{
$ret = $this->redis->lpush($this->getIndexListName(), $index);
return $ret;
}
public function transferToBlocked($index)
{
$ret = $this->redis->lpush($this->getBlockedListName(), $index);
return $ret;
}
public function getIndex()
{
$ret = $this->redis->rPop($this->getIndexListName());
return $ret;
}
public function getData($index)
{
$ret = $this->redis->hGet($this->getDataHashName(), $index);
$data = json_decode($ret, true);
return $data;
}
public function addData($index, $data)
{
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
$ret = $this->redis->hSet($this->getDataHashName(), $index, $data);
return $ret;
}
public function removeData($data)
{
$index = $this->getDataKey($data);
$ret = $this->redis->hDel($this->getDataHashName(), $index);
return $ret;
}
public function getBlockedTimes($processingIndex)
{
$ret = $this->redis->hGet($this->getBlockedTimesHashName(), $processingIndex);
return intval($ret);
}
public function addBlocked($processingIndex)
{
$blockedTime = $this->redis->hGet($this->getBlockedTimesHashName(), $processingIndex);
$blockedTime += 1;
$ret = $this->redis->hSet($this->getBlockedTimesHashName(), $processingIndex, $blockedTime);
return $ret;
}
public function getBlockedIndex()
{
$ret = $this->redis->rPop($this->getBlockedListName());
return $ret;
}
public function removeBlockedTimes($index)
{
$ret = $this->redis->hDel($this->getBlockedTimesHashName(), $index);
return $ret;
}
// ==== API ====================================================================
// add new message
public function add($data)
{
// make index
$index = $this->genGuid();
// add index
$ret = $this->addIndex($index);
if (false == $ret) {
throw new RedisQueueException('Add index failed!');
}
// add data
$ret = $this->addData($index, $data);
if (false == $ret) {
throw new RedisQueueException('Add data failed!');
}
return $index;
}
// get message
public function get()
{
// get index
$index = $this->getIndex();
if (empty($index)) {
sleep($this->waitTime);
return null;
}
// get data
$data = $this->getData($index);
if (empty($data)) { // invalid index
$data = $this->setDataKey($data, $index);
$this->remove($index);
}
// set current key
$data = $this->setDataKey($data, $index);
$this->setProcessingIndex($data);
return $data;
}
// remove message
public function remove($data)
{
// remove processing index
$this->removeProcessingIndex($data);
// remove data
$ret = $this->removeData($data);
return $ret;
}
// rollback message
public function rollback($data)
{
$processingIndex = $this->getProcessingIndex($data);
if ($processingIndex) {
// add blocked times
$this->addBlocked($processingIndex);
// check blocked times
$blockTimes = $this->getBlockedTimes($processingIndex);
if ($blockTimes >= $this->retryTimes) { // if retry times up to max, the index will be transfer to blocked list
// transfer to blocked list
$ret = $this->transferToBlocked($processingIndex);
} else {
// rollback index
$ret = $this->addIndex($processingIndex);
}
if (!empty($ret)) {
// clear processing index
$this->removeProcessingIndex($data);
}
}
}
// processing
public function getCurrentIndex($data)
{
$processingIndex = $this->getProcessingIndex($data);
return $processingIndex;
}
// repair
public function repair()
{
$num = 0;
// restore blocked
$blockedIndex = $this->getBlockedIndex();
while (!empty($blockedIndex)) {
$this->addIndex($blockedIndex);
// clear blocked times
$this->removeBlockedTimes($blockedIndex);
$blockedIndex = $this->getBlockedIndex();
$num += 1;
}
// clear current index
$this->removeAllProcessingIndex();
return $num;
}
// status
public function status()
{
$ret = [];
$ret['total pending index'] = $this->redis->lLen($this->getIndexListName());
$ret['total blocked index'] = $this->redis->lLen($this->getBlockedListName());
return $ret;
}
}