1
0
Fork 0
mirror of synced 2024-10-02 10:16:27 +13:00

Fixing CLI tasks

This commit is contained in:
Eldad Fux 2022-10-17 19:43:51 +03:00
parent 892f74c2a8
commit 1632ae61eb
5 changed files with 78 additions and 99 deletions

View file

@ -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();

View file

@ -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);
}

View file

@ -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');

View file

@ -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':

View file

@ -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 = [];