using dependency injection in CLI
This commit is contained in:
parent
904b8aca8d
commit
f080bd3e68
41
app/cli.php
41
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);
|
||||
|
||||
|
|
4
composer.lock
generated
4
composer.lock
generated
|
@ -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",
|
||||
|
|
|
@ -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 )
|
||||
|
|
Loading…
Reference in a new issue