-
Notifications
You must be signed in to change notification settings - Fork 18
/
SqsConsumer.php
210 lines (171 loc) · 5.77 KB
/
SqsConsumer.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
declare(strict_types=1);
namespace Enqueue\Sqs;
use Interop\Queue\Consumer;
use Interop\Queue\Exception\InvalidMessageException;
use Interop\Queue\Message;
use Interop\Queue\Queue;
class SqsConsumer implements Consumer
{
/**
* @var SqsDestination
*/
private $queue;
/**
* @var SqsContext
*/
private $context;
/**
* @var int|null
*/
private $visibilityTimeout;
/**
* @var int
*/
private $maxNumberOfMessages;
/**
* @var array
*/
private $messages;
public function __construct(SqsContext $context, SqsDestination $queue)
{
$this->context = $context;
$this->queue = $queue;
$this->messages = [];
$this->maxNumberOfMessages = 1;
}
public function getVisibilityTimeout(): ?int
{
return $this->visibilityTimeout;
}
/**
* The duration (in seconds) that the received messages are hidden from subsequent retrieve
* requests after being retrieved by a ReceiveMessage request.
*/
public function setVisibilityTimeout(int $visibilityTimeout = null): void
{
$this->visibilityTimeout = $visibilityTimeout;
}
public function getMaxNumberOfMessages(): int
{
return $this->maxNumberOfMessages;
}
/**
* The maximum number of messages to return. Amazon SQS never returns more messages than this value
* (however, fewer messages might be returned). Valid values are 1 to 10. Default is 1.
*/
public function setMaxNumberOfMessages(int $maxNumberOfMessages): void
{
$this->maxNumberOfMessages = $maxNumberOfMessages;
}
/**
* @return SqsDestination
*/
public function getQueue(): Queue
{
return $this->queue;
}
/**
* @return SqsMessage
*/
public function receive(int $timeout = 0): ?Message
{
$maxLongPollingTime = 20; // 20 is max allowed long polling value
if (0 === $timeout) {
while (true) {
if ($message = $this->receiveMessage($maxLongPollingTime)) {
return $message;
}
}
}
$timeout = (int) ceil($timeout / 1000);
if ($timeout > $maxLongPollingTime) {
throw new \LogicException(sprintf('Max allowed SQS receive message timeout is: "%s"', $maxLongPollingTime));
}
return $this->receiveMessage($timeout);
}
/**
* @return SqsMessage
*/
public function receiveNoWait(): ?Message
{
return $this->receiveMessage(0);
}
/**
* @param SqsMessage $message
*/
public function acknowledge(Message $message): void
{
InvalidMessageException::assertMessageInstanceOf($message, SqsMessage::class);
$this->context->getSqsClient()->deleteMessage([
'@region' => $this->queue->getRegion(),
'QueueUrl' => $this->context->getQueueUrl($this->queue),
'ReceiptHandle' => $message->getReceiptHandle(),
]);
}
/**
* @param SqsMessage $message
*/
public function reject(Message $message, bool $requeue = false): void
{
InvalidMessageException::assertMessageInstanceOf($message, SqsMessage::class);
if ($requeue) {
$this->context->getSqsClient()->changeMessageVisibility([
'@region' => $this->queue->getRegion(),
'QueueUrl' => $this->context->getQueueUrl($this->queue),
'ReceiptHandle' => $message->getReceiptHandle(),
'VisibilityTimeout' => $message->getRequeueVisibilityTimeout(),
]);
} else {
$this->context->getSqsClient()->deleteMessage([
'@region' => $this->queue->getRegion(),
'QueueUrl' => $this->context->getQueueUrl($this->queue),
'ReceiptHandle' => $message->getReceiptHandle(),
]);
}
}
protected function receiveMessage(int $timeoutSeconds): ?SqsMessage
{
if ($message = array_pop($this->messages)) {
return $this->convertMessage($message);
}
$arguments = [
'@region' => $this->queue->getRegion(),
'AttributeNames' => ['All'],
'MessageAttributeNames' => ['All'],
'MaxNumberOfMessages' => $this->maxNumberOfMessages,
'QueueUrl' => $this->context->getQueueUrl($this->queue),
'WaitTimeSeconds' => $timeoutSeconds,
];
if ($this->visibilityTimeout) {
$arguments['VisibilityTimeout'] = $this->visibilityTimeout;
}
$result = $this->context->getSqsClient()->receiveMessage($arguments);
if ($result->hasKey('Messages')) {
$this->messages = $result->get('Messages');
}
if ($message = array_pop($this->messages)) {
return $this->convertMessage($message);
}
return null;
}
protected function convertMessage(array $sqsMessage): SqsMessage
{
$message = $this->context->createMessage();
$message->setBody($sqsMessage['Body']);
$message->setReceiptHandle($sqsMessage['ReceiptHandle']);
if (isset($sqsMessage['Attributes'])) {
$message->setAttributes($sqsMessage['Attributes']);
}
if (isset($sqsMessage['Attributes']['ApproximateReceiveCount'])) {
$message->setRedelivered(((int) $sqsMessage['Attributes']['ApproximateReceiveCount']) > 1);
}
if (isset($sqsMessage['MessageAttributes']['Headers'])) {
$headers = json_decode($sqsMessage['MessageAttributes']['Headers']['StringValue'], true);
$message->setHeaders($headers[0]);
$message->setProperties($headers[1]);
}
$message->setMessageId($sqsMessage['MessageId']);
return $message;
}
}