Merge pull request #9546 from appwrite/feat-improve-maintenance

Use cursor pagination with bigger limit for maintenance project loop
This commit is contained in:
Jake Barnby 2025-03-20 02:51:31 +00:00 committed by GitHub
commit 9345025ade
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -47,13 +47,15 @@ class Maintenance extends Action
Console::info("[{$time}] Notifying workers with maintenance tasks every {$interval} seconds");
$this->foreachProject($dbForPlatform, function (Document $project) use ($queueForDeletes, $usageStatsRetentionHourly) {
$dbForPlatform->foreach('projects', function (Document $project) use ($queueForDeletes, $usageStatsRetentionHourly) {
$queueForDeletes
->setType(DELETE_TYPE_MAINTENANCE)
->setProject($project)
->setUsageRetentionHourlyDateTime(DateTime::addSeconds(new \DateTime(), -1 * $usageStatsRetentionHourly))
->trigger();
});
}, [
Query::limit(100),
]);
$queueForDeletes
->setType(DELETE_TYPE_MAINTENANCE)
@ -68,33 +70,6 @@ class Maintenance extends Action
}, $interval, $delay);
}
protected function foreachProject(Database $dbForPlatform, 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 = $dbForPlatform->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 notifyDeleteConnections(Delete $queueForDeletes): void
{
$queueForDeletes