-
Notifications
You must be signed in to change notification settings - Fork 1
/
Driver.php
275 lines (229 loc) · 6.54 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
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
<?php
namespace Bernard\Driver\Sqs;
use Aws\Sqs\Exception\SqsException;
use Aws\Sqs\SqsClient;
use Bernard\Driver\AbstractPrefetchDriver;
/**
* Implements a Driver for use with AWS SQS client API: https://aws.amazon.com/sqs/
*/
final class Driver extends AbstractPrefetchDriver
{
const AWS_SQS_FIFO_SUFFIX = '.fifo';
const AWS_SQS_EXCEPTION_BAD_REQUEST = 400;
const AWS_SQS_EXCEPTION_NOT_FOUND = 404;
private $sqs;
private $queueUrls;
/**
* @param SqsClient $sqs
* @param array $queueUrls
* @param int|null $prefetch
*/
public function __construct(SqsClient $sqs, array $queueUrls = [], $prefetch = null)
{
parent::__construct($prefetch);
$this->sqs = $sqs;
$this->queueUrls = $queueUrls;
}
/**
* {@inheritdoc}
*/
public function listQueues()
{
$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);
}
/**
* {@inheritdoc}
*
* @see http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#createqueue
*
* @throws SqsException
*/
public function createQueue($queueName)
{
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'];
}
/**
* @param string $queueName
*
* @return bool
*
* @throws SqsException
*/
private function queueExists($queueName)
{
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;
}
}
/**
* @param string $queueName
*
* @return bool
*/
private function isFifoQueue($queueName)
{
return $this->endsWith($queueName, self::AWS_SQS_FIFO_SUFFIX);
}
/**
* @param string $haystack
* @param string $needle
*
* @return bool
*/
private function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length === 0) {
return true;
}
return substr($haystack, -$length) === $needle;
}
/**
* {@inheritdoc}
*/
public function countMessages($queueName)
{
$queueUrl = $this->resolveUrl($queueName);
$result = $this->sqs->getQueueAttributes([
'QueueUrl' => $queueUrl,
'AttributeNames' => ['ApproximateNumberOfMessages'],
]);
if (isset($result['Attributes']['ApproximateNumberOfMessages'])) {
return (int) $result['Attributes']['ApproximateNumberOfMessages'];
}
return 0;
}
/**
* {@inheritdoc}
*/
public function pushMessage($queueName, $message)
{
$queueUrl = $this->resolveUrl($queueName);
$parameters = [
'QueueUrl' => $queueUrl,
'MessageBody' => $message,
];
if ($this->isFifoQueue($queueName)) {
$parameters['MessageGroupId'] = __METHOD__;
$parameters['MessageDeduplicationId'] = md5($message);
}
$this->sqs->sendMessage($parameters);
}
/**
* {@inheritdoc}
*/
public function popMessage($queueName, $duration = 5)
{
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, null];
}
foreach ($messages as $message) {
$this->cache->push($queueName, [$message['Body'], $message['ReceiptHandle']]);
}
return $this->cache->pop($queueName);
}
/**
* {@inheritdoc}
*/
public function acknowledgeMessage($queueName, $receipt)
{
$queueUrl = $this->resolveUrl($queueName);
$this->sqs->deleteMessage([
'QueueUrl' => $queueUrl,
'ReceiptHandle' => $receipt,
]);
}
/**
* {@inheritdoc}
*/
public function peekQueue($queueName, $index = 0, $limit = 20)
{
return [];
}
/**
* {@inheritdoc}
*
* @see http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#deletequeue
*/
public function removeQueue($queueName)
{
$queueUrl = $this->resolveUrl($queueName);
$this->sqs->deleteQueue([
'QueueUrl' => $queueUrl,
]);
}
/**
* {@inheritdoc}
*/
public function info()
{
return [
'prefetch' => $this->prefetch,
];
}
/**
* AWS works with queue URLs rather than queue names. Returns either queue URL (if queue exists) for given name or null if not.
*
* @param string $queueName
*
* @return mixed
*
* @throws SqsException
*/
private function resolveUrl($queueName)
{
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.');
}
}