From 806ac22aa7bdb4b01df2d34c48e8b2e79cc64824 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 31 Aug 2023 11:44:54 +0300 Subject: [PATCH] add rounds to connection attempts and adjust attempts sleep between rounds --- app/http.php | 57 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/app/http.php b/app/http.php index 053db20ffc..62999bdf11 100644 --- a/app/http.php +++ b/app/http.php @@ -66,24 +66,47 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) { App::setResource('pools', fn () => $pools); // wait for database to be ready - $attempts = 0; - $max = 10; - $sleep = 1; - - do { - try { - $attempts++; - $dbForConsole = $app->getResource('dbForConsole'); - /** @var Utopia\Database\Database $dbForConsole */ - break; // leave the do-while if successful - } catch (\Exception $e) { - Console::warning("Database not ready. Retrying connection ({$attempts})..."); - if ($attempts >= $max) { - throw new \Exception('Failed to connect to database: ' . $e->getMessage()); - } - sleep($sleep); + $rounds = 0; + $max = 10; //max attempts per round + $sleep = 1; // initial sleep value + $maxRounds = 2; // max round + $delay = 5; // delay each round + $sleepInc = 5; // Increment sleep after each round + $leave = false; // exit rounds loop + while (true) { + if ($leave === true) { + break; } - } while ($attempts < $max); + + $attempts = 0; + + do { + try { + $attempts++; + + if ($attempts === $max) { + $sleep = + $sleepInc; + } + + $dbForConsole = $app->getResource('dbForConsole'); + + /** @var Utopia\Database\Database $dbForConsole */ + $leave = true; + break; // leave the do-while if successful + } catch (\Exception $e) { + Console::warning("Database not ready. Retrying connection ({$attempts})..."); + + if ($attempts >= $max && $rounds >= $maxRounds) { + throw new \Exception('Failed to connect to database: ' . $e->getMessage()); + } + + sleep($sleep); + } + } while ($attempts < $max); + + $rounds++; + sleep($delay); + } Console::success('[Setup] - Server database init started...');