From a52de551a1070831eec037f4f245a1f66c2b2c0b Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Mon, 19 Oct 2020 23:38:49 +0300 Subject: [PATCH] Fixed connection retry --- app/realtime.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/realtime.php b/app/realtime.php index 40ee8f572d..5bfb207bd7 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -34,17 +34,18 @@ $server->on("workerStart", function ($server, $workerId) use (&$connections) { $attempts = 0; $start = time(); - while ($attempts < 3) { + while ($attempts < 300) { try { + if($attempts > 0) { + Console::error('Connection lost (lasted '.(time() - $start).' seconds). Attempting restart in 5 seconds (attempt #'.$attempts.')'); + sleep(5); // 1 sec delay between connection attempts + } + $redis = new Redis(); $redis->connect('redis', 6379); $redis->setOption(Redis::OPT_READ_TIMEOUT, -1); - if($attempts > 0) { - Console::error('Connection lost (lasted '.(time() - $start).' seconds). Attempting restart (attempt #'.$attempts.')'); - } - - if($redis->ping('')) { + if($redis->ping(true)) { $attempts = 0; Console::success('Connection established'); } @@ -52,8 +53,6 @@ $server->on("workerStart", function ($server, $workerId) use (&$connections) { Console::error('Connection failed'); } - sleep(1); // 1 sec delay between connection attempts - $redis->subscribe(['realtime'], function($redis, $channel, $message) use ($server, $workerId, &$connections) { $message = 'Message from worker #'.$workerId.'; '.$message; @@ -74,12 +73,13 @@ $server->on("workerStart", function ($server, $workerId) use (&$connections) { } }); - $attempts++; - } catch (\Throwable $th) { + Console::error('Connection error: '.$th->getMessage()); $attempts++; continue; } + + $attempts++; } Console::error('Failed to restart connection...');