Merge pull request #7095 from appwrite/feat-delete-orphaned-projects
Add commit flag to delete orphaned projects task
This commit is contained in:
commit
bc09b50c11
1 changed files with 44 additions and 19 deletions
|
@ -2,17 +2,16 @@
|
||||||
|
|
||||||
namespace Appwrite\Platform\Tasks;
|
namespace Appwrite\Platform\Tasks;
|
||||||
|
|
||||||
use PHPMailer\PHPMailer\PHPMailer;
|
|
||||||
use Utopia\App;
|
use Utopia\App;
|
||||||
use Utopia\Config\Config;
|
use Utopia\Config\Config;
|
||||||
use Utopia\Database\Query;
|
use Utopia\Database\Query;
|
||||||
use Utopia\Database\Validator\Authorization;
|
|
||||||
use Utopia\Platform\Action;
|
use Utopia\Platform\Action;
|
||||||
use Utopia\Cache\Cache;
|
use Utopia\Cache\Cache;
|
||||||
use Utopia\CLI\Console;
|
use Utopia\CLI\Console;
|
||||||
use Utopia\Database\Database;
|
use Utopia\Database\Database;
|
||||||
use Utopia\Pools\Group;
|
use Utopia\Pools\Group;
|
||||||
use Utopia\Registry\Registry;
|
use Utopia\Registry\Registry;
|
||||||
|
use Utopia\Validator\Boolean;
|
||||||
|
|
||||||
class DeleteOrphanedProjects extends Action
|
class DeleteOrphanedProjects extends Action
|
||||||
{
|
{
|
||||||
|
@ -25,18 +24,19 @@ class DeleteOrphanedProjects extends Action
|
||||||
{
|
{
|
||||||
|
|
||||||
$this
|
$this
|
||||||
->desc('Get stats for projects')
|
->desc('Delete orphaned projects')
|
||||||
|
->param('commit', false, new Boolean(true), 'Commit project deletion', true)
|
||||||
->inject('pools')
|
->inject('pools')
|
||||||
->inject('cache')
|
->inject('cache')
|
||||||
->inject('dbForConsole')
|
->inject('dbForConsole')
|
||||||
->inject('register')
|
->inject('register')
|
||||||
->callback(function (Group $pools, Cache $cache, Database $dbForConsole, Registry $register) {
|
->callback(function (bool $commit, Group $pools, Cache $cache, Database $dbForConsole, Registry $register) {
|
||||||
$this->action($pools, $cache, $dbForConsole, $register);
|
$this->action($commit, $pools, $cache, $dbForConsole, $register);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function action(Group $pools, Cache $cache, Database $dbForConsole, Registry $register): void
|
public function action(bool $commit, Group $pools, Cache $cache, Database $dbForConsole, Registry $register): void
|
||||||
{
|
{
|
||||||
|
|
||||||
Console::title('Delete orphaned projects V1');
|
Console::title('Delete orphaned projects V1');
|
||||||
|
@ -54,7 +54,8 @@ class DeleteOrphanedProjects extends Action
|
||||||
$totalProjects = $dbForConsole->count('projects');
|
$totalProjects = $dbForConsole->count('projects');
|
||||||
Console::success("Found a total of: {$totalProjects} projects");
|
Console::success("Found a total of: {$totalProjects} projects");
|
||||||
|
|
||||||
$orphans = 0;
|
$orphans = 1;
|
||||||
|
$cnt = 0;
|
||||||
$count = 0;
|
$count = 0;
|
||||||
$limit = 30;
|
$limit = 30;
|
||||||
$sum = 30;
|
$sum = 30;
|
||||||
|
@ -79,19 +80,43 @@ class DeleteOrphanedProjects extends Action
|
||||||
$dbForProject = new Database($adapter, $cache);
|
$dbForProject = new Database($adapter, $cache);
|
||||||
$dbForProject->setDefaultDatabase('appwrite');
|
$dbForProject->setDefaultDatabase('appwrite');
|
||||||
$dbForProject->setNamespace('_' . $project->getInternalId());
|
$dbForProject->setNamespace('_' . $project->getInternalId());
|
||||||
$collectionsCreated = $dbForProject->count(Database::METADATA);
|
$collectionsCreated = 0;
|
||||||
$message = ' (' . $collectionsCreated . ') collections where found on project (' . $project->getId() . '))';
|
$cnt++;
|
||||||
if ($collectionsCreated < (count($collectionsConfig) + 2)) {
|
if ($dbForProject->exists($dbForProject->getDefaultDatabase(), Database::METADATA)) {
|
||||||
Console::error($message);
|
$collectionsCreated = $dbForProject->count(Database::METADATA);
|
||||||
$orphans++;
|
|
||||||
} else {
|
|
||||||
Console::log($message);
|
|
||||||
}
|
}
|
||||||
} catch (\Throwable $th) {
|
|
||||||
//$dbForConsole->deleteDocument('projects', $project->getId());
|
$msg = '(' . $cnt . ') found (' . $collectionsCreated . ') collections on project (' . $project->getInternalId() . ') , database (' . $project['database'] . ')';
|
||||||
//Console::success('Deleting project (' . $project->getId() . ')');
|
/**
|
||||||
Console::error(' (0) collections where found for project (' . $project->getId() . ')');
|
* +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++;
|
$orphans++;
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
Console::error('Error: ' . $th->getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
$pools
|
$pools
|
||||||
->get($db)
|
->get($db)
|
||||||
|
@ -110,6 +135,6 @@ class DeleteOrphanedProjects extends Action
|
||||||
$count = $count + $sum;
|
$count = $count + $sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
Console::log('Iterated through ' . $count - 1 . '/' . $totalProjects . ' projects found ' . $orphans . ' orphans');
|
Console::log('Iterated through ' . $count - 1 . '/' . $totalProjects . ' projects found ' . $orphans - 1 . ' orphans');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue