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