-
Notifications
You must be signed in to change notification settings - Fork 435
Expand file tree
/
Copy pathRedisConsumer.php
More file actions
151 lines (124 loc) · 3.54 KB
/
RedisConsumer.php
File metadata and controls
151 lines (124 loc) · 3.54 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
<?php
declare(strict_types=1);
namespace Enqueue\Redis;
use Interop\Queue\Consumer;
use Interop\Queue\Exception\InvalidMessageException;
use Interop\Queue\Message;
use Interop\Queue\Queue;
class RedisConsumer implements Consumer
{
use RedisConsumerHelperTrait;
/**
* @var RedisDestination
*/
private $queue;
/**
* @var RedisContext
*/
private $context;
/**
* @var int
*/
private $redeliveryDelay = 300;
/**
* @var int
*/
private $deliveryDelay;
public function __construct(RedisContext $context, RedisDestination $queue)
{
$this->context = $context;
$this->queue = $queue;
}
/**
* @return int
*/
public function getRedeliveryDelay(): ?int
{
return $this->redeliveryDelay;
}
public function setRedeliveryDelay(int $delay): void
{
$this->redeliveryDelay = $delay;
}
/**
* @return int
*/
public function getDeliveryDelay(): ?int
{
return $this->deliveryDelay;
}
public function setDeliveryDelay(int $deliveryDelay): void
{
$this->deliveryDelay = $deliveryDelay;
}
/**
* @return RedisDestination
*/
public function getQueue(): Queue
{
return $this->queue;
}
/**
* @return RedisMessage
*/
public function receive(int $timeout = 0): ?Message
{
$timeout = (int) ceil($timeout / 1000);
if ($timeout <= 0) {
while (true) {
if ($message = $this->receive(5000)) {
return $message;
}
}
}
return $this->receiveMessage([$this->queue], $timeout, $this->redeliveryDelay);
}
/**
* @return RedisMessage
*/
public function receiveNoWait(): ?Message
{
return $this->receiveMessageNoWait($this->queue, $this->redeliveryDelay);
}
/**
* @param RedisMessage $message
*/
public function acknowledge(Message $message): void
{
$this->getRedis()->zrem($this->queue->getName().':reserved', $message->getReservedKey());
}
/**
* @param RedisMessage $message
*/
public function reject(Message $message, bool $requeue = false, $delay = 0): void
{
InvalidMessageException::assertMessageInstanceOf($message, RedisMessage::class);
$this->acknowledge($message);
if ($requeue) {
$message = $this->getContext()->getSerializer()->toMessage($message->getReservedKey());
$message->setRedelivered(true);
if (null !== $this->deliveryDelay && null === $message->getDeliveryDelay()) {
$message->setDeliveryDelay($this->deliveryDelay);
}
if ($message->getTimeToLive()) {
$message->setHeader('expires_at', time() + $message->getTimeToLive());
}
if($message->getDeliveryDelay()) {
$deliveryAt = time() + $message->getDeliveryDelay() / 1000;
$payload = $this->getContext()->getSerializer()->toString($message);
$this->getRedis()->zadd($this->queue->getName().':delayed', $payload, $deliveryAt);
} else {
$payload = $this->getContext()->getSerializer()->toString($message);
$this->getRedis()->lpush($this->queue->getName(), $payload);
}
}
}
private function getContext(): RedisContext
{
return $this->context;
}
private function getRedis(): Redis
{
return $this->context->getRedis();
}
}