From e16bfe7fd3fd25c17fb96ef7b541dfb9d0cb0e52 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 20 Jul 2021 14:59:58 -0400 Subject: [PATCH] Move reconnection loop to first call to db --- app/http.php | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/app/http.php b/app/http.php index 9b42ccff9f..b85d6a051b 100644 --- a/app/http.php +++ b/app/http.php @@ -54,8 +54,23 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) { $app = new App('UTC'); go(function() use ($register, $app) { - $db = $register->get('dbPool')->get(); - $redis = $register->get('redisPool')->get(); + // wait for database to be ready + $attempts = 0; + do { + try { + $attempts++; + $db = $register->get('dbPool')->get(); + $redis = $register->get('redisPool')->get(); + break; // leave the do-while if successful + } catch(\Exception $e) { + Console::warning("Database not ready. Retrying connection ({$attempts})..."); + if ($attempts >= 10) { + throw new \Exception('Failed to connect to database: '. $e->getMessage()); + } + sleep(1); + continue; + } + } while ($attempts < 10); App::setResource('db', function () use (&$db) { return $db; @@ -69,21 +84,7 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) { return $app; }); - // wait for database to be ready - $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); + $dbForConsole = $app->getResource('dbForConsole'); /** @var Utopia\Database\Database $dbForConsole */ if(!$dbForConsole->exists()) { Console::success('[Setup] - Server database init started...');