8000 [Messenger] make delay exchange and queues durable like the normal on… · symfony/symfony@d6434df · GitHub
[go: up one dir, main page]

Skip to content

Commit d6434df

Browse files
committed
[Messenger] make delay exchange and queues durable like the normal ones by default
1 parent cca22c4 commit d6434df

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/Symfony/Component/Messenger/Transport/AmqpExt/Connection.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function __construct(array $connectionOptions, array $exchangeOptions, ar
6262
{
6363
$this->connectionOptions = array_replace_recursive([
6464
'delay' => [
65-
'exchange_name' => 'delay',
65+
'exchange_name' => 'delays',
6666
'queue_name_pattern' => 'delay_%exchange_name%_%routing_key%_%delay%',
6767
],
6868
], $connectionOptions);
@@ -93,7 +93,7 @@ public function __construct(array $connectionOptions, array $exchangeOptions, ar
9393
* * arguments: Extra arguments
9494
* * delay:
9595
* * queue_name_pattern: Pattern to use to create the queues (Default: "delay_%exchange_name%_%routing_key%_%delay%")
96-
* * exchange_name: Name of the exchange to be used for the delayed/retried messages (Default: "delay")
96+
* * exchange_name: Name of the exchange to be used for the delayed/retried messages (Default: "delays")
9797
* * auto_setup: Enable or not the auto-setup of queues and exchanges (Default: true)
9898
* * prefetch_count: set channel prefetch count
9999
*/
@@ -252,6 +252,11 @@ private function getDelayExchange(): \AMQPExchange
252252
$this->amqpDelayExchange = $this->amqpFactory->createExchange($this->channel());
253253
$this->amqpDelayExchange->setName($this->connectionOptions['delay']['exchange_name']);
254254
$this->amqpDelayExchange->setType(AMQP_EX_TYPE_DIRECT);
255+
if ('delays' === $this->connectionOptions['delay']['exchange_name']) {
256+
// only add the new flag when the name was not provided explicitly so we're using the new default name to prevent a redeclaration error
257+
// the condition will be removed in 4.4
258+
$this->amqpDelayExchange->setFlags(AMQP_DURABLE);
259+
}
255260
}
256261

257262
return $this->amqpDelayExchange;
@@ -274,16 +279,24 @@ private function createDelayQueue(int $delay, ?string $routingKey)
274279
[$delay, $this->exchangeOptions['name'], $routingKey ?? ''],
275280
$this->connectionOptions['delay']['queue_name_pattern']
276281
));
282+
if ('delay_%exchange_name%_%routing_key%_%delay%' === $this->connectionOptions['delay']['queue_name_pattern']) {
283+
// the condition will be removed in 4.4
284+
$queue->setFlags(AMQP_DURABLE);
285+
$extraArguments = [
286+
// delete the delay queue 10 seconds after the message expires
287+
// publishing another message redeclares the queue which renews the lease
288+
'x-expires' => $delay + 10000,
289+
];
290+
} else {
291+
$extraArguments = [];
292+
}
277293
$queue->setArguments([
278294
'x-message-ttl' => $delay,
279-
// delete the delay queue 10 seconds after the message expires
280-
// publishing another message redeclares the queue which renews the lease
281-
'x-expires' => $delay + 10000,
282295
'x-dead-letter-exchange' => $this->exchangeOptions['name'],
283296
// after being released from to DLX, make sure the original routing key will be used
284297
// we must use an empty string instead of null for the argument to be picked up
285298
'x-dead-letter-routing-key' => $routingKey ?? '',
286-
]);
299+
] + $extraArguments);
287300

288301
return $queue;
289302
}

0 commit comments

Comments
 (0)
0