Created
August 1, 2017 18:56
-
-
Save HonzaMac/6340775816d8fea1d8ccd3fe11fef719 to your computer and use it in GitHub Desktop.
Example of usage conditional retry function for RabbitMQ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PhpAmqpLib\Exception\AMQPIOException; | |
use PhpAmqpLib\Exception\AMQPProtocolException; | |
use PhpAmqpLib\Exception\AMQPRuntimeException; | |
retryConditional( | |
function () use ($request, $readTimeout) : Response { | |
return $this->doSend($request, $readTimeout); // sends message to RabbitMQ | |
}, | |
function ($result, ?Throwable $ex) { | |
if ($ex instanceof AMQPRuntimeException || $ex instanceof AMQPProtocolException || ($ex instanceof ErrorException && $ex->getMessage( | |
) === 'socket_write(): unable to write to socket [32]: Broken pipe') || ($ex instanceof AMQPIOException)) { | |
pcntl_signal_dispatch(); | |
$this->logger->addWarning('Processing RabbitMQ reconnect due to: ' . $ex->getMessage()); | |
$this->reconnect(); | |
if (!$this->connection->isConnected()) { | |
sleep(2); // Sleep for a while to prevent Rabbit reconnect DoS | |
} | |
return true; | |
} | |
return false; | |
}, | |
30 | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment