diff --git a/Dockerfile b/Dockerfile index 8cea48154..33b143465 100755 --- a/Dockerfile +++ b/Dockerfile @@ -101,6 +101,7 @@ RUN chmod +x /usr/local/bin/hamster && \ chmod +x /usr/local/bin/volume-sync && \ chmod +x /usr/local/bin/patch-delete-schedule-updated-at-attribute && \ chmod +x /usr/local/bin/patch-delete-project-collections && \ + chmod +x /usr/local/bin/delete-orphaned-projects && \ chmod +x /usr/local/bin/clear-card-cache && \ chmod +x /usr/local/bin/calc-users-stats && \ chmod +x /usr/local/bin/calc-tier-stats diff --git a/app/console b/app/console index fe835e503..e96573898 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit fe835e50328ed80f67c66d2d449c0f7b51ade544 +Subproject commit e9657389879c8d76a9b3a0d3486c1d86f43c3bb9 diff --git a/bin/delete-orphaned-projects b/bin/delete-orphaned-projects new file mode 100644 index 000000000..16b9e3184 --- /dev/null +++ b/bin/delete-orphaned-projects @@ -0,0 +1,3 @@ +#!/bin/sh + +php /usr/src/code/app/cli.php delete-orphaned-projects $@ \ No newline at end of file diff --git a/src/Appwrite/Platform/Services/Tasks.php b/src/Appwrite/Platform/Services/Tasks.php index 00779084d..e725ff5f3 100644 --- a/src/Appwrite/Platform/Services/Tasks.php +++ b/src/Appwrite/Platform/Services/Tasks.php @@ -18,6 +18,7 @@ use Appwrite\Platform\Tasks\Version; use Appwrite\Platform\Tasks\VolumeSync; use Appwrite\Platform\Tasks\CalcTierStats; use Appwrite\Platform\Tasks\Upgrade; +use Appwrite\Platform\Tasks\DeleteOrphanedProjects; class Tasks extends Service { @@ -40,6 +41,8 @@ class Tasks extends Service ->addAction(VolumeSync::getName(), new VolumeSync()) ->addAction(Specs::getName(), new Specs()) ->addAction(CalcTierStats::getName(), new CalcTierStats()) + ->addAction(DeleteOrphanedProjects::getName(), new DeleteOrphanedProjects()) + ; } } diff --git a/src/Appwrite/Platform/Tasks/DeleteOrphanedProjects.php b/src/Appwrite/Platform/Tasks/DeleteOrphanedProjects.php new file mode 100644 index 000000000..88f4ae8ce --- /dev/null +++ b/src/Appwrite/Platform/Tasks/DeleteOrphanedProjects.php @@ -0,0 +1,108 @@ +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 delete-orphaned-projects + + Console::title('Delete orphaned projects V1'); + Console::success(APP_NAME . ' Delete orphaned projects started'); + + /** @var array $collections */ + $collectionsConfig = Config::getParam('collections', [])['projects'] ?? []; + + /* Initialise new Utopia app */ + $app = new App('UTC'); + $console = $app->getResource('console'); + $projects = [$console]; + + /** Database connections */ + $totalProjects = $dbForConsole->count('projects'); + Console::success("Found a total of: {$totalProjects} projects"); + + $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()); + $collectionsCreated = $dbForProject->count(Database::METADATA); + } catch (\Throwable $th) { + $dbForConsole->deleteDocument('projects', $project->getId()); + Console::success('Deleting project (' . $project->getId() . ')'); + } 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...'); + } +}