From f080bd3e68477c77f75559bdb35954f9ce16bbc0 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 28 Oct 2022 03:36:20 +0000 Subject: [PATCH] using dependency injection in CLI --- app/cli.php | 41 +++++++++++++++++++++++++++++++ composer.lock | 4 +-- src/Appwrite/CLI/Tasks/Doctor.php | 8 +++--- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/app/cli.php b/app/cli.php index ef0445e67f..3060adc636 100644 --- a/app/cli.php +++ b/app/cli.php @@ -4,11 +4,52 @@ require_once __DIR__ . '/init.php'; require_once __DIR__ . '/controllers/general.php'; use Appwrite\CLI\Tasks; +use Utopia\CLI\CLI; use Utopia\Database\Validator\Authorization; use Utopia\Platform\Service; +use Utopia\App; +use Utopia\CLI\Console; +use Utopia\Cache\Adapter\Redis as RedisCache; +use Utopia\Cache\Cache; +use Utopia\Database\Adapter\MariaDB; +use Utopia\Database\Database; Authorization::disable(); +CLI::setResource('register', fn()=>$register); + +$attempts = 0; +$max = 10; +$sleep = 1; + +do { + try { + $attempts++; + $db = $register->get('db'); + $redis = $register->get('cache'); + 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); + } +} while ($attempts < $max); + +CLI::setResource('db', fn () => $db); +CLI::setResource('cache', fn () => $redis); + +CLI::setResource('dbForConsole', function ($db, $cache) { + $cache = new Cache(new RedisCache($cache)); + + $database = new Database(new MariaDB($db), $cache); + $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); + $database->setNamespace('_console'); + + return $database; +}, ['db', 'cache']); + $cliPlatform = new Tasks(); $cliPlatform->init(Service::TYPE_CLI); diff --git a/composer.lock b/composer.lock index 72180816f0..416a5c1376 100644 --- a/composer.lock +++ b/composer.lock @@ -2443,7 +2443,7 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/platform.git", - "reference": "accc17f6dd6ae404f86691e768dd73eecb0778e0" + "reference": "07e825132492c7d7c9920dbeb527833bda21155d" }, "require": { "ext-json": "*", @@ -2489,7 +2489,7 @@ "upf", "utopia" ], - "time": "2022-10-28T02:27:04+00:00" + "time": "2022-10-28T03:15:57+00:00" }, { "name": "utopia-php/preloader", diff --git a/src/Appwrite/CLI/Tasks/Doctor.php b/src/Appwrite/CLI/Tasks/Doctor.php index 85d389b846..2b728b47c8 100644 --- a/src/Appwrite/CLI/Tasks/Doctor.php +++ b/src/Appwrite/CLI/Tasks/Doctor.php @@ -10,6 +10,7 @@ use Utopia\Storage\Device\Local; use Utopia\Storage\Storage; use Utopia\Domains\Domain; use Utopia\Platform\Action; +use Utopia\Registry\Registry; class Doctor extends Action { @@ -22,13 +23,12 @@ class Doctor extends Action { $this ->desc('Validate server health') - ->callback(fn () => $this->action()); + ->inject('register') + ->callback(fn (Registry $register) => $this->action($register)); } - public function action(): void + public function action(Registry $register): void { - global $register; - Console::log(" __ ____ ____ _ _ ____ __ ____ ____ __ __ / _\ ( _ \( _ \/ )( \( _ \( )(_ _)( __) ( )/ \ / \ ) __/ ) __/\ /\ / ) / )( )( ) _) _ )(( O )