Skip to content

Commit

Permalink
Merge pull request #11 from packaged/multiple-declare
Browse files Browse the repository at this point in the history
Now attempts to declare and bind the queue and exchange up to 2 times…
  • Loading branch information
bajb committed Sep 19, 2017
2 parents fa935fb + 073e474 commit 1dc879d
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Provider/Amqp/AmqpQueueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1dc879d

Please sign in to comment.