Use get audit resource for audit cleanup.

This commit is contained in:
Damodar Lohani 2025-12-28 09:01:37 +00:00
parent 69430154e6
commit d2cda9770b

View file

@ -10,6 +10,7 @@ use Executor\Executor;
use Throwable; use Throwable;
use Utopia\Abuse\Adapters\TimeLimit\Database as AbuseDatabase; use Utopia\Abuse\Adapters\TimeLimit\Database as AbuseDatabase;
use Utopia\Audit\Adapter\SQL; use Utopia\Audit\Adapter\SQL;
use Utopia\Audit\Audit;
use Utopia\Cache\Adapter\Filesystem; use Utopia\Cache\Adapter\Filesystem;
use Utopia\Cache\Cache; use Utopia\Cache\Cache;
use Utopia\CLI\Console; use Utopia\CLI\Console;
@ -62,6 +63,7 @@ class Deletes extends Action
->inject('executionRetention') ->inject('executionRetention')
->inject('auditRetention') ->inject('auditRetention')
->inject('log') ->inject('log')
->inject('getAudit')
->callback($this->action(...)); ->callback($this->action(...));
} }
@ -84,7 +86,8 @@ class Deletes extends Action
Executor $executor, Executor $executor,
string $executionRetention, string $executionRetention,
string $auditRetention, string $auditRetention,
Log $log Log $log,
callable $getAudit,
): void { ): void {
$payload = $message->getPayload() ?? []; $payload = $message->getPayload() ?? [];
@ -145,7 +148,7 @@ class Deletes extends Action
break; break;
case DELETE_TYPE_AUDIT: case DELETE_TYPE_AUDIT:
if (!$project->isEmpty()) { if (!$project->isEmpty()) {
$this->deleteAuditLogs($project, $getProjectDB, $auditRetention); $this->deleteAuditLogs($project, $auditRetention, $getAudit);
} }
break; break;
case DELETE_TYPE_REALTIME: case DELETE_TYPE_REALTIME:
@ -777,23 +780,20 @@ class Deletes extends Action
* @param Database $dbForPlatform * @param Database $dbForPlatform
* @param callable $getProjectDB * @param callable $getProjectDB
* @param string $auditRetention * @param string $auditRetention
* @param callable $getAudit
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
private function deleteAuditLogs(Document $project, callable $getProjectDB, string $auditRetention): void private function deleteAuditLogs(Document $project, string $auditRetention, callable $getAudit): void
{ {
$projectId = $project->getId(); $projectId = $project->getId();
$dbForProject = $getProjectDB($project); /** @var Audit $audit */
$audit = $getAudit($project);
try { try {
$this->deleteByGroup(SQL::COLLECTION, [ $audit->cleanup(new \DateTime($auditRetention));
Query::select([...$this->selects, 'time']), } catch (Throwable $th) {
Query::lessThan('time', $auditRetention), Console::error('Failed to delete audit logs for project ' . $projectId . ': ' . $th->getMessage());
Query::orderDesc('time'),
Query::orderAsc(),
], $dbForProject);
} catch (DatabaseException $e) {
Console::error('Failed to delete audit logs for project ' . $projectId . ': ' . $e->getMessage());
} }
} }