mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
using platform to implement CLI
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Task;
|
||||
|
||||
use Utopia\Platform\Platform;
|
||||
|
||||
class CLIPlatform extends Platform {
|
||||
public function __construct()
|
||||
{
|
||||
$this->addService('cliTasks', new Tasks());
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Task;
|
||||
|
||||
use Utopia\CLI\Task as CLITask;
|
||||
|
||||
interface Task {
|
||||
public static function getTask(): CLITask;
|
||||
}
|
||||
@@ -1,24 +1,14 @@
|
||||
<?php
|
||||
namespace Appwrite\Task;
|
||||
|
||||
use Utopia\CLI\CLI;
|
||||
use Appwrite\Task\Usage;
|
||||
use Appwrite\Task\Version;
|
||||
use Utopia\Platform\Service;
|
||||
|
||||
class Tasks {
|
||||
protected CLI $cli;
|
||||
|
||||
public function init(): Tasks
|
||||
class Tasks extends Service {
|
||||
public function __construct()
|
||||
{
|
||||
$this->cli = new CLI();
|
||||
$this->cli->addTask(Vars::getTask());
|
||||
$this->cli->addTask(Usage::getTask());
|
||||
$this->cli->addTask(Version::getTask());
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function run(): CLI
|
||||
{
|
||||
return $this->cli->run();
|
||||
$this->type = self::TYPE_CLI;
|
||||
$this->addAction('version', new Version());
|
||||
$this->addAction('usage', new Usage());
|
||||
$this->addAction('vars', new Vars());
|
||||
}
|
||||
}
|
||||
|
||||
+61
-62
@@ -3,7 +3,6 @@ namespace Appwrite\Task;
|
||||
|
||||
use Throwable;
|
||||
use Exception;
|
||||
use Appwrite\Task\Task;
|
||||
use Appwrite\Stats\Usage as InfluxUsage;
|
||||
use Appwrite\Stats\UsageDB;
|
||||
use InfluxDB\Database as InfluxDatabase;
|
||||
@@ -16,11 +15,10 @@ use Utopia\Database\Database;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Registry\Registry;
|
||||
use Utopia\Logger\Log;
|
||||
use Utopia\CLI\Task as CLITask;
|
||||
use Utopia\Platform\Action;
|
||||
|
||||
class Usage implements Task{
|
||||
private static CLITask $task;
|
||||
protected static function getDatabase(Registry &$register, string $namespace): Database
|
||||
class Usage extends Action{
|
||||
protected function getDatabase(Registry &$register, string $namespace): Database
|
||||
{
|
||||
$attempts = 0;
|
||||
|
||||
@@ -52,7 +50,7 @@ class Usage implements Task{
|
||||
return $database;
|
||||
}
|
||||
|
||||
protected static function getInfluxDB(Registry &$register): InfluxDatabase
|
||||
protected function getInfluxDB(Registry &$register): InfluxDatabase
|
||||
{
|
||||
/** @var InfluxDB\Client $client */
|
||||
$client = $register->get('influxdb');
|
||||
@@ -78,8 +76,17 @@ class Usage implements Task{
|
||||
return $database;
|
||||
}
|
||||
|
||||
public static function getTask(): CLITask
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
|
||||
$this
|
||||
->desc('Schedules syncing data from influxdb to Appwrite console db')
|
||||
->callback(fn () => $this->action());
|
||||
}
|
||||
|
||||
public function action() {
|
||||
|
||||
global $register;
|
||||
|
||||
Authorization::disable();
|
||||
@@ -119,60 +126,52 @@ class Usage implements Task{
|
||||
Console::warning($error->getTraceAsString());
|
||||
};
|
||||
|
||||
$usage = new CLITask('usage');
|
||||
|
||||
$usage
|
||||
->desc('Schedules syncing data from influxdb to Appwrite console db')
|
||||
->action(function () use ($register, $logError) {
|
||||
Console::title('Usage Aggregation V1');
|
||||
Console::success(APP_NAME . ' usage aggregation process v1 has started');
|
||||
|
||||
$interval = (int) App::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '30'); // 30 seconds (by default)
|
||||
|
||||
$database = self::getDatabase($register, '_console');
|
||||
$influxDB = self::getInfluxDB($register);
|
||||
|
||||
$usage = new InfluxUsage($database, $influxDB, $logError);
|
||||
$usageDB = new UsageDB($database, $logError);
|
||||
|
||||
$iterations = 0;
|
||||
Console::loop(function () use ($interval, $usage, $usageDB, &$iterations) {
|
||||
$now = date('d-m-Y H:i:s', time());
|
||||
Console::info("[{$now}] Aggregating usage data every {$interval} seconds");
|
||||
|
||||
$loopStart = microtime(true);
|
||||
|
||||
/**
|
||||
* Aggregate InfluxDB every 30 seconds
|
||||
*/
|
||||
$usage->collect();
|
||||
|
||||
if ($iterations % 30 != 0) { // return if 30 iterations has not passed
|
||||
$iterations++;
|
||||
$loopTook = microtime(true) - $loopStart;
|
||||
$now = date('d-m-Y H:i:s', time());
|
||||
Console::info("[{$now}] Aggregation took {$loopTook} seconds");
|
||||
return;
|
||||
}
|
||||
|
||||
$iterations = 0; // Reset iterations to prevent overflow when running for long time
|
||||
/**
|
||||
* Aggregate MariaDB every 15 minutes
|
||||
* Some of the queries here might contain full-table scans.
|
||||
*/
|
||||
$now = date('d-m-Y H:i:s', time());
|
||||
Console::info("[{$now}] Aggregating database counters.");
|
||||
|
||||
$usageDB->collect();
|
||||
|
||||
$iterations++;
|
||||
$loopTook = microtime(true) - $loopStart;
|
||||
$now = date('d-m-Y H:i:s', time());
|
||||
|
||||
Console::info("[{$now}] Aggregation took {$loopTook} seconds");
|
||||
}, $interval);
|
||||
});
|
||||
self::$task = $usage;
|
||||
return self::$task;
|
||||
Console::title('Usage Aggregation V1');
|
||||
Console::success(APP_NAME . ' usage aggregation process v1 has started');
|
||||
|
||||
$interval = (int) App::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '30'); // 30 seconds (by default)
|
||||
|
||||
$database = self::getDatabase($register, '_console');
|
||||
$influxDB = self::getInfluxDB($register);
|
||||
|
||||
$usage = new InfluxUsage($database, $influxDB, $logError);
|
||||
$usageDB = new UsageDB($database, $logError);
|
||||
|
||||
$iterations = 0;
|
||||
Console::loop(function () use ($interval, $usage, $usageDB, &$iterations) {
|
||||
$now = date('d-m-Y H:i:s', time());
|
||||
Console::info("[{$now}] Aggregating usage data every {$interval} seconds");
|
||||
|
||||
$loopStart = microtime(true);
|
||||
|
||||
/**
|
||||
* Aggregate InfluxDB every 30 seconds
|
||||
*/
|
||||
$usage->collect();
|
||||
|
||||
if ($iterations % 30 != 0) { // return if 30 iterations has not passed
|
||||
$iterations++;
|
||||
$loopTook = microtime(true) - $loopStart;
|
||||
$now = date('d-m-Y H:i:s', time());
|
||||
Console::info("[{$now}] Aggregation took {$loopTook} seconds");
|
||||
return;
|
||||
}
|
||||
|
||||
$iterations = 0; // Reset iterations to prevent overflow when running for long time
|
||||
/**
|
||||
* Aggregate MariaDB every 15 minutes
|
||||
* Some of the queries here might contain full-table scans.
|
||||
*/
|
||||
$now = date('d-m-Y H:i:s', time());
|
||||
Console::info("[{$now}] Aggregating database counters.");
|
||||
|
||||
$usageDB->collect();
|
||||
|
||||
$iterations++;
|
||||
$loopTook = microtime(true) - $loopStart;
|
||||
$now = date('d-m-Y H:i:s', time());
|
||||
|
||||
Console::info("[{$now}] Aggregation took {$loopTook} seconds");
|
||||
}, $interval);
|
||||
}
|
||||
}
|
||||
+21
-23
@@ -1,34 +1,32 @@
|
||||
<?php
|
||||
namespace Appwrite\Task;
|
||||
use Utopia\App;
|
||||
use Utopia\CLI\Task as CLITask;
|
||||
use Utopia\Config\Config;
|
||||
use Utopia\CLI\Console;
|
||||
use Utopia\Platform\Action;
|
||||
|
||||
|
||||
class Vars implements Task{
|
||||
private static CLITask $task;
|
||||
class Vars extends Action{
|
||||
|
||||
public static function getTask(): CLITask
|
||||
public function __construct()
|
||||
{
|
||||
$vars = new CLITask('vars');
|
||||
$vars
|
||||
$this
|
||||
->desc('List all the server environment variables')
|
||||
->action(function () {
|
||||
$config = Config::getParam('variables', []);
|
||||
$vars = [];
|
||||
|
||||
foreach ($config as $category) {
|
||||
foreach ($category['variables'] ?? [] as $var) {
|
||||
$vars[] = $var;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($vars as $key => $value) {
|
||||
Console::log('- ' . $value['name'] . '=' . App::getEnv($value['name'], ''));
|
||||
}
|
||||
});
|
||||
self::$task = $vars;
|
||||
return self::$task;
|
||||
->callback(fn () => $this->action());
|
||||
}
|
||||
|
||||
public function action(): void
|
||||
{
|
||||
$config = Config::getParam('variables', []);
|
||||
$vars = [];
|
||||
|
||||
foreach ($config as $category) {
|
||||
foreach ($category['variables'] ?? [] as $var) {
|
||||
$vars[] = $var;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($vars as $key => $value) {
|
||||
Console::log('- ' . $value['name'] . '=' . App::getEnv($value['name'], ''));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,21 +2,16 @@
|
||||
namespace Appwrite\Task;
|
||||
|
||||
use Utopia\App;
|
||||
use Appwrite\Task\Task;
|
||||
use Utopia\CLI\Task as CLITask;
|
||||
use Utopia\CLI\Console;
|
||||
use Utopia\Platform\Action;
|
||||
|
||||
class Version implements Task {
|
||||
protected static CLITask $task;
|
||||
|
||||
public static function getTask(): CLITask {
|
||||
$version = new CLITask('version');
|
||||
$version
|
||||
class Version extends Action {
|
||||
public function __construct()
|
||||
{
|
||||
$this
|
||||
->desc('Get the server version')
|
||||
->action(function () {
|
||||
->callback(function () {
|
||||
Console::log(App::getEnv('_APP_VERSION', 'UNKNOWN'));
|
||||
});
|
||||
self::$task = $version;
|
||||
return self::$task;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user