diff --git a/app/realtime.php b/app/realtime.php index 8c7bec86d9..c5ff8ccb8c 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -1,7 +1,6 @@ get('cache'))); - $consoleDB = $register->get('dbPool')->getConsoleDB(); - $database = new Database(new MariaDB($consoleDB), $cache); - $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); - $database->setNamespace('_console'); // Main DB - - if (!$database->exists($database->getDefaultDatabase(), 'certificates')) { - throw new \Exception('Console project not ready'); - } - - break; // leave loop if successful - } catch (\Exception $e) { - Console::warning("Database not ready. Retrying connection ({$attempts})..."); - if ($attempts >= DATABASE_RECONNECT_MAX_ATTEMPTS) { - throw new \Exception('Failed to connect to database: ' . $e->getMessage()); - } - sleep(DATABASE_RECONNECT_SLEEP); - } - } while ($attempts < DATABASE_RECONNECT_MAX_ATTEMPTS); - - return $database; -} - $cli ->task('maintenance') ->desc('Schedules maintenance tasks and publishes them to resque') ->action(function () { + global $register; + Console::title('Maintenance V1'); Console::success(APP_NAME . ' maintenance process v1 has started'); @@ -136,8 +102,9 @@ $cli $usageStatsRetention30m = (int) App::getEnv('_APP_MAINTENANCE_RETENTION_USAGE_30M', '129600'); //36 hours $usageStatsRetention1d = (int) App::getEnv('_APP_MAINTENANCE_RETENTION_USAGE_1D', '8640000'); // 100 days - Console::loop(function () use ($interval, $executionLogsRetention, $abuseLogsRetention, $auditLogRetention, $usageStatsRetention30m, $usageStatsRetention1d) { - $database = getConsoleDB(); + Console::loop(function () use ($register, $interval, $executionLogsRetention, $abuseLogsRetention, $auditLogRetention, $usageStatsRetention30m, $usageStatsRetention1d) { + $redis = $register->get('cache'); + $database = $register->get('dbPool')->getDB('console', $redis); $time = date('d-m-Y H:i:s', time()); Console::info("[{$time}] Notifying workers with maintenance tasks every {$interval} seconds"); diff --git a/app/tasks/migrate.php b/app/tasks/migrate.php index d0836f7bb1..0a2e78082f 100644 --- a/app/tasks/migrate.php +++ b/app/tasks/migrate.php @@ -27,18 +27,13 @@ $cli Console::success('Starting Data Migration to version ' . $version); - $db = $register->get('db', true); + $dbPool = $register->get('dbPool', true); $redis = $register->get('cache', true); $redis->flushAll(); $cache = new Cache(new RedisCache($redis)); - // TODO: Iterate through all project DBs - $projectDB = new Database(new MariaDB($db), $cache); - $projectDB->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); - - $consoleDB = new Database(new MariaDB($db), $cache); - $consoleDB->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); - $consoleDB->setNamespace('_project_console'); + $dbForConsole = $dbPool->getDB('console', $cache); + $dbForConsole->setNamespace('_project_console'); $console = $app->getResource('console'); @@ -49,10 +44,10 @@ $cli $count = 0; try { - $totalProjects = $consoleDB->count('projects') + 1; + $totalProjects = $dbForConsole->count('projects') + 1; } catch (\Throwable $th) { - $consoleDB->setNamespace('_console'); - $totalProjects = $consoleDB->count('projects') + 1; + $dbForConsole->setNamespace('_console'); + $totalProjects = $dbForConsole->count('projects') + 1; } $class = 'Appwrite\\Migration\\Version\\' . Migration::$versions[$version]; @@ -61,8 +56,10 @@ $cli while (!empty($projects)) { foreach ($projects as $project) { try { + // TODO: Iterate through all project DBs + $projectDB = $dbPool->getDB($project->getId(), $cache); $migration - ->setProject($project, $projectDB, $consoleDB) + ->setProject($project, $projectDB, $dbForConsole) ->execute(); } catch (\Throwable $th) { throw $th; @@ -71,7 +68,7 @@ $cli } $sum = \count($projects); - $projects = $consoleDB->find('projects', limit: $limit, offset: $offset); + $projects = $dbForConsole->find('projects', limit: $limit, offset: $offset); $offset = $offset + $limit; $count = $count + $sum; diff --git a/app/tasks/usage.php b/app/tasks/usage.php index 7a22d7e79f..c1d20b4d8d 100644 --- a/app/tasks/usage.php +++ b/app/tasks/usage.php @@ -6,11 +6,7 @@ use Appwrite\Stats\Usage; use Appwrite\Stats\UsageDB; use InfluxDB\Database as InfluxDatabase; use Utopia\App; -use Utopia\Cache\Adapter\Redis as RedisCache; -use Utopia\Cache\Cache; use Utopia\CLI\Console; -use Utopia\Database\Adapter\MariaDB; -use Utopia\Database\Database; use Utopia\Database\Validator\Authorization; use Utopia\Registry\Registry; use Utopia\Logger\Log; @@ -18,38 +14,6 @@ use Utopia\Logger\Log; Authorization::disable(); Authorization::setDefaultStatus(false); -function getDatabase(Registry &$register, string $namespace): Database -{ - $attempts = 0; - - do { - try { - $attempts++; - - $db = $register->get('db'); - $redis = $register->get('cache'); - - $cache = new Cache(new RedisCache($redis)); - $database = new Database(new MariaDB($db), $cache); - $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); - $database->setNamespace($namespace); - - if (!$database->exists($database->getDefaultDatabase(), 'projects')) { - throw new Exception('Projects collection not ready'); - } - break; // leave loop if successful - } catch (\Exception$e) { - Console::warning("Database not ready. Retrying connection ({$attempts})..."); - if ($attempts >= DATABASE_RECONNECT_MAX_ATTEMPTS) { - throw new \Exception('Failed to connect to database: ' . $e->getMessage()); - } - sleep(DATABASE_RECONNECT_SLEEP); - } - } while ($attempts < DATABASE_RECONNECT_MAX_ATTEMPTS); - - return $database; -} - function getInfluxDB(Registry &$register): InfluxDatabase { /** @var InfluxDB\Client $client */ @@ -119,7 +83,8 @@ $cli $interval = (int) App::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '30'); // 30 seconds (by default) - $database = getDatabase($register, '_console'); + $redis = $register->get('cache'); + $database = $register->get('dbPool')->getDB('console', $redis); $influxDB = getInfluxDB($register); $usage = new Usage($database, $influxDB, $logError); diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index a60272f3bd..17eee00591 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -88,8 +88,6 @@ abstract class Migration { $this->project = $project; $this->projectDB = $projectDB; - $this->projectDB->setNamespace('_' . $this->project->getId()); - $this->consoleDB = $consoleDB; return $this;