diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 6962035882..7e9450830e 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('type', DeletesV1::TYPE_DOCUMENT) + ->setParam('type', DELETE_TYPE_DOCUMENT) ; $events diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 06daa4b33a..a664d0ebb7 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('type', DeletesV1::TYPE_DOCUMENT) + ->setParam('type', DELETE_TYPE_DOCUMENT) ; $response->noContent(); diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 0315c211ba..3d9b33f012 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('type', DeletesV1::TYPE_DOCUMENT) + ->setParam('type', DELETE_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 d639d50b83..cbddcac56c 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('type', DeletesV1::TYPE_DOCUMENT) + ->setParam('type', DELETE_TYPE_DOCUMENT) ; $events diff --git a/app/init.php b/app/init.php index acaf6277d9..1db02ead44 100644 --- a/app/init.php +++ b/app/init.php @@ -52,7 +52,11 @@ const APP_SOCIAL_INSTAGRAM = 'https://www.instagram.com/appwrite.io'; const APP_SOCIAL_GITHUB = 'https://github.com/appwrite'; const APP_SOCIAL_DISCORD = 'https://appwrite.io/discord'; const APP_SOCIAL_DEV = 'https://dev.to/appwrite'; -const APP_SOCIAL_STACKSHARE = 'https://stackshare.io/appwrite'; +const APP_SOCIAL_STACKSHARE = 'https://stackshare.io/appwrite'; +// Deletion Types +const DELETE_TYPE_DOCUMENT = 'document'; +const DELETE_TYPE_AUDIT = 'audit'; +const DELETE_TYPE_ABUSE = 'abuse'; $register = new Registry(); diff --git a/app/tasks/maintenance.php b/app/tasks/maintenance.php index d826c4dd50..31171a1d1b 100644 --- a/app/tasks/maintenance.php +++ b/app/tasks/maintenance.php @@ -30,7 +30,7 @@ function getConsoleDB() { function notifyDeleteExecutionLogs(array $projectIds) { Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [ - 'type' => DeletesV1::TYPE_DOCUMENT, + 'type' => DELETE_TYPE_DOCUMENT, 'document' => new Document([ '$collection' => Database::SYSTEM_COLLECTION_EXECUTIONS, 'projectIds' => $projectIds @@ -38,24 +38,24 @@ function notifyDeleteExecutionLogs(array $projectIds) ]); } -function notifyDeleteAbuseLogs(array $projectIds, int $timestamp) +function notifyDeleteAbuseLogs(array $projectIds, int $interval) { Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [ - 'type' => DeletesV1::TYPE_ABUSE, + 'type' => DELETE_TYPE_ABUSE, 'document' => new Document([ 'projectIds' => $projectIds, - 'timestamp' => $timestamp, + 'timestamp' => time() - $interval, ]) ]); } -function notifyDeleteAuditLogs(array $projectIds, int $timestamp) +function notifyDeleteAuditLogs(array $projectIds, int $interval) { Resque::enqueue(DELETE_QUEUE_NAME, DELETE_CLASS_NAME, [ - 'type' => DeletesV1::TYPE_AUDIT, + 'type' => DELETE_TYPE_AUDIT, 'document' => new Document([ 'projectIds' => $projectIds, - 'timestamp' => $timestamp + 'timestamp' => time() - $interval ]) ]); } @@ -67,11 +67,11 @@ $cli // # of days in seconds (1 day = 86400s) $interval = App::getEnv('_APP_MAINTENANCE_INTERVAL', '') + 0; //Convert Seconds to microseconds - $interval = $interval * 1000000; + $intervalMicroseconds = $interval * 1000000; $consoleDB = getConsoleDB(); - Console::loop(function() use ($consoleDB){ + Console::loop(function() use ($consoleDB, $interval){ Authorization::disable(); $projects = $consoleDB->getCollection([ @@ -86,9 +86,9 @@ $cli }, $projects); notifyDeleteExecutionLogs($projectIds); - notifyDeleteAbuseLogs($projectIds); - notifyDeleteAuditLogs($projectIds); + notifyDeleteAbuseLogs($projectIds, $interval); + notifyDeleteAuditLogs($projectIds, $interval); - }, $interval); + }, $intervalMicroseconds); }); \ No newline at end of file diff --git a/app/workers/deletes.php b/app/workers/deletes.php index b0e9b6deb8..7d6752e29b 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -20,11 +20,6 @@ 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; @@ -38,10 +33,11 @@ class DeletesV1 $document = $this->args['document']; $document = new Document($document); $projectId = $this->args['projectId']; + $type = $this->args['type']; switch (strval($type)) { - case DeletesV1::TYPE_DOCUMENT: + case DELETE_TYPE_DOCUMENT: switch (strval($document->getCollection())) { case Database::SYSTEM_COLLECTION_PROJECTS: $this->deleteProject($document); @@ -63,11 +59,11 @@ class DeletesV1 break; } break; - case DeletesV1::TYPE_AUDIT: + case DELETE_TYPE_AUDIT: $this->deleteAuditLogs($document); break; - case DeletesV1::TYPE_ABUSE: + case DELETE_TYPE_ABUSE: $this->deleteAbuseLogs($document); break; } @@ -137,29 +133,39 @@ class DeletesV1 { global $register; $projectIds = $document->getAttribute('projectIds', []); + $timestamp = $document->getAttribute('timestamp', 0); + + if($timestamp == 0) { + throw new Exception('Failed to delete abuse logs. No timestamp provided'); + } foreach ($projectIds as $projectId) { $adapter = new AuditAdapter($register->get('db')); $adapter->setNamespace('app_'.$projectId); $audit = new Audit($adapter); - $status = $audit->cleanup(); + $status = $audit->cleanup($timestamp); if (!$status) { throw new Exception('Failed to delete Audit logs for project'.$projectId, 500); } } + } protected function deleteAuditLogs($document) { global $register; $projectIds = $document->getAttribute('projectIds', []); - $timestamp = $document->getAttribute('time', 0); + $timestamp = $document->getAttribute('timestamp', 0); + + if($timestamp == 0) { + throw new Exception('Failed to delete audit logs. No timestamp provided'); + } foreach ($projectIds as $projectId) { $adapter = new AuditAdapter($register->get('db')); $adapter->setNamespace('app_'.$projectId); $audit = new Audit($adapter); - $status = $audit->cleanup(); + $status = $audit->cleanup($timestamp); if (!$status) { throw new Exception('Failed to delete Audit logs for project'.$projectId, 500); }