Skip to content

Instantly share code, notes, and snippets.

@HonzaMac
Created August 1, 2017 18:56
Show Gist options
  • Save HonzaMac/6340775816d8fea1d8ccd3fe11fef719 to your computer and use it in GitHub Desktop.
Save HonzaMac/6340775816d8fea1d8ccd3fe11fef719 to your computer and use it in GitHub Desktop.
Example of usage conditional retry function for RabbitMQ
<?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