From ae9a5182509099c2fc35a6c554cc377778459793 Mon Sep 17 00:00:00 2001 From: Matej Baco Date: Wed, 16 Nov 2022 16:28:15 +0100 Subject: [PATCH 1/2] Add retry logic to CLI --- app/cli.php | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/app/cli.php b/app/cli.php index 13709b9b57..edab558c99 100644 --- a/app/cli.php +++ b/app/cli.php @@ -154,4 +154,52 @@ $platform = new Appwrite(); $platform->init(Service::TYPE_CLI); $cli = $platform->getCli(); + +$cli + ->error() + ->inject('error') + ->action(function(Throwable $error) { + Console::error($error->getMessage()); + }); + +$cli + ->init() + ->inject('pools') + ->inject('cache') + ->action(function(Group $pools, Cache $cache) { + $maxAttempts = 5; + $sleep = 3; + + $attempts = 0; + $ready = false; + + do { + $attempts++; + + // Prepare database connection + $dbAdapter = $pools + ->get('console') + ->pop() + ->getResource(); + + $dbForConsole = new Database($dbAdapter, $cache); + $dbForConsole->setNamespace('console'); + + // Ensure tables exist + $collections = Config::getParam('collections', []); + $last = \array_key_last($collections); + + if($dbForConsole->exists($dbForConsole->getDefaultDatabase(), $last)) { + $ready = true; + break; + } + + sleep($sleep); + } while ($attempts < $maxAttempts); + + if(!$ready) { + throw new Exception("Console is not ready yet. Please try again later."); + } + }); + $cli->run(); From f3245766862244d77c7398ebdc1df1f85e779d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 16 Nov 2022 19:41:01 +0000 Subject: [PATCH 2/2] Fix linter --- app/cli.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/cli.php b/app/cli.php index edab558c99..c7e5b98157 100644 --- a/app/cli.php +++ b/app/cli.php @@ -158,7 +158,7 @@ $cli = $platform->getCli(); $cli ->error() ->inject('error') - ->action(function(Throwable $error) { + ->action(function (Throwable $error) { Console::error($error->getMessage()); }); @@ -166,7 +166,7 @@ $cli ->init() ->inject('pools') ->inject('cache') - ->action(function(Group $pools, Cache $cache) { + ->action(function (Group $pools, Cache $cache) { $maxAttempts = 5; $sleep = 3; @@ -189,7 +189,7 @@ $cli $collections = Config::getParam('collections', []); $last = \array_key_last($collections); - if($dbForConsole->exists($dbForConsole->getDefaultDatabase(), $last)) { + if ($dbForConsole->exists($dbForConsole->getDefaultDatabase(), $last)) { $ready = true; break; } @@ -197,7 +197,7 @@ $cli sleep($sleep); } while ($attempts < $maxAttempts); - if(!$ready) { + if (!$ready) { throw new Exception("Console is not ready yet. Please try again later."); } });