From c851214bab74834bef2f559aca725b7d04a2ccf6 Mon Sep 17 00:00:00 2001 From: Cosmin Ardeleanu Date: Tue, 29 Apr 2025 09:31:15 +0300 Subject: [PATCH] Added config option to allow disabling x-expires in getDelayQueueArguments --- README.md | 24 ++++++++++++++++++++++++ src/Queue/QueueConfig.php | 17 +++++++++++++++++ src/Queue/QueueConfigFactory.php | 5 +++++ src/Queue/RabbitMQQueue.php | 9 +++++++-- 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index adb7b2e9..9ee4669f 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,30 @@ by adding extra options. ], ``` +When you want to disable x-expires from a delayed queue, then this is possible by adding extra option on queue `expire_delay_queue`. +- When the `expire_delay_queue` option is omitted, it is considered to be true. +- Useful for when you're using quorum queues, as they risk to delete the queue before the messages are moved to their destination queue. + +```php +'connections' => [ + // ... + + 'rabbitmq' => [ + // ... + + 'options' => [ + 'queue' => [ + // ... + + 'expire_delay_queue' => false, + ], + ], + ], + + // ... +], +``` + ### Horizon support Starting with 8.0, this package supports [Laravel Horizon](https://laravel.com/docs/horizon) out of the box. Firstly, diff --git a/src/Queue/QueueConfig.php b/src/Queue/QueueConfig.php index e7ec27c4..4f17a061 100644 --- a/src/Queue/QueueConfig.php +++ b/src/Queue/QueueConfig.php @@ -30,6 +30,8 @@ class QueueConfig protected bool $quorum = false; + protected bool $expireDelayQueue = true; + protected array $options = []; /** @@ -262,6 +264,21 @@ public function setOptions(array $options): QueueConfig return $this; } + /** + * Returns &true;, if the delay queue should expire. + */ + public function hasExpireDelayQueue(): bool + { + return $this->expireDelayQueue; + } + + public function setExpireDelayQueue($expireDelayQueue): QueueConfig + { + $this->expireDelayQueue = $this->toBoolean($expireDelayQueue); + + return $this; + } + /** * Filters $value to boolean value * diff --git a/src/Queue/QueueConfigFactory.php b/src/Queue/QueueConfigFactory.php index 6f2befc5..60d0a800 100644 --- a/src/Queue/QueueConfigFactory.php +++ b/src/Queue/QueueConfigFactory.php @@ -68,6 +68,11 @@ protected static function getOptionsFromConfig(QueueConfig $queueConfig, array $ $queueConfig->setQuorum($quorum); } + // Feature: Enable/disable x-expires from delay queue. + if (! is_null($expireDelayQueue = Arr::pull($queueOptions, 'expire_delay_queue'))) { + $queueConfig->setExpireDelayQueue($expireDelayQueue); + } + // All extra options not defined $queueConfig->setOptions($queueOptions); } diff --git a/src/Queue/RabbitMQQueue.php b/src/Queue/RabbitMQQueue.php index fadedce5..591a9d8f 100644 --- a/src/Queue/RabbitMQQueue.php +++ b/src/Queue/RabbitMQQueue.php @@ -626,12 +626,17 @@ protected function getQueueArguments(string $destination): array */ protected function getDelayQueueArguments(string $destination, int $ttl): array { - return [ + $arguments = [ 'x-dead-letter-exchange' => $this->getExchange(), 'x-dead-letter-routing-key' => $this->getRoutingKey($destination), 'x-message-ttl' => $ttl, - 'x-expires' => $ttl * 2, ]; + + if ($this->getConfig()->hasExpireDelayQueue()) { + $arguments['x-expires'] = $ttl * 2; + } + + return $arguments; } /**