From 073e4742eb324f20ff731cfe26f2eeeb267dbf9c Mon Sep 17 00:00:00 2001 From: "richard.gooding" Date: Tue, 19 Sep 2017 16:44:50 +0100 Subject: [PATCH] Now attempts to declare and bind the queue and exchange up to 2 times in case the binding doesn't work the first time around (seen on RabbitMQ 3.6.9 and 3.6.12) --- src/Provider/Amqp/AmqpQueueProvider.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Provider/Amqp/AmqpQueueProvider.php b/src/Provider/Amqp/AmqpQueueProvider.php index c667a38..54f2f1f 100644 --- a/src/Provider/Amqp/AmqpQueueProvider.php +++ b/src/Provider/Amqp/AmqpQueueProvider.php @@ -96,9 +96,12 @@ public function pushBatch(array $batch, $persistent = null) $mandatory = $this->_getMandatoryFlag(); $autoDeclare = $this->_getAutoDeclare(); $publishConfirm = $this->_getPublishConfirm(); + // max no. of times to attempt declaring the queue + $declareRetryLimit = 2; $needRetry = true; $needDeclare = false; + $declareAttempts = 0; $returnCallback = null; if($mandatory) @@ -108,8 +111,15 @@ public function pushBatch(array $batch, $persistent = null) $replyText, $exchange, $routingKey - ) use (&$needRetry, &$needDeclare, &$autoDeclare) { - if($autoDeclare && (!$needDeclare) && ($replyCode == 312)) + ) use ( + &$needRetry, &$needDeclare, &$autoDeclare, + $declareAttempts, $declareRetryLimit + ) + { + if($autoDeclare + && ($declareAttempts < $declareRetryLimit) + && ($replyCode == 312) + ) { $needDeclare = true; $needRetry = true; @@ -136,6 +146,7 @@ public function pushBatch(array $batch, $persistent = null) if($needDeclare) { $this->_log("Auto-declaring exchange and queue"); + $declareAttempts++; $this->declareExchange(); $this->declareQueue(); $this->bindQueue(); @@ -170,7 +181,10 @@ public function pushBatch(array $batch, $persistent = null) catch(\Exception $e) { $this->disconnectAll(); - if($autoDeclare && (!$needDeclare) && ($e->getCode() == 404)) + if($autoDeclare + && ($declareAttempts < $declareRetryLimit) + && ($e->getCode() == 404) + ) { $needRetry = true; $needDeclare = true;