18
18
*/
19
19
class Connection
20
20
{
21
- private $ connectionCredentials ;
21
+ private $ connectionConfiguration ;
22
22
private $ exchangeConfiguration ;
23
23
private $ queueConfiguration ;
24
24
private $ debug ;
@@ -44,9 +44,45 @@ class Connection
44
44
*/
45
45
private $ amqpRetryExchange ;
46
46
47
- public function __construct (array $ connectionCredentials , array $ exchangeConfiguration , array $ queueConfiguration , bool $ debug = false , AmqpFactory $ amqpFactory = null )
47
+ /**
48
+ * Available options:
49
+ *
50
+ * * host: Hostname of the AMQP service
51
+ * * port: Port of the AMQP service
52
+ * * vhost: Virtual Host to use with the AMQP service
53
+ * * user: Username to use to connect the the AMQP service
54
+ * * password: Username to use the connect to the AMQP service
55
+ * * queue:
56
+ * * name: Name of the queue
57
+ * * routing_key: The routing key (if any) to use to push the messages to
58
+ * * flags: Queue flags (Default: AMQP_DURABLE)
59
+ * * arguments: Extra arguments
60
+ * * exchange:
61
+ * * name: Name of the exchange
62
+ * * type: Type of exchange (Default: fanout)
63
+ * * flags: Exchange flags (Default: AMQP_DURABLE)
64
+ * * arguments: Extra arguments
65
+ * * retry:
66
+ * * attempts: Number of times it will try to retry
67
+ * * routing_key_pattern: The pattern of the routing key (Default: "attempt_%attempt%")
68
+ * * dead_queue: Name of the queue in which messages that retry more than attempts time are pushed to
69
+ * * dead_routing_key: Routing key name for the dead queue (Default: "dead")
70
+ * * queue_name_pattern: Pattern to use to create the queues (Default: "retry_queue_%attempt%")
71
+ * * exchange_name: Name of the exchange to be used for the retried messages (Default: "retry")
72
+ * * ttl: Key-value pairs of attempt number -> seconds to wait. If not configured, 10 seconds will be waited each attempt.
73
+ * * auto-setup: Enable or not the auto-setup of queues and exchanges (Default: true)
74
+ * * loop_sleep: Amount of micro-seconds to wait if no message are available (Default: 200000)
75
+ */
76
+ public function __construct (array $ connectionConfiguration , array $ exchangeConfiguration , array $ queueConfiguration , bool $ debug = false , AmqpFactory $ amqpFactory = null )
48
77
{
49
- $ this ->connectionCredentials = $ connectionCredentials ;
78
+ $ this ->connectionConfiguration = array_replace_recursive (array (
79
+ 'retry ' => array (
80
+ 'routing_key_pattern ' => 'attempt_%attempt% ' ,
81
+ 'dead_routing_key ' => 'dead ' ,
82
+ 'exchange_name ' => 'retry ' ,
83
+ 'queue_name_pattern ' => 'retry_queue_%attempt% ' ,
84
+ ),
85
+ ), $ connectionConfiguration );
50
86
$ this ->debug = $ debug ;
51
87
$ this ->exchangeConfiguration = $ exchangeConfiguration ;
52
88
$ this ->queueConfiguration = $ queueConfiguration ;
@@ -111,26 +147,26 @@ public function publish(string $body, array $headers = array()): void
111
147
*/
112
148
public function publishForRetry (\AMQPEnvelope $ message ): bool
113
149
{
114
- if (!isset ($ this ->connectionCredentials ['retry ' ])) {
150
+ if (!isset ($ this ->connectionConfiguration ['retry ' ])) {
115
151
return false ;
116
152
}
117
153
118
- $ retryConfiguration = $ this ->connectionCredentials ['retry ' ];
154
+ $ retryConfiguration = $ this ->connectionConfiguration ['retry ' ];
119
155
$ attemptNumber = ((int ) $ message ->getHeader ('symfony-messenger-attempts ' ) ?: 0 ) + 1 ;
120
156
121
157
if ($ this ->shouldSetup ()) {
122
158
$ this ->setupRetry ($ retryConfiguration , $ attemptNumber );
123
159
}
124
160
125
161
$ maximumAttempts = $ retryConfiguration ['attempts ' ] ?? 3 ;
126
- $ routingKey = str_replace ('%attempt% ' , $ attemptNumber , $ retryConfiguration ['routing_key_pattern ' ] ?? ' attempt_%attempt% ' );
162
+ $ routingKey = str_replace ('%attempt% ' , $ attemptNumber , $ retryConfiguration ['routing_key_pattern ' ]);
127
163
128
164
if ($ attemptNumber > $ maximumAttempts ) {
129
165
if (!isset ($ retryConfiguration ['dead_queue ' ])) {
130
166
return false ;
131
167
}
132
168
133
- $ routingKey = $ retryConfiguration ['dead_routing_key ' ] ?? ' dead ' ;
169
+ $ routingKey = $ retryConfiguration ['dead_routing_key ' ];
134
170
}
135
171
136
172
$ this ->retryExchange ($ retryConfiguration )->publish (
@@ -154,21 +190,21 @@ private function setupRetry(array $retryConfiguration, int $attemptNumber)
154
190
155
191
$ queue = $ this ->retryQueue ($ retryConfiguration , $ attemptNumber );
156
192
$ queue ->declareQueue ();
157
- $ queue ->bind ($ exchange ->getName (), str_replace ('%attempt% ' , $ attemptNumber , $ retryConfiguration ['routing_key_pattern ' ] ?? ' attempt_%attempt% ' ));
193
+ $ queue ->bind ($ exchange ->getName (), str_replace ('%attempt% ' , $ attemptNumber , $ retryConfiguration ['routing_key_pattern ' ]));
158
194
159
195
if (isset ($ retryConfiguration ['dead_queue ' ])) {
160
196
$ queue = $ this ->amqpFactory ->createQueue ($ this ->channel ());
161
197
$ queue ->setName ($ retryConfiguration ['dead_queue ' ]);
162
198
$ queue ->declareQueue ();
163
- $ queue ->bind ($ exchange ->getName (), $ retryConfiguration ['dead_routing_key ' ] ?? ' dead ' );
199
+ $ queue ->bind ($ exchange ->getName (), $ retryConfiguration ['dead_routing_key ' ]);
164
200
}
165
201
}
166
202
167
203
private function retryExchange (array $ retryConfiguration )
168
204
{
169
205
if (null === $ this ->amqpRetryExchange ) {
170
206
$ this ->amqpRetryExchange = $ this ->amqpFactory ->createExchange ($ this ->channel ());
171
- $ this ->amqpRetryExchange ->setName ($ retryConfiguration ['name ' ] ?? ' retry ' );
207
+ $ this ->amqpRetryExchange ->setName ($ retryConfiguration ['exchange_name ' ] );
172
208
$ this ->amqpRetryExchange ->setType (AMQP_EX_TYPE_DIRECT );
173
209
}
174
210
@@ -178,9 +214,9 @@ private function retryExchange(array $retryConfiguration)
178
214
private function retryQueue (array $ retryConfiguration , int $ attemptNumber )
179
215
{
180
216
$ queue = $ this ->amqpFactory ->createQueue ($ this ->channel ());
181
- $ queue ->setName (str_replace ('%attempt% ' , $ attemptNumber , $ retryConfiguration ['queue_name_pattern ' ] ?? ' retry_queue_%attempt% ' ));
217
+ $ queue ->setName (str_replace ('%attempt% ' , $ attemptNumber , $ retryConfiguration ['queue_name_pattern ' ]));
182
218
$ queue ->setArguments (array (
183
- 'x-message-ttl ' => $ retryConfiguration ['ttls ' ][$ attemptNumber - 1 ] ?? 30000 , // 30 seconds by default
219
+ 'x-message-ttl ' => $ retryConfiguration ['ttl ' ][$ attemptNumber - 1 ] ?? 10000 , // 10 seconds by default
184
220
'x-dead-letter-exchange ' => $ this ->exchange ()->getName (),
185
221
));
186
222
@@ -250,8 +286,8 @@ public function setup(): void
250
286
public function channel (): \AMQPChannel
251
287
{
252
288
if (null === $ this ->amqpChannel ) {
253
- $ connection = $ this ->amqpFactory ->createConnection ($ this ->connectionCredentials );
254
- $ connectMethod = 'true ' === ($ this ->connectionCredentials ['persistent ' ] ?? 'false ' ) ? 'pconnect ' : 'connect ' ;
289
+ $ connection = $ this ->amqpFactory ->createConnection ($ this ->connectionConfiguration );
290
+ $ connectMethod = 'true ' === ($ this ->connectionConfiguration ['persistent ' ] ?? 'false ' ) ? 'pconnect ' : 'connect ' ;
255
291
256
292
if (false === $ connection ->{$ connectMethod }()) {
257
293
throw new \AMQPException ('Could not connect to the AMQP server. Please verify the provided DSN. ' );
@@ -294,9 +330,9 @@ public function exchange(): \AMQPExchange
294
330
return $ this ->amqpExchange ;
295
331
}
296
332
297
- public function getConnectionCredentials (): array
333
+ public function getConnectionConfiguration (): array
298
334
{
299
- return $ this ->connectionCredentials ;
335
+ return $ this ->connectionConfiguration ;
300
336
}
301
337
302
338
private function clear (): void
@@ -308,6 +344,6 @@ private function clear(): void
308
344
309
345
private function shouldSetup (): bool
310
346
{
311
- return !array_key_exists ('auto-setup ' , $ this ->connectionCredentials ) || !in_array ($ this ->connectionCredentials ['auto-setup ' ], array (false , 'false ' ), true );
347
+ return !array_key_exists ('auto-setup ' , $ this ->connectionConfiguration ) || !in_array ($ this ->connectionConfiguration ['auto-setup ' ], array (false , 'false ' ), true );
312
348
}
313
349
}
0 commit comments