diff --git a/app/worker.php b/app/worker.php index 59119c692f..539b756869 100644 --- a/app/worker.php +++ b/app/worker.php @@ -21,6 +21,7 @@ use Utopia\Cache\Cache; use Utopia\CLI\Console; use Utopia\Config\Config; use Utopia\Database\Database; +use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; use Utopia\Platform\Service; @@ -102,6 +103,24 @@ Server::setResource('getProjectDB', function (Group $pools, Database $dbForConso }; }, ['pools', 'dbForConsole', 'cache']); +Server::setResource('getProjectAbuseRetention', function () { + return function (Document $project) { + return DateTime::addSeconds(new \DateTime(), -1 * App::getEnv('_APP_MAINTENANCE_RETENTION_ABUSE', 86400)); + }; +}); + +Server::setResource('getProjectAuditRetention', function () { + return function (Document $project) { + return DateTime::addSeconds(new \DateTime(), -1 * App::getEnv('_APP_MAINTENANCE_RETENTION_AUDIT', 1209600)); + }; +}); + +Server::setResource('getProjectExecutionRetention', function () { + return function (Document $project) { + return DateTime::addSeconds(new \DateTime(), -1 * App::getEnv('_APP_MAINTENANCE_RETENTION_EXECUTION', 1209600)); + }; +}); + Server::setResource('cache', function (Registry $register) { $pools = $register->get('pools'); $list = Config::getParam('pools-cache', []); diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 38ff56ce44..4f23db87d1 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -45,14 +45,17 @@ class Deletes extends Action ->inject('getFunctionsDevice') ->inject('getBuildsDevice') ->inject('getCacheDevice') - ->callback(fn ($message, $dbForConsole, callable $getProjectDB, callable $getFilesDevice, callable $getFunctionsDevice, callable $getBuildsDevice, callable $getCacheDevice) => $this->action($message, $dbForConsole, $getProjectDB, $getFilesDevice, $getFunctionsDevice, $getBuildsDevice, $getCacheDevice)); + ->inject('getProjectAbuseRetention') + ->inject('getProjectExecutionRetention') + ->inject('getProjectAuditRetention') + ->callback(fn ($message, $dbForConsole, callable $getProjectDB, callable $getFilesDevice, callable $getFunctionsDevice, callable $getBuildsDevice, callable $getCacheDevice, callable $getProjectAbuseRetention, callable $getProjectExecutionRetention, callable $getProjectAuditRetention) => $this->action($message, $dbForConsole, $getProjectDB, $getFilesDevice, $getFunctionsDevice, $getBuildsDevice, $getCacheDevice, $getProjectAbuseRetention, $getProjectExecutionRetention, $getProjectAuditRetention)); } /** * @throws Exception * @throws Throwable */ - public function action(Message $message, Database $dbForConsole, callable $getProjectDB, callable $getFilesDevice, callable $getFunctionsDevice, callable $getBuildsDevice, callable $getCacheDevice): void + public function action(Message $message, Database $dbForConsole, callable $getProjectDB, callable $getFilesDevice, callable $getFunctionsDevice, callable $getBuildsDevice, callable $getCacheDevice, callable $getProjectAbuseRetention, callable $getProjectExecutionRetention, callable $getProjectAuditRetention): void { $payload = $message->getPayload() ?? []; @@ -114,12 +117,12 @@ class Deletes extends Action break; case DELETE_TYPE_EXECUTIONS: - $this->deleteExecutionLogs($dbForConsole, $getProjectDB, $datetime); + $this->deleteExecutionLogs($dbForConsole, $getProjectDB, $getProjectExecutionRetention); break; case DELETE_TYPE_AUDIT: if (!empty($datetime)) { - $this->deleteAuditLogs($dbForConsole, $getProjectDB, $datetime); + $this->deleteAuditLogs($dbForConsole, $getProjectDB, $getProjectAuditRetention); } if (!$document->isEmpty()) { @@ -127,7 +130,7 @@ class Deletes extends Action } break; case DELETE_TYPE_ABUSE: - $this->deleteAbuseLogs($dbForConsole, $getProjectDB, $datetime); + $this->deleteAbuseLogs($dbForConsole, $getProjectDB, $getProjectAbuseRetention); break; case DELETE_TYPE_REALTIME: @@ -543,10 +546,11 @@ class Deletes extends Action * @return void * @throws Exception */ - private function deleteExecutionLogs(database $dbForConsole, callable $getProjectDB, string $datetime): void + private function deleteExecutionLogs(database $dbForConsole, callable $getProjectDB, callable $getProjectExecutionRetention): void { - $this->deleteForProjectIds($dbForConsole, function (Document $project) use ($getProjectDB, $datetime) { + $this->deleteForProjectIds($dbForConsole, function (Document $project) use ($getProjectDB, $getProjectExecutionRetention) { $dbForProject = $getProjectDB($project); + $datetime = $getProjectExecutionRetention($project); // Delete Executions $this->deleteByGroup('executions', [ Query::lessThan('$createdAt', $datetime) @@ -597,15 +601,12 @@ class Deletes extends Action * @return void * @throws Exception */ - private function deleteAbuseLogs(Database $dbForConsole, callable $getProjectDB, string $datetime): void + private function deleteAbuseLogs(Database $dbForConsole, callable $getProjectDB, callable $getProjectAbuseRetention): void { - if (empty($datetime)) { - throw new Exception('Failed to delete audit logs. No datetime provided'); - } - - $this->deleteForProjectIds($dbForConsole, function (Document $project) use ($getProjectDB, $datetime) { + $this->deleteForProjectIds($dbForConsole, function (Document $project) use ($getProjectDB, $getProjectAbuseRetention) { $projectId = $project->getId(); $dbForProject = $getProjectDB($project); + $datetime = $getProjectAbuseRetention($project); $timeLimit = new TimeLimit("", 0, 1, $dbForProject); $abuse = new Abuse($timeLimit); $status = $abuse->cleanup($datetime); @@ -622,15 +623,12 @@ class Deletes extends Action * @return void * @throws Exception */ - private function deleteAuditLogs(Database $dbForConsole, callable $getProjectDB, string $datetime): void + private function deleteAuditLogs(Database $dbForConsole, callable $getProjectDB, callable $getProjectAuditRetention): void { - if (empty($datetime)) { - throw new Exception('Failed to delete audit logs. No datetime provided'); - } - - $this->deleteForProjectIds($dbForConsole, function (Document $project) use ($getProjectDB, $datetime) { + $this->deleteForProjectIds($dbForConsole, function (Document $project) use ($getProjectDB, $getProjectAuditRetention) { $projectId = $project->getId(); $dbForProject = $getProjectDB($project); + $datetime = $getProjectAuditRetention($project); $audit = new Audit($dbForProject); $status = $audit->cleanup($datetime); if (!$status) {