-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMessageQueueMethod.php
More file actions
487 lines (470 loc) · 19.3 KB
/
MessageQueueMethod.php
File metadata and controls
487 lines (470 loc) · 19.3 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php declare(strict_types=1);
namespace Workbunny\WebmanRqueue\Builders\Traits;
use RedisException;
use support\Log;
use Workbunny\WebmanRqueue\Exceptions\WebmanRqueueException;
use Workbunny\WebmanRqueue\Headers;
use Workerman\Worker;
use function Workbunny\WebmanRqueue\config;
trait MessageQueueMethod
{
use MessageTempMethod;
protected array $claimStartTags = [];
protected bool $_init = false;
/**
* @param string|null $queueName
* @return array
* @throws RedisException
*/
public function getQueueFullInfo(null|string $queueName = null): array
{
$queues = $queueName ? [$queueName] : $this->getBuilderConfig()->getQueues();
$result = [];
foreach ($queues as $queue) {
$result[$queue] = $this->getConnection()->client()->xInfo('STREAM', $queue, 'FULL');
}
return $result;
}
/**
* 获取Builder中所有队列的信息
*
* @param string|null $queueName
* @return array = [
* queueName => [
* 'length' => int,
* 'radix-tree-keys' => int,
* 'radix-tree-nodes' => int,
* 'last-generated-id' => string [int-int],
* 'max-deleted-entry-id' => string [int-int],
* 'entries-added' => int,
* 'groups' => int,
* 'first-entry' => [
* ID => [
* '_header' => array,
* '_body' => string
* ]
* ],
* 'last-entry' => [
* ID => [
* '_header' => array,
* '_body' => string
* ]
* ]
* ]
* ]
* @throws RedisException
*/
public function getQueueInfo(null|string $queueName = null): array
{
$queues = $queueName ? [$queueName] : $this->getBuilderConfig()->getQueues();
$result = [];
foreach ($queues as $queue) {
$result[$queue] = $this->getConnection()->client()->xInfo('STREAM', $queue);
}
return $result;
}
/**
* @param string|null $queueName
* @return array
* @throws RedisException
*/
public function getQueueConsumersInfo(null|string $queueName = null): array
{
$groupName = $this->getBuilderConfig()->getGroup();
$queues = $queueName ? [$queueName] : $this->getBuilderConfig()->getQueues();
$result = [];
foreach ($queues as $queue) {
$result[$queue] = $this->getConnection()->client()->xInfo('CONSUMERS', $queueName, $groupName);
}
return $result;
}
/**
* 获取Builder中所有队列的消费组信息
*
* @param string|null $queueName
* @return array = [
* queueName => [
* [
* 'name' => string,
* 'consumers' => int,
* 'pending' => int,
* 'last-delivered-id' => string [int-int],
* 'entries-read' => int,
* 'lag' => int
* ]
* ]
* ]
* @throws RedisException
*/
public function getQueueGroupsInfo(null|string $queueName = null): array
{
$queues = $queueName ? [$queueName] : $this->getBuilderConfig()->getQueues();
$result = [];
foreach ($queues as $queue) {
$result[$queue] = $this->getConnection()->client()->xInfo('GROUPS', $queue);
}
return $result;
}
/**
* 仅移除所有分组中游标最落后的至第一个消息间的消息
*
* @return void
* @throws RedisException
*/
public function del(): void
{
$groupsInfo = $this->getQueueGroupsInfo();
$queuesInfo = $this->getQueueInfo();
$client = $this->getConnection()->client();
foreach ($groupsInfo as $queueName => $info) {
$queueFirstId = array_key_first($queuesInfo[$queueName]['first-entry'] ?? []);
$lastDeliveredIds = array_column($info, 'last-delivered-id');
if ($queueFirstId and $lastDeliveredIds) {
$leftMin = $rightMin = null;
// 获取当前队列所有group中最落后的游标id
foreach ($lastDeliveredIds as $lastDeliveredId) {
list($left, $right) = explode('-', $lastDeliveredId);
if (
($left > $leftMin and $leftMin !== null) or
($left == $leftMin and $right > $rightMin and $leftMin !== null)
) {
continue;
}
$leftMin = $left;
$rightMin = $right;
}
$lastDeliveredId = "$leftMin-$rightMin";
$result = $client->xRange($queueName, $queueFirstId, $lastDeliveredId, 100);
foreach ($result as $id => $value) {
if($id !== $lastDeliveredId) {
$client->xDel($queueName, [$id]);
}
}
}
}
}
/**
* @param string $queueName
* @param string $groupName
* @param array $id
* @param bool $retry
* @return bool
*/
public function ack(string $queueName, string $groupName, array $id, bool $retry = false): bool
{
try {
$this->getConnection()->client()->xAck($queueName, $groupName, $id);
return true;
} catch (RedisException) {
Log::channel('plugin.workbunny.webman-rqueue.warning')?->warning("Ack failed [$queueName->$groupName]. ", [
'message' => $exception->getMessage(), 'code' => $exception->getCode(),
'file' => $exception->getFile() . ':' . $exception->getLine(),
'trace' => $exception->getTrace(), 'ids' => $id
]);
// 兼容旧版
$this->getLogger()?->warning("Ack failed. ", [
'queue' => $queueName, 'group' => $groupName, 'id' => $id
]);
}
if ($retry) {
// 阻塞当前进程,重试
$sleep = 250 * 1000;
$index = 2;
$max = 5 * 60 * 1000 * 1000;
while (1) {
Log::channel('plugin.workbunny.webman-rqueue.notice')
->notice($message = __CLASS__ . "| Consumer blocking-retry! [usleep: $sleep] ", [
'queue' => $queueName, 'group' => $groupName, 'id' => $id, 'sleep' => $sleep
]);
// 日志
$this->getLogger()?->notice($message, [
'queue' => $queueName, 'group' => $groupName, 'id' => $id, 'sleep' => $sleep
]);
// 输出
echo $message . PHP_EOL;
// 每次阻塞指数倍数上升,最大max
usleep($sleep);
$sleep = min(($sleep * $index), $max);
if ($this->ack($queueName, $groupName, $id)) {
break;
}
}
}
return false;
}
/**
* 消息发布
* 1. 多队列模式不能保证事务
*
* @param string $body
* @param array $headers = [
* @see Headers
* ]
* @param string|null $queueName
* @param bool $temp
* @return array 返回成功的消息ID组
*/
public function publishGetIds(string $body, array $headers = [], null|string $queueName = null, bool $temp = false): array
{
$client = $this->getConnection()->client();
$header = new Headers($headers);
$header->_timestamp = $header->_timestamp > 0.0 ? $header->_timestamp : microtime(true);
if(
($header->_delay and !$this->getBuilderConfig()->isDelayed()) or
(!$header->_delay and $this->getBuilderConfig()->isDelayed())
){
throw new WebmanRqueueException('Invalid publish.');
}
$queues = $this->getBuilderConfig()->getQueues();
$queueSize = $this->getBuilderConfig()->getQueueSize();
if($queueName !== null and !isset($queues[$queueName])) {
throw new WebmanRqueueException('Invalid queue name.');
}
$queues = $queueName ? [$queueName] : $queues;
$ids = [];
try {
foreach ($queues as $queue) {
if ($queueSize > 0) {
$queueLen = $client->xLen($queue);
if($queueLen >= $queueSize){
throw new WebmanRqueueException('Queue size exceeded.');
}
}
if ($id = $client->xAdd($queue, (string)$header->_id, $data = [
'_header' => $header->toString(),
'_body' => $body,
])) {
$ids[$queue] = $id;
} else {
if ($temp) {
$this->tempInsert('requeue', $queue, $data);
$ids["<temp>$queue</temp>"] = $id;
}
}
}
return $ids;
} catch (RedisException $exception) {
Log::channel('plugin.workbunny.webman-rqueue.debug')?->debug($exception->getMessage(), $exception->getTrace());
$this->getLogger()?->debug($exception->getMessage(), $exception->getTrace());
throw new WebmanRqueueException($exception->getMessage(), $exception->getCode(), $exception);
}
}
/**
* 消息发布
* 1. 多队列模式不能保证事务
*
* @param string $body
* @param array $headers = [
* @see Headers
* ]
* @param string|null $queueName
* @param bool $temp
* @return int|false 0/false 代表全部失败
*/
public function publish(string $body, array $headers = [], null|string $queueName = null , bool $temp = false): int|false
{
return count($this->publishGetIds($body, $headers, $queueName, $temp));
}
/**
* @param string $body
* @param array $headers = [
* @see Headers
* ]
* @return int|false
*/
public function requeue(string $body, array $headers = []): int|false
{
$client = $this->getConnection()->client();
$header = new Headers($headers);
$header->_timestamp = $header->_timestamp > 0.0 ? $header->_timestamp : microtime(true);
if(
($header->_delay and !$this->getBuilderConfig()->isDelayed()) or
(!$header->_delay and $this->getBuilderConfig()->isDelayed())
){
throw new WebmanRqueueException('Invalid publish.');
}
$count = 0;
$queues = $this->getBuilderConfig()->getQueues();
foreach ($queues as $queue) {
try {
if (!$client->xAdd($queue, (string)$header->_id, $data = [
'_header' => $header->toString(),
'_body' => $body,
])) {
return false;
}
$count ++;
} catch (RedisException $exception) {
Log::channel('plugin.workbunny.webman-rqueue.debug')?->debug($exception->getMessage(), $exception->getTrace());
$this->getLogger()?->debug($exception->getMessage(), $exception->getTrace());
if (isset($data)) {
$this->tempInsert('requeue', $queue, $data);
echo 'temp requeue insert' . PHP_EOL;
}
}
}
return $count;
}
/**
* @param Worker $worker
* @param int $pendingTimeout
* @param bool $autoDel
* @return void
*/
public function claim(Worker $worker, int $pendingTimeout, bool $autoDel = true): void
{
try {
$client = $this->getConnection()->client();
if (!method_exists($client, 'xAutoClaim')) {
Log::channel('plugin.workbunny.webman-rqueue.warning')->warning(
'Method xAutoClaim requires redis-server >= 6.2.0. '
);
return;
}
$builderConfig = $this->getBuilderConfig();
$queues = $builderConfig->getQueues();
$groupName = $builderConfig->getGroup();
$consumerName = "$groupName-$worker->id";
foreach ($queues as $queueName) {
$datas = $client->xAutoClaim(
$queueName, $groupName, $consumerName,
$pendingTimeout * 1000,
$this->claimStartTags[$queueName][$groupName][$consumerName] ?? '0-0', -1
);
if ($datas) {
$this->claimStartTags[$queueName][$groupName][$consumerName] = $datas[0] ?? '0-0';
if ($datas = $datas[1] ?? []) {
if ($client->xAck($queueName, $groupName, array_keys($datas))) {
// pending超时的消息自动ack,并存入本地缓存
try {
foreach ($datas as $message) {
$header = new Headers($message['_header']);
$body = $message['_body'];
$this->tempInsert('pending', $queueName, $message);
echo 'temp pending insert' . PHP_EOL;
$this->requeue($body, $header->toArray());
}
}
// 忽略失败
catch (\Throwable) {}
if ($autoDel) {
// 移除
$client->xDel($queueName, array_keys($datas));
}
}
}
}
}
} catch (RedisException $exception) {
Log::channel('plugin.workbunny.webman-rqueue.debug')?->debug($exception->getMessage(), $exception->getTrace());
$this->getLogger()?->debug($exception->getMessage(), $exception->getTrace());
throw new WebmanRqueueException($exception->getMessage(), $exception->getCode(), $exception);
}
}
/**
* @param Worker $worker
* @param bool $del
* @return bool true:有消费 false:无消费
* @throws WebmanRqueueException
*/
public function consume(Worker $worker, bool $del = true): bool
{
try {
$client = $this->getConnection()->client();
$builderConfig = $this->getBuilderConfig();
$queues = $builderConfig->getQueues();
$groupName = $builderConfig->getGroup();
$consumerName = "$groupName-$worker->id";
// create group
$queueStreams = [];
if (!$this->_init) {
foreach ($queues as $queueName) {
$client->xGroup('CREATE', $queueName, $groupName,'0', true);
}
$this->_init = true;
}
foreach ($queues as $queueName) {
$queueStreams[$queueName] = '>';
}
// group read
if ($res = $client->xReadGroup(
$groupName, $consumerName, $queueStreams, $builderConfig->getPrefetchCount(),
$this->getTimerInterval() >= 1.0 ? (int)$this->getTimerInterval() : null
)) {
foreach ($res as $queueName => $item) {
$ids = [];
// messages
foreach ($item as $id => $message){
// drop
if(!isset($message['_header']) or !isset($message['_body'])) {
$this->ack($queueName, $groupName, $this->idsAdd($ids, $id));
continue;
}
$header = new Headers($message['_header']);
$body = $message['_body'];
// delay message
if(
$this->getBuilderConfig()->isDelayed() and $header->_delay > 0 and
(($header->_delay / 1000 + $header->_timestamp) - microtime(true)) > 0
){
// republish
$header->_id = '*';
if ($this->requeue($body, $header->toArray())) {
// blocking-retry ack
$this->ack($queueName, $groupName, $this->idsAdd($ids, $id), true);
// if (!$this->ack($queueName, $groupName, $this->idsAdd($ids, $id))) {
// $this->idsDel($ids, $id);
// }
}
continue;
}
try {
// handler
if (!\call_user_func($this->getBuilderConfig()->getCallback(), $id, $message, $this->getConnection())) {
throw new WebmanRqueueException('Consume failed. ');
}
$this->ack($queueName, $groupName, $this->idsAdd($ids, $id));
} catch (\Throwable $throwable) {
// republish
$header->_count = $header->_count + 1;
$header->_error = "{$throwable->getMessage()} [{$throwable->getFile()}:{$throwable->getLine()}]";
// republish都将刷新使用redis stream自身的id,自定义id无效
$header->_id = '*';
// 如果错误超过error max count,则存入本地error表
if (
($errorMaxCount = config('plugin.workbunny.webman-rqueue.app.error_max_count', 0)) > 0 and
$header->_count >= $errorMaxCount
) {
// 存入
if ($this->tempInsert('error', $queueName, [
'_header' => $header->toArray(),
'_body' => $body
])) {
// blocking-retry ack
$this->ack($queueName, $groupName, $this->idsAdd($ids, $id), true);
}
} else {
if ($this->requeue($body, $header->toArray())) {
// blocking-retry ack
$this->ack($queueName, $groupName, $this->idsAdd($ids, $id), true);
// if (!$this->ack($queueName, $groupName, $this->idsAdd($ids, $id))) {
// $this->idsDel($ids, $id);
// }
}
}
}
}
// del
if($del) { $client->xDel($queueName, $ids); }
}
return true;
}
return false;
} catch (RedisException $exception) {
Log::channel('plugin.workbunny.webman-rqueue.debug')?->debug($exception->getMessage(), $exception->getTrace());
$this->getLogger()?->debug($exception->getMessage(), $exception->getTrace());
$this->_init = false;
throw new WebmanRqueueException($exception->getMessage(), $exception->getCode(), $exception);
}
}
}