From 86234420755812370ac1f79de36ba974e8150d06 Mon Sep 17 00:00:00 2001 From: Alexander Mohorev Date: Fri, 6 Sep 2024 10:26:37 +0300 Subject: [PATCH] Fix redis reconnection issue (#15) --- README.md | 6 +++--- composer.json | 6 ++++++ src/Middlewares/RedisReconnectMiddleware.php | 22 ++++++++++++-------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8327768..24d2e4e 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ composer require onliner/laravel-command-bus or add this code line to the `require` section of your `composer.json` file: ``` -"onliner/laravel-command-bus": "^0.2.0" +"onliner/laravel-command-bus": "^1.1" ``` Configuration @@ -44,8 +44,8 @@ Released under the [MIT license](LICENSE). [version-badge]: https://img.shields.io/packagist/v/onliner/laravel-command-bus.svg [version-link]: https://packagist.org/packages/onliner/laravel-command-bus -[downloads-badge]: https://poser.pugx.org/onliner/laravel-command-bus/downloads.png +[downloads-badge]: https://poser.pugx.org/onliner/laravel-command-bus/downloads.svg [downloads-link]: https://packagist.org/packages/onliner/laravel-command-bus -[php-badge]: https://img.shields.io/badge/php-7.2+-brightgreen.svg +[php-badge]: https://img.shields.io/badge/php-8.0+-brightgreen.svg [php-link]: https://www.php.net/ [license-badge]: https://img.shields.io/badge/license-MIT-brightgreen.svg diff --git a/composer.json b/composer.json index 0c7b477..bade11a 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,12 @@ "illuminate/redis": ">=6.0", "onliner/command-bus": "^1.1" }, + "require-dev": { + "predis/predis": "^2.0.2" + }, + "suggest": { + "predis/predis": "Required to use the redis reconnect middleware." + }, "autoload": { "psr-4": { "Onliner\\Laravel\\CommandBus\\": "src" diff --git a/src/Middlewares/RedisReconnectMiddleware.php b/src/Middlewares/RedisReconnectMiddleware.php index f633caa..bb6cbbd 100644 --- a/src/Middlewares/RedisReconnectMiddleware.php +++ b/src/Middlewares/RedisReconnectMiddleware.php @@ -4,11 +4,12 @@ namespace Onliner\Laravel\CommandBus\Middlewares; -use Exception; use Illuminate\Redis\Connections\Connection; +use Illuminate\Redis\Connections\PredisConnection; use Illuminate\Redis\RedisManager; use Onliner\CommandBus\Context; use Onliner\CommandBus\Middleware; +use Predis\Connection\ConnectionException; class RedisReconnectMiddleware implements Middleware { @@ -20,21 +21,24 @@ public function call(object $message, Context $context, callable $next): void { $connections = $this->redis->connections() ?: []; - foreach ($connections as $name => $connection) { - if (!$this->ping($connection)) { - $this->redis->purge($name); - } + foreach ($connections as $connection) { + $this->ping($connection); } $next($message, $context); } - private function ping(Connection $connection, string $msg = 'ok'): bool + private function ping(Connection $connection): void { + if (!$connection instanceof PredisConnection) { + return; + } + try { - return $connection->ping($msg) === $msg; - } catch (Exception) { - return false; + $connection->ping(); + } catch (ConnectionException) { + $connection->disconnect(); + $connection->connect(); } } }