$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']); /** @var InfluxDB\Client $client */ $client = $register->get('influxdb'); $attempts = 0; $max = 10; $sleep = 1; do { // check if telegraf database is ready try { $attempts++; $database = $client->selectDB('telegraf'); if (in_array('telegraf', $client->listDatabases())) { break; // leave the do-while if successful } } catch (\Throwable$th) { Console::warning("InfluxDB not ready. Retrying connection ({$attempts})..."); if ($attempts >= $max) { throw new \Exception('InfluxDB database not ready yet'); } sleep($sleep); } } while ($attempts < $max); CLI::setResource('influxdb', fn() => $database); $cliPlatform = new Tasks(); $cliPlatform->init(Service::TYPE_CLI); $cli = $cliPlatform->getCli(); $cli->run();