diff --git a/app/cli.php b/app/cli.php index 6447de592b..618ba56d1c 100644 --- a/app/cli.php +++ b/app/cli.php @@ -6,7 +6,79 @@ require_once __DIR__ . '/controllers/general.php'; use Utopia\App; use Utopia\CLI\CLI; use Utopia\CLI\Console; +use Utopia\Cache\Adapter\Sharding; +use Utopia\Cache\Cache; +use Utopia\Config\Config; +use Utopia\Database\Database; use Utopia\Database\Validator\Authorization; +use InfluxDB\Database as InfluxDatabase; + +function getInfluxDB(): InfluxDatabase +{ + global $register; + + $client = $register->get('influxdb'); /** @var InfluxDB\Client $client */ + $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); + return $database; +} + +function getConsoleDB(): Database +{ + global $register; + + $pools = $register->get('pools'); /** @var \Utopia\Pools\Group $pools */ + + $dbAdapter = $pools + ->get('console') + ->pop() + ->getResource() + ; + + $database = new Database($dbAdapter, getCache()); + + $database->setNamespace('console'); + $database->setDefaultDatabase('appwrite'); + + return $database; +} + +function getCache(): Cache +{ + global $register; + + $pools = $register->get('pools'); /** @var \Utopia\Pools\Group $pools */ + + $list = Config::getParam('pools-cache', []); + $adapters = []; + + foreach ($list as $value) { + $adapters[] = $pools + ->get($value) + ->pop() + ->getResource() + ; + } + + return new Cache(new Sharding($adapters)); +} Authorization::disable(); diff --git a/app/init.php b/app/init.php index 9c813d8b98..e731859b54 100644 --- a/app/init.php +++ b/app/init.php @@ -606,7 +606,7 @@ $register->set('pools', function () { case 'redis': $resource = function() use ($dsnHost, $dsnPort, $dsnPass) { $redis = new Redis(); - @$redis->pconnect($dsnHost, $dsnPort); + @$redis->pconnect($dsnHost, (int)$dsnPort); if($dsnPass) { $redis->auth($dsnPass); } diff --git a/app/tasks/maintenance.php b/app/tasks/maintenance.php index d7a4de77a6..1c16ca6911 100644 --- a/app/tasks/maintenance.php +++ b/app/tasks/maintenance.php @@ -3,15 +3,10 @@ global $cli; use Appwrite\Auth\Auth; -use Appwrite\Database\Pools; use Appwrite\Event\Certificate; use Appwrite\Event\Delete; use Utopia\App; -use Utopia\Cache\Adapter\Sharding; -use Utopia\Cache\Cache; use Utopia\CLI\Console; -use Utopia\Config\Config; -use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\DateTime; use Utopia\Database\Query; @@ -20,8 +15,6 @@ $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'); @@ -110,54 +103,6 @@ $cli ->trigger(); } - /** - * Get console database - * @return Database - */ - function getConsoleDB(): Database - { - global $register; - - $pools = $register->get('pools'); /* @var \Utopia\Pools\Group $pools */ - - $dbAdapter = $pools - ->get('console') - ->pop() - ->getResource() - ; - - $database = new Database($dbAdapter, getCache()); - - $database->setNamespace('console'); - $database->setDefaultDatabase('appwrite'); - - return $database; - } - - /** - * Get Cache - * @return Cache - */ - function getCache(): Cache - { - global $register; - - $pools = $register->get('pools'); /* @var \Utopia\Pools\Group $pools */ - - $list = Config::getParam('pools-cache', []); - $adapters = []; - - foreach ($list as $value) { - $adapters[] = $pools - ->get($value) - ->pop() - ->getResource() - ; - } - - return new Cache(new Sharding($adapters)); - } - // # of days in seconds (1 day = 86400s) $interval = (int) App::getEnv('_APP_MAINTENANCE_INTERVAL', '86400'); $executionLogsRetention = (int) App::getEnv('_APP_MAINTENANCE_RETENTION_EXECUTION', '1209600'); diff --git a/app/tasks/usage.php b/app/tasks/usage.php index fc171aa524..d1aeab2e84 100644 --- a/app/tasks/usage.php +++ b/app/tasks/usage.php @@ -2,7 +2,6 @@ global $cli, $register; -use Appwrite\Database\Pools; use Appwrite\Usage\Calculators\Aggregator; use Appwrite\Usage\Calculators\Database; use Appwrite\Usage\Calculators\TimeSeries; @@ -11,39 +10,12 @@ use Utopia\App; use Utopia\CLI\Console; use Utopia\Database\Database as UtopiaDatabase; use Utopia\Database\Validator\Authorization; -use Utopia\Registry\Registry; use Utopia\Logger\Log; use Utopia\Validator\WhiteList; Authorization::disable(); Authorization::setDefaultStatus(false); -function getInfluxDB(Registry &$register): InfluxDatabase -{ - /** @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); - return $database; -} - $logError = function (Throwable $error, string $action = 'syncUsageStats') use ($register) { $logger = $register->get('logger'); @@ -78,7 +50,6 @@ $logError = function (Throwable $error, string $action = 'syncUsageStats') use ( Console::warning($error->getTraceAsString()); }; - function aggregateTimeseries(UtopiaDatabase $database, InfluxDatabase $influxDB, callable $logError): void { $interval = (int) App::getEnv('_APP_USAGE_TIMESERIES_INTERVAL', '30'); // 30 seconds (by default) @@ -120,21 +91,12 @@ $cli ->task('usage') ->param('type', 'timeseries', new WhiteList(['timeseries', 'database'])) ->desc('Schedules syncing data from influxdb to Appwrite console db') - ->action(function (string $type) use ($register, $logError) { + ->action(function (string $type) use ($logError) { Console::title('Usage Aggregation V1'); Console::success(APP_NAME . ' usage aggregation process v1 has started'); - $redis = $register->get('cache'); - $dbPool = $register->get('dbPool'); - - $database = $dbPool->getConsoleDB(); - $pdo = $dbPool->getPDO($database); - $database = Pools::wait( - Pools::getDatabase($pdo, $redis, '_console'), - 'projects', - ); - - $influxDB = getInfluxDB($register); + $database = getConsoleDB(); + $influxDB = getInfluxDB(); switch ($type) { case 'timeseries': diff --git a/src/Appwrite/Resque/Worker.php b/src/Appwrite/Resque/Worker.php index 28e7106b7e..e504cae679 100644 --- a/src/Appwrite/Resque/Worker.php +++ b/src/Appwrite/Resque/Worker.php @@ -201,7 +201,7 @@ abstract class Worker { global $register; - $pools = $register->get('pools'); /* @var \Utopia\Pools\Group $pools */ + $pools = $register->get('pools'); /** @var \Utopia\Pools\Group $pools */ $dbAdapter = $pools ->get('console') @@ -225,7 +225,7 @@ abstract class Worker { global $register; - $pools = $register->get('pools'); /* @var \Utopia\Pools\Group $pools */ + $pools = $register->get('pools'); /** @var \Utopia\Pools\Group $pools */ $list = Config::getParam('pools-cache', []); $adapters = [];