This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
forked from PHPNuts/yii2-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqsQueue.php
106 lines (92 loc) · 2.26 KB
/
SqsQueue.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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\queue;
use Aws\Sqs\SqsClient;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\helpers\Json;
/**
* SqsQueue
*
* @author Alexander Kochetov <[email protected]>
*/
class SqsQueue extends Component implements QueueInterface
{
/**
* @var SqsClient|array
*/
public $sqs;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->sqs === null) {
throw new InvalidConfigException('The "sqs" property must be set.');
}
if (!$this->sqs instanceof SqsClient) {
$this->sqs = new SqsClient($this->sqs);
}
}
/**
* @inheritdoc
*/
public function push($payload, $queue, $delay = 0)
{
return $this->sqs->sendMessage([
'QueueUrl' => $queue,
'MessageBody' => is_string($payload) ? $payload : Json::encode($payload),
'DelaySeconds' => $delay,
])->get('MessageId');
}
/**
* @inheritdoc
*/
public function pop($queue)
{
$response = $this->sqs->receiveMessage(['QueueUrl' => $queue]);
if (empty($response['Messages'])) {
return false;
}
$data = reset($response['Messages']);
return [
'id' => $data['MessageId'],
'body' => $data['Body'],
'queue' => $queue,
'receipt-handle' => $data['ReceiptHandle'],
];
}
/**
* @inheritdoc
*/
public function purge($queue) {
$this->sqs->purgeQueue(['QueueUrl' => $queue]);
}
/**
* @inheritdoc
*/
public function release(array $message, $delay = 0)
{
$this->sqs->changeMessageVisibility([
'QueueUrl' => $message['queue'],
'ReceiptHandle' => $message['receipt-handle'],
'VisibilityTimeout' => $delay,
]);
}
/**
* @inheritdoc
*/
public function delete(array $message)
{
$this->sqs->deleteMessage([
'QueueUrl' => $message['queue'],
'ReceiptHandle' => $message['receipt-handle'],
]);
}
}