-
Notifications
You must be signed in to change notification settings - Fork 3
/
Driver.php
223 lines (178 loc) · 6 KB
/
Driver.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
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
declare(strict_types=1);
namespace Bernard\Driver\Sqs;
use Aws\Sqs\Exception\SqsException;
use Aws\Sqs\SqsClient;
use Bernard\Driver\AbstractPrefetchDriver;
use Bernard\Driver\Message;
/**
* Implements a Driver for use with AWS SQS client API: https://aws.amazon.com/sqs/.
*/
final class Driver extends AbstractPrefetchDriver
{
public const AWS_SQS_FIFO_SUFFIX = '.fifo';
public const AWS_SQS_EXCEPTION_BAD_REQUEST = 400;
public const AWS_SQS_EXCEPTION_NOT_FOUND = 404;
public function __construct(private SqsClient $sqs, private array $queueUrls = [], ?int $prefetch = null)
{
parent::__construct($prefetch);
}
public function listQueues(): array
{
$result = $this->sqs->listQueues();
// TODO: drop this as it can easily get inconsistent?
if (!$queueUrls = $result->get('QueueUrls')) {
return array_keys($this->queueUrls);
}
foreach ($queueUrls as $queueUrl) {
if (\in_array($queueUrl, $this->queueUrls)) {
continue;
}
$queueName = current(array_reverse(explode('/', $queueUrl)));
$this->queueUrls[$queueName] = $queueUrl;
}
return array_keys($this->queueUrls);
}
/**
* @see http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#createqueue
*
* @throws SqsException
*/
public function createQueue(string $queueName): void
{
if ($this->queueExists($queueName)) {
return;
}
$parameters = [
'QueueName' => $queueName,
];
if ($this->isFifoQueue($queueName)) {
$parameters['Attributes'] = [
'FifoQueue' => 'true',
];
}
$result = $this->sqs->createQueue($parameters);
$this->queueUrls[$queueName] = $result['QueueUrl'];
}
/**
* @throws SqsException
*/
private function queueExists(string $queueName): bool
{
try {
$this->resolveUrl($queueName);
return true;
} catch (\InvalidArgumentException $exception) {
return false;
} catch (SqsException $exception) {
if ($previousException = $exception->getPrevious()) {
switch ($previousException->getCode()) {
case self::AWS_SQS_EXCEPTION_BAD_REQUEST:
case self::AWS_SQS_EXCEPTION_NOT_FOUND:
return false;
}
}
throw $exception;
}
}
private function isFifoQueue(string $queueName): bool
{
return $this->endsWith($queueName, self::AWS_SQS_FIFO_SUFFIX);
}
private function endsWith(string $haystack, string $needle): bool
{
$length = \strlen($needle);
if ($length === 0) {
return true;
}
return substr($haystack, -$length) === $needle;
}
/**
* @see http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#deletequeue
*/
public function removeQueue(string $queueName): void
{
$queueUrl = $this->resolveUrl($queueName);
$this->sqs->deleteQueue([
'QueueUrl' => $queueUrl,
]);
}
public function pushMessage(string $queueName, string $message): void
{
$queueUrl = $this->resolveUrl($queueName);
$parameters = [
'QueueUrl' => $queueUrl,
'MessageBody' => $message,
];
if ($this->isFifoQueue($queueName)) {
$parameters['MessageGroupId'] = __METHOD__;
$parameters['MessageDeduplicationId'] = md5($message);
}
$this->sqs->sendMessage($parameters);
}
public function popMessage(string $queueName, int $duration = 5): ?Message
{
if ($message = $this->cache->pop($queueName)) {
return $message;
}
$queueUrl = $this->resolveUrl($queueName);
$result = $this->sqs->receiveMessage([
'QueueUrl' => $queueUrl,
'MaxNumberOfMessages' => $this->prefetch,
'WaitTimeSeconds' => $duration,
]);
if (!$result || !$messages = $result->get('Messages')) {
return null;
}
foreach ($messages as $message) {
$this->cache->push($queueName, new Message($message['Body'], $message['ReceiptHandle']));
}
return $this->cache->pop($queueName);
}
public function acknowledgeMessage(string $queueName, mixed $receipt): void
{
$queueUrl = $this->resolveUrl($queueName);
$this->sqs->deleteMessage([
'QueueUrl' => $queueUrl,
'ReceiptHandle' => $receipt,
]);
}
public function info(): array
{
return [
'prefetch' => $this->prefetch,
];
}
public function countMessages(string $queueName): int
{
$queueUrl = $this->resolveUrl($queueName);
$result = $this->sqs->getQueueAttributes([
'QueueUrl' => $queueUrl,
'AttributeNames' => ['ApproximateNumberOfMessages'],
]);
if (isset($result['Attributes']['ApproximateNumberOfMessages'])) {
return (int) $result['Attributes']['ApproximateNumberOfMessages'];
}
return 0;
}
public function peekQueue(string $queueName, int $index = 0, int $limit = 20): array
{
return [];
}
/**
* AWS works with queue URLs rather than queue names. Returns either queue URL (if queue exists) for given name or null if not.
*
* @throws SqsException
*/
private function resolveUrl(string $queueName): mixed
{
if (isset($this->queueUrls[$queueName])) {
return $this->queueUrls[$queueName];
}
$result = $this->sqs->getQueueUrl(['QueueName' => $queueName]);
if ($result && $queueUrl = $result->get('QueueUrl')) {
return $this->queueUrls[$queueName] = $queueUrl;
}
throw new \InvalidArgumentException('Queue "'.$queueName.'" cannot be resolved to an url.');
}
}