forked from dmtrs/yii-aws-sqs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAWSQueueManager.php
290 lines (254 loc) · 8.86 KB
/
AWSQueueManager.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
Yii::import('ext.yii-aws-sqs.*');
/**
* AWSQueueManager
*/
class AWSQueueManager extends CApplicationComponent
{
/**
* @var string SQS access key (a.k.a. AWS_KEY)
*/
public $accessKey;
/**
* @var string SQS secret key (a.k.a. AWS_SECRET_KEY)
*/
public $secretKey;
/**
* @var string The AWS region where this SQS account lives
*/
public $region;
/**
* @var string The version of the SQS API to use
*/
public $version = '2012-11-05';
/**
* @var string The local endpoint for SQS
*/
public $localEndpoint;
/**
* @var AmazonSQS
*/
private $_sqs;
/**
* @var CList queues list
*/
private $_queues;
/**
* @var string Optional prefix to add to the queue name.
*/
public $tablePrefix;
/**
* Initializes the application component.
*/
public function init()
{
if($this->accessKey===null || $this->secretKey===null)
throw new CException(__CLASS__.' $accessKey and $secretKey must be set');
if ($this->region === null) {
$this->region = 'us-east-1'; // set default, so don't need any config for normal case.
}
$this->_sqs = new Aws\Sqs\SqsClient([
'version' => $this->version,
'region' => $this->region,
'credentials' => [
'key' => $this->accessKey,
'secret' => $this->secretKey,
],
'endpoint' => $this->localEndpoint,
'credentials.cache' => false, // Utilize the Doctrine Cache PHP library to cache credentials with APC. Avoids the cost of sending an HTTP request to the IMDS each time the SDK is utilized.
]);
parent::init();
}
/**
* Returns a queue, property value, an event handler list or a behavior based on its name.
* Do not call this method.
*/
public function __get($name)
{
if($this->getQueues()->itemAt($name)!==null)
return $this->queues->{$name};
else
return parent::__get($name);
}
/**
* @return CList queues list
*/
public function getQueues($refresh=false)
{
if($this->_queues===null || $refresh) {
$this->_queues = new AWSQueueList();
$this->_queues->caseSensitive = true;
$result = $this->_sqs->listQueues();
$list = $result->get('QueueUrls');
if(!empty($list)) {
foreach($list as $qUrl)
{
$q = new AWSQueue($this, $qUrl);
$this->_queues->add($q->name,$q);
}
unset($list);
}
}
return $this->_queues;
}
/**
* Important Note: If the SQS queue is FIFO type we MUST send the MessageGroupId also,
* otherwise the request will fail.
* @link https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html
*
* Define if the SQS queue is a FIFO one via passing the `MessageGroupId` on $options param.
*
* @param string $url url of the queue to send message
* @param string $message message to send
* @param array $options extra options for the message
* @return boolean message was succesfull
*/
public function send($url, $message, $options= [])
{
$this->_sqs->sendMessage(array_merge([
'QueueUrl' => $url,
'MessageBody' => $message,
], $options));
return true; // If delete failed the above would throw an exception
}
/**
* Send a batch of messages. AWS SQS limits the message batches
* with a limit of 10 per request. If $messageArray has more than 10 messages
* then 2 requests will be triggered.
*
* Important Note: If the SQS queue is FIFO type we MUST send the MessageGroupId also,
* otherwise the request will fail.
* @link https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html
*
* Define if the SQS queue is a FIFO one via passing the `MessageGroupId` on $options param.
*
* @param string $url url of the queue to send message
* @param string $messageArray message to send
* @param array $options extra options for the message
* @return boolean message was successful
*/
public function sendBatch($url, $messageArray, $options= [])
{
$r = true;
foreach(array_chunk($messageArray,10) as $batch) {
$messages= [];
foreach($batch as $i=>$message) {
$messageData = [
'Id' => $i,
'MessageBody' => (string)$message,
];
if (isset($options['MessageGroupId'])) {
$messageData['MessageGroupId'] = $options['MessageGroupId'];
}
$messages[]= $messageData;
}
$result = $this->_sqs->sendMessageBatch(array_merge([
'QueueUrl' => $url,
'Entries' => $messages,
],$options));
$fails = $result->get('Failed');
$r=$r&&!$fails;
}
return $r;
}
/**
* Receive messages from the queue
* If there is no message returned then this function returns null.
* In case of one message then a AWSMessage is returned for convenience, if more
* then an array of AWSMessage objects is returned.
*
* @param string $url url of the queue to send message
* @param array $options extra options for the message
* @return mixed
*/
public function receive($url, $options=array())
{
$msgs=array();
$result = $this->_sqs->receiveMessage(array_merge(array(
'QueueUrl' => $url,
),$options));
$hasMsg = ($result['Messages'] !== null);
if ($hasMsg) {
foreach ($result['Messages'] as $message) {
$m = new AWSMessage();
$m->id = (string)$message['MessageId'];
$m->body = (string)$message['Body'];
$m->md5 = (string)$message['MD5OfBody'];
$m->receiptHandle = (string)$message['ReceiptHandle'];
if (isset($message['Attributes'])) {
foreach ($message['Attributes'] as $name=>$value) {
$name = lcfirst((string)$name);
$value = (string)$value;
if(in_array($name, $m->attributeNames())){
$m->$name = $value;
}
}
}
$msgs[]=$m;
}
}
if(isset($options['MaxNumberOfMessages'])){
return $msgs;
} else {
return empty($msgs) ? null : array_pop($msgs);
}
}
/**
* Delete a message from a queue
*
* @param string $url url of the queue
* @param mixed $receiptHandle AWSMessage contain the receiptHandle or the receipthandle for the message
* @return boolean if message was delete succesfull
*/
public function delete($url, $handle, $options=array())
{
$this->_sqs->deleteMessage(array_merge(array(
'QueueUrl' => $url,
'ReceiptHandle' => $handle,
),$options));
return true; // If delete failed the above would throw an exception
}
/**
* Deletes a batch of messages
* @param type $url The url of the queue
* @param array $handles An array of messages or handles to delete
* @param type $options
* @return boolean if the delete was sucessful or not
*/
public function deleteBatch($url, $handles, $options=array())
{
$deleteRequest = array();
foreach ($handles as $key => $handle) {
$receiptHandle = ($handle instanceof AWSMessage) ? $handle->receiptHandle : $handle;
$req = array('Id'=>$key,'ReceiptHandle'=>$receiptHandle);
array_push($deleteRequest, $req);
}
$result = $this->_sqs->deleteMessageBatch(array_merge(array(
'QueueUrl' => $url,
'Entries' => $deleteRequest,
),$options));
$fails = $result->get('Failed');
return empty($fails);
}
/**
* Create a new queue
*
* @return mixed AWSQueue object if creation was succesfull, null else
*/
public function createQueue($name)
{
$result = $this->_sqs->createQueue(array('QueueName' => $name));
$queueUrl = $result->get('QueueUrl');
$q=new AWSQueue($this, $queueUrl);
$this->queues->add($q->name, $q);
return $q;
}
/**
* Delete a queue
*/
public function deleteQueue($url)
{
$this->_sqs->deleteQueue(array('QueueUrl' => $url));
return true; // If delete failed the above would throw an exception
}
}