1
0
Fork 0
mirror of synced 2024-09-30 01:08:13 +13:00

using platform to implement CLI

This commit is contained in:
Damodar Lohani 2022-07-13 06:26:22 +00:00
parent d476c8bbd8
commit 752117e07a
9 changed files with 196 additions and 147 deletions

View file

@ -3,13 +3,14 @@
require_once __DIR__ . '/init.php'; require_once __DIR__ . '/init.php';
require_once __DIR__ . '/controllers/general.php'; require_once __DIR__ . '/controllers/general.php';
use Appwrite\Task\Tasks; use Appwrite\Task\CLIPlatform;
use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Authorization;
Authorization::disable(); Authorization::disable();
$tasks = new Tasks(); $cliPlatform = new CLIPlatform();
$tasks $cliPlatform->init('CLI');
->init()
->run(); $cli = $cliPlatform->getCli();
$cli->run();

View file

@ -42,13 +42,14 @@
"ext-sockets": "*", "ext-sockets": "*",
"appwrite/php-clamav": "1.1.*", "appwrite/php-clamav": "1.1.*",
"appwrite/php-runtimes": "0.10.*", "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/logger": "0.3.*",
"utopia-php/abuse": "0.7.*", "utopia-php/abuse": "0.7.*",
"utopia-php/analytics": "0.2.*", "utopia-php/analytics": "0.2.*",
"utopia-php/audit": "0.8.*", "utopia-php/audit": "0.8.*",
"utopia-php/cache": "0.6.*", "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/config": "0.2.*",
"utopia-php/database": "0.18.*", "utopia-php/database": "0.18.*",
"utopia-php/locale": "0.4.*", "utopia-php/locale": "0.4.*",
@ -73,6 +74,10 @@
{ {
"url": "https://github.com/appwrite/runtimes.git", "url": "https://github.com/appwrite/runtimes.git",
"type": "git" "type": "git"
},
{
"url": "https://github.com/utopia-php/platform.git",
"type": "git"
} }
], ],
"require-dev": { "require-dev": {

94
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "4bd2cfb69f671c01e68b67221f8664fd", "content-hash": "3775608dcbc4262c4edd8710aa94ce4d",
"packages": [ "packages": [
{ {
"name": "adhocore/jwt", "name": "adhocore/jwt",
@ -1947,16 +1947,16 @@
}, },
{ {
"name": "utopia-php/cli", "name": "utopia-php/cli",
"version": "dev-feat-allow-adding-task", "version": "0.12.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/utopia-php/cli.git", "url": "https://github.com/utopia-php/cli.git",
"reference": "1ecc371b018f6e65d7d70b65f1c8b7a74c9ebcb2" "reference": "6d164b752efeb1ca089e3a517bc274d8b383474b"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/utopia-php/cli/zipball/1ecc371b018f6e65d7d70b65f1c8b7a74c9ebcb2", "url": "https://api.github.com/repos/utopia-php/cli/zipball/6d164b752efeb1ca089e3a517bc274d8b383474b",
"reference": "1ecc371b018f6e65d7d70b65f1c8b7a74c9ebcb2", "reference": "6d164b752efeb1ca089e3a517bc274d8b383474b",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -1994,9 +1994,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/utopia-php/cli/issues", "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", "name": "utopia-php/config",
@ -2169,16 +2169,16 @@
}, },
{ {
"name": "utopia-php/framework", "name": "utopia-php/framework",
"version": "0.19.21", "version": "dev-feat-public-add-route",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/utopia-php/framework.git", "url": "https://github.com/utopia-php/framework.git",
"reference": "3b7bd8e4acf84fd7d560ced8e0142221d302575d" "reference": "d16f3d44375beb1e880600957ac65e97699d6e23"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/utopia-php/framework/zipball/3b7bd8e4acf84fd7d560ced8e0142221d302575d", "url": "https://api.github.com/repos/utopia-php/framework/zipball/d16f3d44375beb1e880600957ac65e97699d6e23",
"reference": "3b7bd8e4acf84fd7d560ced8e0142221d302575d", "reference": "d16f3d44375beb1e880600957ac65e97699d6e23",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -2212,9 +2212,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/utopia-php/framework/issues", "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", "name": "utopia-php/image",
@ -2440,6 +2440,63 @@
}, },
"time": "2022-02-20T09:23:06+00:00" "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", "name": "utopia-php/preloader",
"version": "0.2.4", "version": "0.2.4",
@ -5348,15 +5405,16 @@
], ],
"aliases": [ "aliases": [
{ {
"package": "utopia-php/cli", "package": "utopia-php/framework",
"version": "dev-feat-allow-adding-task", "version": "dev-feat-public-add-route",
"alias": "0.12.0", "alias": "0.19.21",
"alias_normalized": "0.12.0.0" "alias_normalized": "0.19.21.0"
} }
], ],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": { "stability-flags": {
"utopia-php/cli": 20 "utopia-php/framework": 20,
"utopia-php/platform": 20
}, },
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,

