delete orphaned projects task

This commit is contained in:
shimon 2023-11-13 19:24:55 +02:00
parent 0f3c438976
commit dd0bf66212

View file

@ -2,11 +2,9 @@
namespace Appwrite\Platform\Tasks;
use PHPMailer\PHPMailer\PHPMailer;
use Utopia\App;
use Utopia\Config\Config;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\Platform\Action;
use Utopia\Cache\Cache;
use Utopia\CLI\Console;
@ -14,7 +12,6 @@ use Utopia\Database\Database;
use Utopia\Pools\Group;
use Utopia\Registry\Registry;
use Utopia\Validator\Boolean;
use Utopia\Validator\Hostname;
class DeleteOrphanedProjects extends Action
{
@ -27,7 +24,7 @@ class DeleteOrphanedProjects extends Action
{
$this
->desc('Get stats for projects')
->desc('Delete orphaned projects')
->param('commit', false, new boolean(true), 'Commit project deletion', true)
->inject('pools')
->inject('cache')
@ -45,6 +42,9 @@ class DeleteOrphanedProjects extends Action
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');
@ -55,6 +55,7 @@ class DeleteOrphanedProjects extends Action
Console::success("Found a total of: {$totalProjects} projects");
$orphans = 1;
$cnt = 0;
$count = 0;
$limit = 30;
$sum = 30;
@ -79,24 +80,43 @@ class DeleteOrphanedProjects extends Action
$dbForProject = new Database($adapter, $cache);
$dbForProject->setDefaultDatabase('appwrite');
$dbForProject->setNamespace('_' . $project->getInternalId());
$collectionsCreated = $dbForProject->count(Database::METADATA);
if ($collectionsCreated === 0) {
if ($commit === true) {
Console::info('(' . $orphans . ') deleting project (' . $project->getId() . ')');
$this->deleteProject($dbForConsole, $project->getId());
} else {
Console::log('(' . $orphans . ') project (' . $project->getId() . ')');
$collectionsCreated = 0;
$cnt++;
if ($dbForProject->exists($dbForProject->getDefaultDatabase(), Database::METADATA)) {
$collectionsCreated = $dbForProject->count(Database::METADATA);
}
$msg = '(' . $cnt . ') ignoring found (' . $collectionsCreated . ') collections on project (' . $project->getInternalId() . ') , database (' . $project['database'] . ')';
/**
* +2 == audit+abuse
*/
if ($collectionsCreated === (count($collectionsConfig) + 2)) {
Console::log($msg . ' ignoring....');
continue;
}
Console::log($msg);
if ($collectionsCreated > 0) {
$collections = $dbForProject->find(Database::METADATA, []);
foreach ($collections as $collection) {
if ($commit) {
$dbForProject->deleteCollection($collection->getId());
$dbForConsole->deleteCachedCollection($collection->getId());
}
Console::info('--Deleting collection (' . $collection->getId() . ') project no (' . $project->getInternalId() . ')');
}
}
if ($commit) {
$dbForConsole->deleteDocument('projects', $project->getId());
$dbForConsole->deleteCachedDocument('projects', $project->getId());
}
Console::info('--Deleting project no (' . $project->getInternalId() . ')');
$orphans++;
}
} catch (\Throwable $th) {
if ($commit === true) {
Console::info('(' . $orphans . ') deleting project (' . $project->getId() . ')');
$this->deleteProject($dbForConsole, $project->getId());
} else {
Console::log('(' . $orphans . ') project (' . $project->getId() . ')');
}
$orphans++;
Console::error('Error: ' . $th->getMessage());
} finally {
$pools
->get($db)
@ -117,13 +137,4 @@ class DeleteOrphanedProjects extends Action
Console::log('Iterated through ' . $count - 1 . '/' . $totalProjects . ' projects found ' . $orphans - 1 . ' orphans');
}
private function deleteProject(Database $dbForConsole, $projectId): void
{
try {
$dbForConsole->deleteDocument('projects', $projectId);
} catch (\Throwable $th) {
Console::error('Error when trying to delete project (' . $projectId . ') ' . $th->getMessage());
}
}
}