Skip to content

Commit

Permalink
Added reconnection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
eldadfux committed Apr 22, 2024
1 parent 309796b commit 8749796
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ public function start(): self
{
try {
$this->adapter->onWorkerStart(function (string $workerId) {
// Check if the connection is ready
$retryAttempts = 30;
$retryDelay = 1; // seconds

while (!$this->adapter->connection->ping()) {
if ($retryAttempts <= 0) {
Console::error("[Worker] connection is not ready. Exiting...");
return $this;
}

$retryAttempts--;

Console::warning("[Worker] connection is not ready. Retrying in {$retryDelay} seconds [{$retryAttempts} left] ...");

sleep($retryDelay);
}

Console::success("[Worker] Worker {$workerId} is ready!");

while (true) {
Expand Down
6 changes: 3 additions & 3 deletions tests/Queue/servers/Swoole/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function isValid(mixed $value): bool
return true;
}
}
$jobs = 100000;
$jobs = 10000000;
$sleep = 0;
$connection = new Redis('redis', 6379);
$client = new Client('swoole', $connection);
Expand All @@ -92,9 +92,9 @@ public function isValid(mixed $value): bool
sleep($sleep);

$container = new Container();
$connection = new Queue\Adapter\Swoole\Redis('redis');
// $connection = new Queue\Adapter\Swoole\Redis('redis');
$connection = new Queue\Connection\Redis('redis');
$adapter = new Queue\Adapter\Swoole\Server($connection, 9000, 'swoole');
$adapter = new Queue\Adapter\Swoole\Server($connection, 1000, 'swoole');
$server = new Queue\Worker($adapter);
$server->setContainer($container);

Expand Down

0 comments on commit 8749796

Please sign in to comment.