View 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());
}
}

View file

@ -1,9 +0,0 @@
<?php
namespace Appwrite\Task;
use Utopia\CLI\Task as CLITask;
interface Task {
public static function getTask(): CLITask;
}

View file

@ -1,24 +1,14 @@
<?php <?php
namespace Appwrite\Task; namespace Appwrite\Task;
use Utopia\CLI\CLI; use Utopia\Platform\Service;
use Appwrite\Task\Usage;
use Appwrite\Task\Version;
class Tasks { class Tasks extends Service {
protected CLI $cli; public function __construct()
public function init(): Tasks
{ {
$this->cli = new CLI(); $this->type = self::TYPE_CLI;
$this->cli->addTask(Vars::getTask()); $this->addAction('version', new Version());
$this->cli->addTask(Usage::getTask()); $this->addAction('usage', new Usage());
$this->cli->addTask(Version::getTask()); $this->addAction('vars', new Vars());
return $this;
}
public function run(): CLI
{
return $this->cli->run();
} }
} }

View file

@ -3,7 +3,6 @@ namespace Appwrite\Task;
use Throwable; use Throwable;
use Exception; use Exception;
use Appwrite\Task\Task;
use Appwrite\Stats\Usage as InfluxUsage; use Appwrite\Stats\Usage as InfluxUsage;
use Appwrite\Stats\UsageDB; use Appwrite\Stats\UsageDB;
use InfluxDB\Database as InfluxDatabase; use InfluxDB\Database as InfluxDatabase;
@ -16,11 +15,10 @@ use Utopia\Database\Database;
use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Authorization;
use Utopia\Registry\Registry; use Utopia\Registry\Registry;
use Utopia\Logger\Log; use Utopia\Logger\Log;
use Utopia\CLI\Task as CLITask; use Utopia\Platform\Action;
class Usage implements Task{ class Usage extends Action{
private static CLITask $task; protected function getDatabase(Registry &$register, string $namespace): Database
protected static function getDatabase(Registry &$register, string $namespace): Database
{ {
$attempts = 0; $attempts = 0;
@ -52,7 +50,7 @@ class Usage implements Task{
return $database; return $database;
} }
protected static function getInfluxDB(Registry &$register): InfluxDatabase protected function getInfluxDB(Registry &$register): InfluxDatabase
{ {
/** @var InfluxDB\Client $client */ /** @var InfluxDB\Client $client */
$client = $register->get('influxdb'); $client = $register->get('influxdb');
@ -78,8 +76,17 @@ class Usage implements Task{
return $database; 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; global $register;
Authorization::disable(); Authorization::disable();
@ -119,60 +126,52 @@ class Usage implements Task{
Console::warning($error->getTraceAsString()); Console::warning($error->getTraceAsString());
}; };
$usage = new CLITask('usage'); Console::title('Usage Aggregation V1');
Console::success(APP_NAME . ' usage aggregation process v1 has started');
$usage $interval = (int) App::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '30'); // 30 seconds (by default)
->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);
$database = self::getDatabase($register, '_console'); $usage = new InfluxUsage($database, $influxDB, $logError);
$influxDB = self::getInfluxDB($register); $usageDB = new UsageDB($database, $logError);
$usage = new InfluxUsage($database, $influxDB, $logError); $iterations = 0;
$usageDB = new UsageDB($database, $logError); 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");
$iterations = 0; $loopStart = microtime(true);
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
* Aggregate InfluxDB every 30 seconds $iterations++;
*/ $loopTook = microtime(true) - $loopStart;
$usage->collect(); $now = date('d-m-Y H:i:s', time());
Console::info("[{$now}] Aggregation took {$loopTook} seconds");
return;
}
if ($iterations % 30 != 0) { // return if 30 iterations has not passed $iterations = 0; // Reset iterations to prevent overflow when running for long time
$iterations++; /**
$loopTook = microtime(true) - $loopStart; * Aggregate MariaDB every 15 minutes
$now = date('d-m-Y H:i:s', time()); * Some of the queries here might contain full-table scans.
Console::info("[{$now}] Aggregation took {$loopTook} seconds"); */
return; $now = date('d-m-Y H:i:s', time());
} Console::info("[{$now}] Aggregating database counters.");
$iterations = 0; // Reset iterations to prevent overflow when running for long time $usageDB->collect();
/**
* 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());
$iterations++; Console::info("[{$now}] Aggregation took {$loopTook} seconds");
$loopTook = microtime(true) - $loopStart; }, $interval);
$now = date('d-m-Y H:i:s', time());
Console::info("[{$now}] Aggregation took {$loopTook} seconds");
}, $interval);
});
self::$task = $usage;
return self::$task;
} }
} }

View file

@ -1,34 +1,32 @@
<?php <?php
namespace Appwrite\Task; namespace Appwrite\Task;
use Utopia\App; use Utopia\App;
use Utopia\CLI\Task as CLITask;
use Utopia\Config\Config; use Utopia\Config\Config;
use Utopia\CLI\Console; use Utopia\CLI\Console;
use Utopia\Platform\Action;
class Vars extends Action{
class Vars implements Task{ public function __construct()
private static CLITask $task;
public static function getTask(): CLITask
{ {
$vars = new CLITask('vars'); $this
$vars
->desc('List all the server environment variables') ->desc('List all the server environment variables')
->action(function () { ->callback(fn () => $this->action());
$config = Config::getParam('variables', []); }
$vars = [];
foreach ($config as $category) { public function action(): void
foreach ($category['variables'] ?? [] as $var) { {
$vars[] = $var; $config = Config::getParam('variables', []);
} $vars = [];
}
foreach ($vars as $key => $value) { foreach ($config as $category) {
Console::log('- ' . $value['name'] . '=' . App::getEnv($value['name'], '')); foreach ($category['variables'] ?? [] as $var) {
} $vars[] = $var;
}); }
self::$task = $vars; }
return self::$task;
foreach ($vars as $key => $value) {
Console::log('- ' . $value['name'] . '=' . App::getEnv($value['name'], ''));
}
} }
} }

View file

@ -2,21 +2,16 @@
namespace Appwrite\Task; namespace Appwrite\Task;
use Utopia\App; use Utopia\App;
use Appwrite\Task\Task;
use Utopia\CLI\Task as CLITask;
use Utopia\CLI\Console; use Utopia\CLI\Console;
use Utopia\Platform\Action;
class Version implements Task { class Version extends Action {
protected static CLITask $task; public function __construct()
{
public static function getTask(): CLITask { $this
$version = new CLITask('version');
$version
->desc('Get the server version') ->desc('Get the server version')
->action(function () { ->callback(function () {
Console::log(App::getEnv('_APP_VERSION', 'UNKNOWN')); Console::log(App::getEnv('_APP_VERSION', 'UNKNOWN'));
}); });
self::$task = $version;
return self::$task;
} }
} }