From 7806cf1b55a9b799ae4aaae459b47560ac216886 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Tue, 17 Oct 2023 23:29:25 +0400 Subject: [PATCH] chore: cleanup obsolete CLI tasks --- composer.lock | 2 +- src/Appwrite/Platform/Services/Tasks.php | 10 - .../Platform/Tasks/CalcUsersStats.php | 176 ------------------ .../Platform/Tasks/ClearCardCache.php | 62 ------ .../Tasks/PatchCreateMissingSchedules.php | 97 ---------- .../Tasks/PatchDeleteProjectCollections.php | 129 ------------- .../PatchDeleteScheduleUpdatedAtAttribute.php | 74 -------- src/Appwrite/Platform/Workers/Messaging.php | 3 + 8 files changed, 4 insertions(+), 549 deletions(-) delete mode 100644 src/Appwrite/Platform/Tasks/CalcUsersStats.php delete mode 100644 src/Appwrite/Platform/Tasks/ClearCardCache.php delete mode 100644 src/Appwrite/Platform/Tasks/PatchCreateMissingSchedules.php delete mode 100644 src/Appwrite/Platform/Tasks/PatchDeleteProjectCollections.php delete mode 100644 src/Appwrite/Platform/Tasks/PatchDeleteScheduleUpdatedAtAttribute.php diff --git a/composer.lock b/composer.lock index 7ccc6df4df..4634780720 100644 --- a/composer.lock +++ b/composer.lock @@ -6019,5 +6019,5 @@ "platform-overrides": { "php": "8.0" }, - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.6.0" } diff --git a/src/Appwrite/Platform/Services/Tasks.php b/src/Appwrite/Platform/Services/Tasks.php index bc8d1bbc72..00779084d4 100644 --- a/src/Appwrite/Platform/Services/Tasks.php +++ b/src/Appwrite/Platform/Services/Tasks.php @@ -8,20 +8,15 @@ use Appwrite\Platform\Tasks\Install; use Appwrite\Platform\Tasks\Maintenance; use Appwrite\Platform\Tasks\Migrate; use Appwrite\Platform\Tasks\Schedule; -use Appwrite\Platform\Tasks\PatchCreateMissingSchedules; use Appwrite\Platform\Tasks\SDKs; use Appwrite\Platform\Tasks\Specs; use Appwrite\Platform\Tasks\SSL; use Appwrite\Platform\Tasks\Hamster; -use Appwrite\Platform\Tasks\PatchDeleteScheduleUpdatedAtAttribute; -use Appwrite\Platform\Tasks\ClearCardCache; use Appwrite\Platform\Tasks\Usage; use Appwrite\Platform\Tasks\Vars; use Appwrite\Platform\Tasks\Version; use Appwrite\Platform\Tasks\VolumeSync; -use Appwrite\Platform\Tasks\CalcUsersStats; use Appwrite\Platform\Tasks\CalcTierStats; -use Appwrite\Platform\Tasks\PatchDeleteProjectCollections; use Appwrite\Platform\Tasks\Upgrade; class Tasks extends Service @@ -39,17 +34,12 @@ class Tasks extends Service ->addAction(Install::getName(), new Install()) ->addAction(Upgrade::getName(), new Upgrade()) ->addAction(Maintenance::getName(), new Maintenance()) - ->addAction(PatchCreateMissingSchedules::getName(), new PatchCreateMissingSchedules()) - ->addAction(ClearCardCache::getName(), new ClearCardCache()) - ->addAction(PatchDeleteScheduleUpdatedAtAttribute::getName(), new PatchDeleteScheduleUpdatedAtAttribute()) ->addAction(Schedule::getName(), new Schedule()) ->addAction(Migrate::getName(), new Migrate()) ->addAction(SDKs::getName(), new SDKs()) ->addAction(VolumeSync::getName(), new VolumeSync()) ->addAction(Specs::getName(), new Specs()) - ->addAction(CalcUsersStats::getName(), new CalcUsersStats()) ->addAction(CalcTierStats::getName(), new CalcTierStats()) - ->addAction(PatchDeleteProjectCollections::getName(), new PatchDeleteProjectCollections()) ; } } diff --git a/src/Appwrite/Platform/Tasks/CalcUsersStats.php b/src/Appwrite/Platform/Tasks/CalcUsersStats.php deleted file mode 100644 index 6310fe17b4..0000000000 --- a/src/Appwrite/Platform/Tasks/CalcUsersStats.php +++ /dev/null @@ -1,176 +0,0 @@ -desc('Get stats for projects') - ->inject('pools') - ->inject('cache') - ->inject('dbForConsole') - ->inject('register') - ->callback(function (Group $pools, Cache $cache, Database $dbForConsole, Registry $register) { - $this->action($pools, $cache, $dbForConsole, $register); - }); - } - - public function action(Group $pools, Cache $cache, Database $dbForConsole, Registry $register): void - { - //docker compose exec -t appwrite calc-users-stats - - Console::title('Cloud Users calculation V1'); - Console::success(APP_NAME . ' cloud Users calculation has started'); - - /* Initialise new Utopia app */ - $app = new App('UTC'); - $console = $app->getResource('console'); - - /** CSV stuff */ - $this->date = date('Y-m-d'); - $this->path = "{$this->directory}/users_stats_{$this->date}.csv"; - $csv = Writer::createFromPath($this->path, 'w'); - $csv->insertOne($this->columns); - - /** Database connections */ - $totalProjects = $dbForConsole->count('projects'); - Console::success("Found a total of: {$totalProjects} projects"); - - $projects = [$console]; - $count = 0; - $limit = 30; - $sum = 30; - $offset = 0; - while (!empty($projects)) { - foreach ($projects as $project) { - - /** - * Skip user projects with id 'console' - */ - if ($project->getId() === 'console') { - continue; - } - - Console::info("Getting stats for {$project->getId()}"); - - try { - $db = $project->getAttribute('database'); - $adapter = $pools - ->get($db) - ->pop() - ->getResource(); - - $dbForProject = new Database($adapter, $cache); - $dbForProject->setDefaultDatabase('appwrite'); - $dbForProject->setNamespace('_' . $project->getInternalId()); - - /** Get Project ID */ - $stats['Project ID'] = $project->getId(); - - /** Get Project Name */ - $stats['Project Name'] = $project->getAttribute('name'); - - - /** Get Team Name and Id */ - $teamId = $project->getAttribute('teamId', null); - $teamName = null; - if ($teamId) { - $team = $dbForConsole->getDocument('teams', $teamId); - $teamName = $team->getAttribute('name'); - } - - $stats['Team ID'] = $teamId; - $stats['Team name'] = $teamName; - - /** Get Total Users */ - $stats['users'] = $dbForProject->count('users', []); - - $csv->insertOne(array_values($stats)); - } catch (\Throwable $th) { - Console::error('Failed to update project ("' . $project->getId() . '") version with error: ' . $th->getMessage()); - } finally { - $pools - ->get($db) - ->reclaim(); - } - } - - $sum = \count($projects); - - $projects = $dbForConsole->find('projects', [ - Query::limit($limit), - Query::offset($offset), - ]); - - $offset = $offset + $limit; - $count = $count + $sum; - } - Console::log('Iterated through ' . $count - 1 . '/' . $totalProjects . ' projects...'); - $pools - ->get('console') - ->reclaim(); - - /** @var PHPMailer $mail */ - $mail = $register->get('smtp'); - - $mail->clearAddresses(); - $mail->clearAllRecipients(); - $mail->clearReplyTos(); - $mail->clearAttachments(); - $mail->clearBCCs(); - $mail->clearCCs(); - - try { - /** Addresses */ - $mail->setFrom(App::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM), 'Appwrite Cloud Hamster'); - $recipients = explode(',', App::getEnv('_APP_USERS_STATS_RECIPIENTS', '')); - - foreach ($recipients as $recipient) { - $mail->addAddress($recipient); - } - - /** Attachments */ - $mail->addAttachment($this->path); - - /** Content */ - $mail->Subject = "Cloud Report for {$this->date}"; - $mail->Body = "Please find the daily cloud report atttached"; - $mail->send(); - Console::success('Email has been sent!'); - } catch (Exception $e) { - Console::error("Message could not be sent. Mailer Error: {$mail->ErrorInfo}"); - } - } -} diff --git a/src/Appwrite/Platform/Tasks/ClearCardCache.php b/src/Appwrite/Platform/Tasks/ClearCardCache.php deleted file mode 100644 index d3153b995c..0000000000 --- a/src/Appwrite/Platform/Tasks/ClearCardCache.php +++ /dev/null @@ -1,62 +0,0 @@ -desc('Deletes card cache for specific user') - ->param('userId', '', new UID(), 'User UID.', false) - ->inject('dbForConsole') - ->callback(fn (string $userId, Database $dbForConsole) => $this->action($userId, $dbForConsole)); - } - - public function action(string $userId, Database $dbForConsole): void - { - Authorization::disable(); - Authorization::setDefaultStatus(false); - - Console::title('ClearCardCache V1'); - Console::success(APP_NAME . ' ClearCardCache v1 has started'); - $resources = ['card/' . $userId, 'card-back/' . $userId, 'card-og/' . $userId]; - - $caches = Authorization::skip(fn () => $dbForConsole->find('cache', [ - Query::equal('resource', $resources), - Query::limit(100) - ])); - - $count = \count($caches); - Console::info("Going to delete {$count} cache records in 10 seconds..."); - \sleep(10); - - foreach ($caches as $cache) { - $key = $cache->getId(); - - $cacheFolder = new Cache( - new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-console') - ); - - $cacheFolder->purge($key); - - Authorization::skip(fn () => $dbForConsole->deleteDocument('cache', $cache->getId())); - } - - Console::success(APP_NAME . ' ClearCardCache v1 has finished'); - } -} diff --git a/src/Appwrite/Platform/Tasks/PatchCreateMissingSchedules.php b/src/Appwrite/Platform/Tasks/PatchCreateMissingSchedules.php deleted file mode 100644 index 74ef644498..0000000000 --- a/src/Appwrite/Platform/Tasks/PatchCreateMissingSchedules.php +++ /dev/null @@ -1,97 +0,0 @@ -desc('Ensure every function has a schedule') - ->inject('dbForConsole') - ->inject('getProjectDB') - ->callback(fn (Database $dbForConsole, callable $getProjectDB) => $this->action($dbForConsole, $getProjectDB)); - } - - /** - * Iterate over every function on every project to make sure there is a schedule. If not, recreate the schedule. - */ - public function action(Database $dbForConsole, callable $getProjectDB): void - { - Authorization::disable(); - Authorization::setDefaultStatus(false); - - Console::title('PatchCreateMissingSchedules V1'); - Console::success(APP_NAME . ' PatchCreateMissingSchedules v1 has started'); - - $limit = 100; - $projectCursor = null; - while (true) { - $projectsQueries = [Query::limit($limit)]; - if ($projectCursor !== null) { - $projectsQueries[] = Query::cursorAfter($projectCursor); - } - $projects = $dbForConsole->find('projects', $projectsQueries); - - if (count($projects) === 0) { - break; - } - - foreach ($projects as $project) { - Console::log("Checking Project " . $project->getAttribute('name') . " (" . $project->getId() . ")"); - $dbForProject = $getProjectDB($project); - $functionCursor = null; - - while (true) { - $functionsQueries = [Query::limit($limit)]; - if ($functionCursor !== null) { - $functionsQueries[] = Query::cursorAfter($functionCursor); - } - $functions = $dbForProject->find('functions', $functionsQueries); - if (count($functions) === 0) { - break; - } - - foreach ($functions as $function) { - $scheduleId = $function->getAttribute('scheduleId'); - $schedule = $dbForConsole->getDocument('schedules', $scheduleId); - - if ($schedule->isEmpty()) { - $functionId = $function->getId(); - $schedule = $dbForConsole->createDocument('schedules', new Document([ - '$id' => ID::custom($scheduleId), - 'region' => $project->getAttribute('region', 'default'), - 'resourceType' => 'function', - 'resourceId' => $functionId, - 'resourceUpdatedAt' => DateTime::now(), - 'projectId' => $project->getId(), - 'schedule' => $function->getAttribute('schedule'), - 'active' => !empty($function->getAttribute('schedule')) && !empty($function->getAttribute('deployment')), - ])); - - Console::success('Recreated schedule for function ' . $functionId); - } - } - - $functionCursor = $functions[array_key_last($functions)]; - } - } - - $projectCursor = $projects[array_key_last($projects)]; - } - } -} diff --git a/src/Appwrite/Platform/Tasks/PatchDeleteProjectCollections.php b/src/Appwrite/Platform/Tasks/PatchDeleteProjectCollections.php deleted file mode 100644 index a909e68595..0000000000 --- a/src/Appwrite/Platform/Tasks/PatchDeleteProjectCollections.php +++ /dev/null @@ -1,129 +0,0 @@ -desc('Delete unnecessary project collections') - ->param('offset', 0, new Numeric(), 'Resume deletion from param pos', true) - ->inject('pools') - ->inject('cache') - ->inject('dbForConsole') - ->callback(function (int $offset, Group $pools, Cache $cache, Database $dbForConsole) { - $this->action($offset, $pools, $cache, $dbForConsole); - }); - } - - public function action(int $offset, Group $pools, Cache $cache, Database $dbForConsole): void - { - //docker compose exec -t appwrite patch-delete-project-collections - - Console::title('Delete project collections V1'); - Console::success(APP_NAME . ' delete project collections has started'); - - /* Initialise new Utopia app */ - $app = new App('UTC'); - $console = $app->getResource('console'); - - /** Database connections */ - $totalProjects = $dbForConsole->count('projects'); - Console::success("Found a total of: {$totalProjects} projects"); - - $projects = [$console]; - $count = 0; - $limit = 50; - $sum = 50; - $offset = $offset; - while (!empty($projects)) { - foreach ($projects as $project) { - - /** - * Skip user projects with id 'console' - */ - if ($project->getId() === 'console') { - continue; - } - - Console::info("Deleting collections for {$project->getId()}"); - - try { - $db = $project->getAttribute('database'); - $adapter = $pools - ->get($db) - ->pop() - ->getResource(); - - $dbForProject = new Database($adapter, $cache); - $dbForProject->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); - $dbForProject->setNamespace('_' . $project->getInternalId()); - - foreach ($this->names as $name) { - if (empty($name)) { - continue; - } - if ($dbForProject->exists(App::getEnv('_APP_DB_SCHEMA', 'appwrite'), $name)) { - if ($dbForProject->deleteCollection($name)) { - Console::log('Deleted ' . $name); - } else { - Console::error('Failed to delete ' . $name); - } - } - } - } catch (\Throwable $th) { - Console::error('Failed on project ("' . $project->getId() . '") version with error: ' . $th->getMessage()); - } finally { - $pools - ->get($db) - ->reclaim(); - } - } - - $sum = \count($projects); - - $projects = $dbForConsole->find('projects', [ - Query::limit($limit), - Query::offset($offset), - ]); - - if (!empty($projects)) { - Console::log('Querying..... offset=' . $offset . ' , limit=' . $limit . ', count=' . $count); - } - - $offset = $offset + $limit; - $count = $count + $sum; - } - Console::log('Iterated through ' . $count - 1 . '/' . $totalProjects . ' projects...'); - $pools - ->get('console') - ->reclaim(); - } -} diff --git a/src/Appwrite/Platform/Tasks/PatchDeleteScheduleUpdatedAtAttribute.php b/src/Appwrite/Platform/Tasks/PatchDeleteScheduleUpdatedAtAttribute.php deleted file mode 100644 index 95a7c4ffe1..0000000000 --- a/src/Appwrite/Platform/Tasks/PatchDeleteScheduleUpdatedAtAttribute.php +++ /dev/null @@ -1,74 +0,0 @@ -desc('Ensure function collections do not have scheduleUpdatedAt attribute') - ->inject('pools') - ->inject('dbForConsole') - ->inject('getProjectDB') - ->callback(fn (Group $pools, Database $dbForConsole, callable $getProjectDB) => $this->action($pools, $dbForConsole, $getProjectDB)); - } - - /** - * Iterate over every function on every project to make sure there is a schedule. If not, recreate the schedule. - */ - public function action(Group $pools, Database $dbForConsole, callable $getProjectDB): void - { - Authorization::disable(); - Authorization::setDefaultStatus(false); - - Console::title('PatchDeleteScheduleUpdatedAtAttribute V1'); - Console::success(APP_NAME . ' PatchDeleteScheduleUpdatedAtAttribute v1 has started'); - - $limit = 100; - $projectCursor = null; - while (true) { - $projectsQueries = [Query::limit($limit)]; - if ($projectCursor !== null) { - $projectsQueries[] = Query::cursorAfter($projectCursor); - } - $projects = $dbForConsole->find('projects', $projectsQueries); - - if (count($projects) === 0) { - break; - } - - foreach ($projects as $project) { - Console::log("Checking Project " . $project->getAttribute('name') . " (" . $project->getId() . ")"); - $dbForProject = $getProjectDB($project); - - try { - /** - * Delete 'scheduleUpdatedAt' attribute - */ - $dbForProject->deleteAttribute('functions', 'scheduleUpdatedAt'); - $dbForProject->deleteCachedCollection('functions'); - Console::success("'scheduleUpdatedAt' deleted."); - } catch (\Throwable $th) { - Console::warning("'scheduleUpdatedAt' errored: {$th->getMessage()}"); - } - - $pools->reclaim(); - } - - $projectCursor = $projects[array_key_last($projects)]; - } - } -} diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index 76b86e4f0c..ecdc48376a 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -33,6 +33,9 @@ class Messaging extends Action */ public function __construct() { + + throw new Exception('Exception from the Messaging Worker'); + $this->provider = App::getEnv('_APP_SMS_PROVIDER', ''); if (!empty($this->provider)) { $this->dsn = new DSN($this->provider);