1
0
Fork 0
mirror of synced 2024-06-02 19:04:49 +12:00

Attempt to reconnect five times before throwing exception

This commit is contained in:
kodumbeats 2021-07-20 13:01:47 -04:00
parent 8a205d1b63
commit 9870c6fdff

View file

@ -70,15 +70,20 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) {
});
// wait for database to be ready
sleep(5);
try {
$dbForConsole = $app->getResource('dbForConsole'); /** @var Utopia\Database\Database $dbForConsole */
} catch(\Exception $e) {
Console::warning('Database not ready. Retrying connection...');
sleep(5);
$dbForConsole = $app->getResource('dbForConsole'); /** @var Utopia\Database\Database $dbForConsole */
}
$attempts = 0;
do {
try {
$dbForConsole = $app->getResource('dbForConsole'); /** @var Utopia\Database\Database $dbForConsole */
break; // leave the do-while if successful
} catch(\Exception $e) {
$attempts++;
Console::warning("Database not ready. Retrying connection ({$attempts})...");
if ($attempts >= 5) {
throw new \Exception('Failed to connect to database: '. $e->getMessage());
}
sleep(5);
}
} while (!$dbForConsole || $attempts < 5);
if(!$dbForConsole->exists()) {
Console::success('[Setup] - Server database init started...');