From 1ecbf8bc5d90eb98c80d04b77361b44fd53c2d6f Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 18 Dec 2020 17:58:08 +0530 Subject: [PATCH] patch: refactor delete worker invocation --- app/controllers/api/database.php | 2 +- app/controllers/api/functions.php | 2 +- app/controllers/api/projects.php | 2 +- app/controllers/api/users.php | 2 +- app/tasks/maintenance.php | 18 +++++++---- app/workers/deletes.php | 54 ++++++++++++++++++++----------- 6 files changed, 51 insertions(+), 29 deletions(-) diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index d8cf23615..696203588 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -253,7 +253,7 @@ App::delete('/v1/database/collections/:collectionId') $deletes ->setParam('document', $collection) - ->setParam('collection', Database::SYSTEM_COLLECTION_COLLECTIONS) + ->setParam('type', DeletesV1::TYPE_DOCUMENT) ; $events diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 62f5a6659..06daa4b33 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -366,7 +366,7 @@ App::delete('/v1/functions/:functionId') $deletes ->setParam('document', $function->getArrayCopy()) - ->setParam('collection', Database::SYSTEM_COLLECTION_FUNCTIONS) + ->setParam('type', DeletesV1::TYPE_DOCUMENT) ; $response->noContent(); diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 1d3474b0e..0315c211b 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -431,7 +431,7 @@ App::delete('/v1/projects/:projectId') $deletes ->setParam('document', $project->getArrayCopy()) - ->setParam('collection', Database::SYSTEM_COLLECTION_PROJECTS) + ->setParam('type', DeletesV1::TYPE_DOCUMENT) ; foreach (['keys', 'webhooks', 'tasks', 'platforms', 'domains'] as $key) { // Delete all children (keys, webhooks, tasks [stop tasks?], platforms) diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 4fca950fd..d639d50b8 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -517,7 +517,7 @@ App::delete('/v1/users/:userId') $deletes ->setParam('document', $user) - ->setParam('collection', Database::SYSTEM_COLLECTION_USERS) + ->setParam('type', DeletesV1::TYPE_DOCUMENT) ; $events diff --git a/app/tasks/maintenance.php b/app/tasks/maintenance.php index f8c6cca59..d826c4dd5 100644 --- a/app/tasks/maintenance.php +++ b/app/tasks/maintenance.php @@ -9,6 +9,7 @@ use Appwrite\Database\Document; use Appwrite\Database\Adapter\MySQL as MySQLAdapter; use Appwrite\Database\Adapter\Redis as RedisAdapter; use Appwrite\Database\Validator\Authorization; +use Swoole\FastCGI\Record\Data; use Utopia\App; use Utopia\CLI\Console; use Utopia\Config\Config; @@ -29,29 +30,32 @@ function getConsoleDB() { function notifyDeleteExecutionLogs(array $projectIds) { Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [ - 'collection' => Database::SYSTEM_COLLECTION_EXECUTIONS, + 'type' => DeletesV1::TYPE_DOCUMENT, 'document' => new Document([ + '$collection' => Database::SYSTEM_COLLECTION_EXECUTIONS, 'projectIds' => $projectIds ]) ]); } -function notifyDeleteAbuseLogs(array $projectIds) +function notifyDeleteAbuseLogs(array $projectIds, int $timestamp) { Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [ - 'collection' => Database::SYSTEM_COLLECTION_EXECUTIONS, + 'type' => DeletesV1::TYPE_ABUSE, 'document' => new Document([ - 'projectIds' => $projectIds + 'projectIds' => $projectIds, + 'timestamp' => $timestamp, ]) ]); } -function notifyDeleteAuditLogs(array $projectIds) +function notifyDeleteAuditLogs(array $projectIds, int $timestamp) { Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [ - 'collection' => Database::SYSTEM_COLLECTION_EXECUTIONS, + 'type' => DeletesV1::TYPE_AUDIT, 'document' => new Document([ - 'projectIds' => $projectIds + 'projectIds' => $projectIds, + 'timestamp' => $timestamp ]) ]); } diff --git a/app/workers/deletes.php b/app/workers/deletes.php index 7ea5e1657..b0e9b6deb 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -19,6 +19,12 @@ use Utopia\Audit\Adapters\MySQL as AuditAdapter; class DeletesV1 { + + // Deletion Types + const TYPE_DOCUMENT = 'document'; + const TYPE_AUDIT = 'audit'; + const TYPE_ABUSE = 'abuse'; + public $args = []; protected $consoleDB = null; @@ -32,28 +38,39 @@ class DeletesV1 $document = $this->args['document']; $document = new Document($document); $projectId = $this->args['projectId']; - $collection = $this->args['collection']; + $type = $this->args['type']; - switch (strval($collection)) { - case Database::SYSTEM_COLLECTION_PROJECTS: - $this->deleteProject($document); + switch (strval($type)) { + case DeletesV1::TYPE_DOCUMENT: + switch (strval($document->getCollection())) { + case Database::SYSTEM_COLLECTION_PROJECTS: + $this->deleteProject($document); + break; + case Database::SYSTEM_COLLECTION_FUNCTIONS: + $this->deleteFunction($document, $projectId); + break; + case Database::SYSTEM_COLLECTION_EXECUTIONS: + $this->deleteExecutionLogs($document); + break; + case Database::SYSTEM_COLLECTION_USERS: + $this->deleteUser($document, $projectId); + break; + case Database::SYSTEM_COLLECTION_COLLECTIONS: + $this->deleteDocuments($document, $projectId); + break; + default: + Console::error('No lazy delete operation available for document of type: '.$document->getCollection()); + break; + } break; - case Database::SYSTEM_COLLECTION_FUNCTIONS: - $this->deleteFunction($document, $projectId); + case DeletesV1::TYPE_AUDIT: + $this->deleteAuditLogs($document); break; - case Database::SYSTEM_COLLECTION_EXECUTIONS: - $this->deleteExecutionLogs($document); + + case DeletesV1::TYPE_ABUSE: + $this->deleteAbuseLogs($document); break; - case Database::SYSTEM_COLLECTION_USERS: - $this->deleteUser($document, $projectId); - break; - case Database::SYSTEM_COLLECTION_COLLECTIONS: - $this->deleteDocuments($document, $projectId); - break; - default: - Console::error('No lazy delete operation available for document of type: '.$document->getCollection()); - break; - } + } } public function tearDown(): void @@ -136,6 +153,7 @@ class DeletesV1 { global $register; $projectIds = $document->getAttribute('projectIds', []); + $timestamp = $document->getAttribute('time', 0); foreach ($projectIds as $projectId) { $adapter = new AuditAdapter($register->get('db'));