Skip to content

Commit

Permalink
Fix redis reconnection issue (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohorev authored Sep 6, 2024
1 parent c6670d7 commit 8623442
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 13 additions & 9 deletions src/Middlewares/RedisReconnectMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
}
}
}

0 comments on commit 8623442

Please sign in to comment.