1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\Queueing\Plugins\SQSBundle\Adapter\SQS; |
4
|
|
|
|
5
|
|
|
use Kaliop\QueueingBundle\Queue\ProducerInterface; |
6
|
|
|
use Aws\Sqs\SqsClient; |
7
|
|
|
use Aws\TraceMiddleware; |
8
|
|
|
|
9
|
|
|
class Producer implements ProducerInterface |
10
|
|
|
{ |
11
|
|
|
/** @var \Aws\Sqs\SqsClient */ |
12
|
|
|
protected $client; |
13
|
|
|
protected $queueUrl; |
14
|
|
|
protected $debug = false; |
15
|
|
|
protected $contentType = 'text/plain'; |
16
|
|
|
// The message attribute used to store content-type. To be kept in sync with the Consumer |
17
|
|
|
protected $contentTypeAttribute = 'contentType'; |
18
|
|
|
protected $routingKeyAttribute = 'routingKey'; |
19
|
|
|
protected $messageGroupId; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array $config - minimum seems to be: 'credentials', 'region', 'version' |
23
|
|
|
* @see \Aws\AwsClient::__construct for the full list |
24
|
|
|
* @see http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/configuration.html |
25
|
|
|
*/ |
26
|
9 |
|
public function __construct(array $config) |
27
|
|
|
{ |
28
|
9 |
|
$this->client = new SqsClient($config); |
29
|
9 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Enabled debug. At the moment can not disable it |
33
|
|
|
* |
34
|
|
|
* @param $debug |
35
|
|
|
* @return $this |
36
|
|
|
* |
37
|
|
|
* @todo test if using $handlerList->removeByInstance we can disable debug as well |
38
|
|
|
*/ |
39
|
9 |
View Code Duplication |
public function setDebug($debug) { |
|
|
|
|
40
|
9 |
|
if ($debug == $this->debug) { |
41
|
9 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
if ($debug) { |
44
|
|
|
$handlerList = $this->client->getHandlerList(); |
45
|
|
|
$handlerList->interpose(new TraceMiddleware($debug === true ? [] : $debug)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $queueName NB: complete queue name as used by SQS |
|
|
|
|
53
|
|
|
* @param string $queueUrl |
54
|
|
|
* @return Producer |
55
|
|
|
* @todo test that we can successfully send messages to 2 queues using the same SqsClient |
56
|
|
|
*/ |
57
|
9 |
|
public function setQueueUrl($queueUrl) |
58
|
|
|
{ |
59
|
9 |
|
$this->queueUrl = $queueUrl; |
60
|
|
|
|
61
|
9 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string the complete queue name as used by SQS |
66
|
|
|
*/ |
67
|
9 |
|
public function getQueueUrl() |
68
|
|
|
{ |
69
|
9 |
|
return $this->queueUrl; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function setMessageGroupId($messageGroupId) |
73
|
|
|
{ |
74
|
|
|
$this->messageGroupId = $messageGroupId; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Publishes the message and does nothing with the properties |
79
|
|
|
* |
80
|
|
|
* @param string $msgBody |
81
|
|
|
* @param string $routingKey |
82
|
|
|
* @param array $additionalProperties see https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#sendmessage |
83
|
|
|
* |
84
|
|
|
* @todo support custom message attributes (possible via $additionalProperties) |
85
|
|
|
* @todo support custom delaySeconds (possible via $additionalProperties) |
86
|
|
|
* @todo support custom MessageDeduplicationId (possible via $additionalProperties) |
87
|
|
|
*/ |
88
|
4 |
|
public function publish($msgBody, $routingKey = '', $additionalProperties = array()) |
89
|
|
|
{ |
90
|
4 |
|
$this->client->sendMessage(array_merge( |
91
|
|
|
array( |
92
|
4 |
|
'QueueUrl' => $this->queueUrl, |
93
|
4 |
|
'MessageBody' => $msgBody, |
94
|
|
|
), |
95
|
4 |
|
$this->getClientParams($routingKey, $additionalProperties) |
96
|
|
|
)); |
97
|
4 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @see http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#sendmessagebatch |
101
|
|
|
* @param array[] $messages each element is an array that must contain: |
102
|
|
|
* - msgBody (string) |
103
|
|
|
* - routingKey (string, optional) |
104
|
|
|
* - additionalProperties (array, optional) |
105
|
|
|
*/ |
106
|
|
|
public function batchPublish(array $messages) |
107
|
|
|
{ |
108
|
|
|
$j = 0; |
109
|
|
|
for ($i = 0; $i < count($messages); $i += 10) { |
|
|
|
|
110
|
|
|
$entries = array(); |
111
|
|
|
$toSend = array_slice($messages, $i, 10); |
112
|
|
|
foreach($toSend as $message) { |
113
|
|
|
$entries[] = array_merge( |
114
|
|
|
array( |
115
|
|
|
'MessageBody' => $message['msgBody'], |
116
|
|
|
'Id' => $j++ |
117
|
|
|
), |
118
|
|
|
$this->getClientParams(@$message['routingKey'], @$message['additionalProperties']) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$result = $this->client->sendMessageBatch( |
123
|
|
|
array( |
124
|
|
|
'QueueUrl' => $this->queueUrl, |
125
|
|
|
'Entries' => $entries, |
126
|
|
|
) |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
if (($ok = count($result->get('Successful'))) != ($tot = count($toSend))) { |
130
|
|
|
throw new \RuntimeException("Batch sending of messages failed - $ok ok out of $tot"); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Allows callers to do whatever they want with the client - useful to the Queue Mgr |
137
|
|
|
* |
138
|
|
|
* @param string $method |
139
|
|
|
* @param array $args |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
9 |
|
public function call($method, array $args = array()) |
143
|
|
|
{ |
144
|
9 |
|
return $this->client->$method(array_merge($args, $this->getClientParams())); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Prepares the extra parameters to be injected into calls made via the SQS Client |
149
|
|
|
* @param string $routingKey |
150
|
|
|
* @param array $additionalProperties |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
9 |
|
protected function getClientParams($routingKey = '', array $additionalProperties = array()) |
154
|
|
|
{ |
155
|
|
|
$result = array( |
156
|
|
|
'MessageAttributes' => array( |
157
|
9 |
|
$this->contentTypeAttribute => array('StringValue' => $this->contentType, 'DataType' => 'String'), |
158
|
|
|
) |
159
|
|
|
); |
160
|
9 |
|
if ($routingKey != '') { |
161
|
3 |
|
$result['MessageAttributes'][$this->routingKeyAttribute] = array('StringValue' => $routingKey, 'DataType' => 'String'); |
162
|
|
|
} |
163
|
9 |
|
if ($this->messageGroupId != null) { |
164
|
|
|
$result['MessageGroupId'] = $this->messageGroupId; |
165
|
|
|
} |
166
|
|
|
|
167
|
9 |
|
$result = array_merge($result, $additionalProperties); |
168
|
|
|
|
169
|
9 |
|
return $result; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $contentType |
174
|
|
|
* @return Producer |
175
|
|
|
* @throws \Exception if unsupported contentType is used |
176
|
|
|
*/ |
177
|
4 |
|
public function setContentType($contentType) |
178
|
|
|
{ |
179
|
4 |
|
$this->contentType = $contentType; |
180
|
|
|
|
181
|
4 |
|
return $this; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.