From e225bbe3c911e681b4f40249f03dfe16990098d1 Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 14 Jun 2023 17:42:23 +0300 Subject: [PATCH] delete unnecessary project collections task --- Dockerfile | 1 + app/console | 2 +- bin/delete-project-collections | 3 + src/Appwrite/Platform/Services/Tasks.php | 2 + .../Tasks/DeleteProjectCollections.php | 129 ++++++++++++++++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 bin/delete-project-collections create mode 100644 src/Appwrite/Platform/Tasks/DeleteProjectCollections.php diff --git a/Dockerfile b/Dockerfile index b0ae0248e5..0239b308d9 100755 --- a/Dockerfile +++ b/Dockerfile @@ -121,6 +121,7 @@ RUN chmod +x /usr/local/bin/doctor && \ chmod +x /usr/local/bin/clear-card-cache && \ chmod +x /usr/local/bin/calc-users-stats && \ chmod +x /usr/local/bin/calc-tier-stats && \ + chmod +x /usr/local/bin/delete-project-collections && \ chmod +x /usr/local/bin/maintenance && \ chmod +x /usr/local/bin/volume-sync && \ chmod +x /usr/local/bin/usage && \ diff --git a/app/console b/app/console index b981302dee..cad6f3b1bf 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit b981302dee30eab33e155af79f0088822b29a2b6 +Subproject commit cad6f3b1bfdae4d423ba6f0735ba2a5cd5a58551 diff --git a/bin/delete-project-collections b/bin/delete-project-collections new file mode 100644 index 0000000000..3991342962 --- /dev/null +++ b/bin/delete-project-collections @@ -0,0 +1,3 @@ +#!/bin/sh + +php /usr/src/code/app/cli.php delete-project-collections $@ \ No newline at end of file diff --git a/src/Appwrite/Platform/Services/Tasks.php b/src/Appwrite/Platform/Services/Tasks.php index 7e8d9a1333..fc40049f7c 100644 --- a/src/Appwrite/Platform/Services/Tasks.php +++ b/src/Appwrite/Platform/Services/Tasks.php @@ -21,6 +21,7 @@ use Appwrite\Platform\Tasks\Version; use Appwrite\Platform\Tasks\VolumeSync; use Appwrite\Platform\Tasks\CalcUsersStats; use Appwrite\Platform\Tasks\CalcTierStats; +use Appwrite\Platform\Tasks\DeleteProjectCollections; class Tasks extends Service { @@ -46,6 +47,7 @@ class Tasks extends Service ->addAction(Specs::getName(), new Specs()) ->addAction(CalcUsersStats::getName(), new CalcUsersStats()) ->addAction(CalcTierStats::getName(), new CalcTierStats()) + ->addAction(DeleteProjectCollections::getName(), new DeleteProjectCollections()) ; } } diff --git a/src/Appwrite/Platform/Tasks/DeleteProjectCollections.php b/src/Appwrite/Platform/Tasks/DeleteProjectCollections.php new file mode 100644 index 0000000000..a083caeab2 --- /dev/null +++ b/src/Appwrite/Platform/Tasks/DeleteProjectCollections.php @@ -0,0 +1,129 @@ +desc('Delete unnecessary project collections') + ->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 delete-project-collections + + 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'); + + /** 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()); + + foreach ($this->names as $name) { + if ($dbForProject->exists('appwrite', $name)) { + if ($dbForProject->deleteCollection($name)) { + Console::log('Deleted ' . $name); + } else { + Console::error('Failed to delete ' . $name); + } + + if ($dbForProject->deleteCachedCollection($name)) { + Console::log('Deleted (cached) ' . $name); + } else { + Console::error('Failed to delete (cached) ' . $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), + ]); + + $offset = $offset + $limit; + $count = $count + $sum; + } + Console::log('Iterated through ' . $count - 1 . '/' . $totalProjects . ' projects...'); + $pools + ->get('console') + ->reclaim(); + } +}