From 5512123e042a2c74fe43d033e69f182545ed76a8 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 12 Dec 2023 11:26:28 +0000 Subject: [PATCH] loop through projects --- src/Appwrite/Platform/Tasks/Maintenance.php | 43 ++++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/Appwrite/Platform/Tasks/Maintenance.php b/src/Appwrite/Platform/Tasks/Maintenance.php index ecc2b26fd1..02198d26ca 100644 --- a/src/Appwrite/Platform/Tasks/Maintenance.php +++ b/src/Appwrite/Platform/Tasks/Maintenance.php @@ -44,18 +44,51 @@ class Maintenance extends Action $time = DateTime::now(); Console::info("[{$time}] Notifying workers with maintenance tasks every {$interval} seconds"); - $this->notifyDeleteExecutionLogs($queueForDeletes); - $this->notifyDeleteAbuseLogs($queueForDeletes); - $this->notifyDeleteAuditLogs($queueForDeletes); - $this->notifyDeleteUsageStats($usageStatsRetentionHourly, $queueForDeletes); + + $this->foreachProject($dbForConsole, function (Document $project) use ($queueForDeletes, $usageStatsRetentionHourly) { + $queueForDeletes->setProject($project); + + $this->notifyDeleteExecutionLogs($queueForDeletes); + $this->notifyDeleteAbuseLogs($queueForDeletes); + $this->notifyDeleteAuditLogs($queueForDeletes); + $this->notifyDeleteUsageStats($usageStatsRetentionHourly, $queueForDeletes); + $this->notifyDeleteExpiredSessions($queueForDeletes); + }); + $this->notifyDeleteConnections($queueForDeletes); - $this->notifyDeleteExpiredSessions($queueForDeletes); $this->renewCertificates($dbForConsole, $queueForCertificates); $this->notifyDeleteCache($cacheRetention, $queueForDeletes); $this->notifyDeleteSchedules($schedulesDeletionRetention, $queueForDeletes); }, $interval); } + protected function foreachProject(Database $dbForConsole, callable $callback): void + { + // TODO: @Meldiron name of this method no longer matches. It does not delete, and it gives whole document + $count = 0; + $chunk = 0; + $limit = 50; + $sum = $limit; + $executionStart = \microtime(true); + + while ($sum === $limit) { + $projects = $dbForConsole->find('projects', [Query::limit($limit), Query::offset($chunk * $limit)]); + + $chunk++; + + /** @var string[] $projectIds */ + $sum = count($projects); + + foreach ($projects as $project) { + $callback($project); + $count++; + } + } + + $executionEnd = \microtime(true); + Console::info("Found {$count} projects " . ($executionEnd - $executionStart) . " seconds"); + } + private function notifyDeleteExecutionLogs(Delete $queueForDeletes): void { ($queueForDeletes)