using platform to implement CLI
This commit is contained in:
parent
d476c8bbd8
commit
752117e07a
9 changed files with 196 additions and 147 deletions
11
app/cli.php
11
app/cli.php
|
@ -3,13 +3,14 @@
|
|||
require_once __DIR__ . '/init.php';
|
||||
require_once __DIR__ . '/controllers/general.php';
|
||||
|
||||
use Appwrite\Task\Tasks;
|
||||
use Appwrite\Task\CLIPlatform;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
|
||||
Authorization::disable();
|
||||
|
||||
$tasks = new Tasks();
|
||||
$tasks
|
||||
->init()
|
||||
->run();
|
||||
$cliPlatform = new CLIPlatform();
|
||||
$cliPlatform->init('CLI');
|
||||
|
||||
$cli = $cliPlatform->getCli();
|
||||
$cli->run();
|
||||
|
||||
|
|
|
@ -42,13 +42,14 @@
|
|||
"ext-sockets": "*",
|
||||
"appwrite/php-clamav": "1.1.*",
|
||||
"appwrite/php-runtimes": "0.10.*",
|
||||
"utopia-php/framework": "0.19.*",
|
||||
"utopia-php/framework": "dev-feat-public-add-route as 0.19.21",
|
||||
"utopia-php/logger": "0.3.*",
|
||||
"utopia-php/abuse": "0.7.*",
|
||||
"utopia-php/analytics": "0.2.*",
|
||||
"utopia-php/audit": "0.8.*",
|
||||
"utopia-php/cache": "0.6.*",
|
||||
"utopia-php/cli": "dev-feat-allow-adding-task as 0.12.0",
|
||||
"utopia-php/cli": "0.12.0",
|
||||
"utopia-php/platform": "dev-dev",
|
||||
"utopia-php/config": "0.2.*",
|
||||
"utopia-php/database": "0.18.*",
|
||||
"utopia-php/locale": "0.4.*",
|
||||
|
@ -73,6 +74,10 @@
|
|||
{
|
||||
"url": "https://github.com/appwrite/runtimes.git",
|
||||
"type": "git"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/utopia-php/platform.git",
|
||||
"type": "git"
|
||||
}
|
||||
],
|
||||
"require-dev": {
|
||||
|
|
94
composer.lock
generated
94
composer.lock
generated
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "4bd2cfb69f671c01e68b67221f8664fd",
|
||||
"content-hash": "3775608dcbc4262c4edd8710aa94ce4d",
|
||||
"packages": [
|
||||
{
|
||||
"name": "adhocore/jwt",
|
||||
|
@ -1947,16 +1947,16 @@
|
|||
},
|
||||
{
|
||||
"name": "utopia-php/cli",
|
||||
"version": "dev-feat-allow-adding-task",
|
||||
"version": "0.12.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/utopia-php/cli.git",
|
||||
"reference": "1ecc371b018f6e65d7d70b65f1c8b7a74c9ebcb2"
|
||||
"reference": "6d164b752efeb1ca089e3a517bc274d8b383474b"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/utopia-php/cli/zipball/1ecc371b018f6e65d7d70b65f1c8b7a74c9ebcb2",
|
||||
"reference": "1ecc371b018f6e65d7d70b65f1c8b7a74c9ebcb2",
|
||||
"url": "https://api.github.com/repos/utopia-php/cli/zipball/6d164b752efeb1ca089e3a517bc274d8b383474b",
|
||||
"reference": "6d164b752efeb1ca089e3a517bc274d8b383474b",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -1994,9 +1994,9 @@
|
|||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/utopia-php/cli/issues",
|
||||
"source": "https://github.com/utopia-php/cli/tree/feat-allow-adding-task"
|
||||
"source": "https://github.com/utopia-php/cli/tree/0.12.0"
|
||||
},
|
||||
"time": "2022-07-08T01:44:31+00:00"
|
||||
"time": "2022-02-18T22:10:41+00:00"
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/config",
|
||||
|
@ -2169,16 +2169,16 @@
|
|||
},
|
||||
{
|
||||
"name": "utopia-php/framework",
|
||||
"version": "0.19.21",
|
||||
"version": "dev-feat-public-add-route",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/utopia-php/framework.git",
|
||||
"reference": "3b7bd8e4acf84fd7d560ced8e0142221d302575d"
|
||||
"reference": "d16f3d44375beb1e880600957ac65e97699d6e23"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/utopia-php/framework/zipball/3b7bd8e4acf84fd7d560ced8e0142221d302575d",
|
||||
"reference": "3b7bd8e4acf84fd7d560ced8e0142221d302575d",
|
||||
"url": "https://api.github.com/repos/utopia-php/framework/zipball/d16f3d44375beb1e880600957ac65e97699d6e23",
|
||||
"reference": "d16f3d44375beb1e880600957ac65e97699d6e23",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -2212,9 +2212,9 @@
|
|||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/utopia-php/framework/issues",
|
||||
"source": "https://github.com/utopia-php/framework/tree/0.19.21"
|
||||
"source": "https://github.com/utopia-php/framework/tree/feat-public-add-route"
|
||||
},
|
||||
"time": "2022-05-12T18:42:28+00:00"
|
||||
"time": "2022-07-10T08:39:49+00:00"
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/image",
|
||||
|
@ -2440,6 +2440,63 @@
|
|||
},
|
||||
"time": "2022-02-20T09:23:06+00:00"
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/platform",
|
||||
"version": "dev-dev",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/utopia-php/platform.git",
|
||||
"reference": "370565fcc39996506357338e28cad97cb9e3db0f"
|
||||
},
|
||||
"require": {
|
||||
"ext-json": "*",
|
||||
"ext-redis": "*",
|
||||
"php": ">=8.0",
|
||||
"utopia-php/cli": "0.12.*",
|
||||
"utopia-php/framework": "dev-feat-public-add-route as 0.19.21"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.3",
|
||||
"squizlabs/php_codesniffer": "^3.6"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Utopia\\Platform\\": "src/Platform"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Utopia\\Tests\\": "tests/Platform"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"format": [
|
||||
"vendor/bin/phpcbf"
|
||||
],
|
||||
"test": [
|
||||
"docker-compose up -d && sleep 10 && docker-compose exec web vendor/bin/phpunit --configuration phpunit.xml"
|
||||
]
|
||||
},
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Eldad Fux",
|
||||
"email": "eldad@appwrite.io"
|
||||
}
|
||||
],
|
||||
"description": "Light and Fast Platform Library",
|
||||
"keywords": [
|
||||
"cache",
|
||||
"framework",
|
||||
"php",
|
||||
"upf",
|
||||
"utopia"
|
||||
],
|
||||
"time": "2022-07-13T05:59:26+00:00"
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/preloader",
|
||||
"version": "0.2.4",
|
||||
|
@ -5348,15 +5405,16 @@
|
|||
],
|
||||
"aliases": [
|
||||
{
|
||||
"package": "utopia-php/cli",
|
||||
"version": "dev-feat-allow-adding-task",
|
||||
"alias": "0.12.0",
|
||||
"alias_normalized": "0.12.0.0"
|
||||
"package": "utopia-php/framework",
|
||||
"version": "dev-feat-public-add-route",
|
||||
"alias": "0.19.21",
|
||||
"alias_normalized": "0.19.21.0"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": {
|
||||
"utopia-php/cli": 20
|
||||
"utopia-php/framework": 20,
|
||||
"utopia-php/platform": 20
|
||||
},
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
|
|
12
src/Appwrite/Task/CLIPlatform.php
Normal file
12
src/Appwrite/Task/CLIPlatform.php
Normal file
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue