From 0a63658d6bb701b37585abd3cf467df5e1ed9c97 Mon Sep 17 00:00:00 2001 From: werewolf81 Date: Thu, 29 Jun 2017 09:34:13 +0100 Subject: [PATCH 1/2] Added an option to throw an exception if the connection to RabbitMQ was lost or became corrupted. --- .../LaravelQueueRabbitMQ/Queue/RabbitMQQueue.php | 7 +++++++ src/config/rabbitmq.php | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/VladimirYuldashev/LaravelQueueRabbitMQ/Queue/RabbitMQQueue.php b/src/VladimirYuldashev/LaravelQueueRabbitMQ/Queue/RabbitMQQueue.php index 6f13eea1..f2d4c674 100644 --- a/src/VladimirYuldashev/LaravelQueueRabbitMQ/Queue/RabbitMQQueue.php +++ b/src/VladimirYuldashev/LaravelQueueRabbitMQ/Queue/RabbitMQQueue.php @@ -324,10 +324,17 @@ public function getCorrelationId() /** * @param string $action * @param Exception $e + * @throws Exception */ protected function reportConnectionError($action, Exception $e) { Log::error('AMQP error while attempting '.$action.': '.$e->getMessage()); + + // If it's set to false, throw an error rather than waiting + if ($this->sleepOnError === false) { + throw new \Exception('Error writing data to the connection with RabbitMQ'); + } + // Sleep so that we don't flood the log file sleep($this->sleepOnError); } diff --git a/src/config/rabbitmq.php b/src/config/rabbitmq.php index fa428c32..c1390956 100644 --- a/src/config/rabbitmq.php +++ b/src/config/rabbitmq.php @@ -40,6 +40,8 @@ 'auto_delete' => env('RABBITMQ_EXCHANGE_AUTODELETE', false), ], - 'sleep_on_error' => env('RABBITMQ_ERROR_SLEEP', 5), // the number of seconds to sleep if there's an error communicating with rabbitmq + // the number of seconds to sleep if there's an error communicating with rabbitmq + // if set to false, it'll throw an exception rather than doing the sleep for X seconds + 'sleep_on_error' => env('RABBITMQ_ERROR_SLEEP', 5), ]; From a2db1e6eacf1ac8f5044d6b398c21b96773b67fd Mon Sep 17 00:00:00 2001 From: werewolf81 Date: Thu, 29 Jun 2017 09:42:50 +0100 Subject: [PATCH 2/2] Changed the thrown \Exception to a \RuntimeException instead. --- .../LaravelQueueRabbitMQ/Queue/RabbitMQQueue.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/VladimirYuldashev/LaravelQueueRabbitMQ/Queue/RabbitMQQueue.php b/src/VladimirYuldashev/LaravelQueueRabbitMQ/Queue/RabbitMQQueue.php index f2d4c674..3bd29dd2 100644 --- a/src/VladimirYuldashev/LaravelQueueRabbitMQ/Queue/RabbitMQQueue.php +++ b/src/VladimirYuldashev/LaravelQueueRabbitMQ/Queue/RabbitMQQueue.php @@ -332,7 +332,7 @@ protected function reportConnectionError($action, Exception $e) // If it's set to false, throw an error rather than waiting if ($this->sleepOnError === false) { - throw new \Exception('Error writing data to the connection with RabbitMQ'); + throw new \RuntimeException('Error writing data to the connection with RabbitMQ'); } // Sleep so that we don't flood the log file