From d17498e7af0eea507dca0067f063bc76b94982ce Mon Sep 17 00:00:00 2001 From: skjnldsv Date: Thu, 27 Jun 2024 09:21:22 +0200 Subject: [PATCH] feat: spread group folder expiry background job Signed-off-by: skjnldsv --- lib/AppInfo/Application.php | 6 ++- lib/BackgroundJob/ExpireGroupVersions.php | 53 +++++++++++++++++++-- lib/Versions/GroupVersionsExpireManager.php | 7 +++ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 05ce1d0c5..2578bd2e2 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -61,6 +61,7 @@ use OCP\Files\IMimeTypeLoader; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; +use OCP\IAppConfig; use OCP\ICacheFactory; use OCP\IConfig; use OCP\IDBConnection; @@ -195,8 +196,11 @@ public function register(IRegistrationContext $context): void { $context->registerService(\OCA\GroupFolders\BackgroundJob\ExpireGroupVersions::class, function (ContainerInterface $c) { if (interface_exists(\OCA\Files_Versions\Versions\IVersionBackend::class)) { return new ExpireGroupVersionsJob( + $c->get(ITimeFactory::class), $c->get(GroupVersionsExpireManager::class), - $c->get(ITimeFactory::class) + $c->get(IAppConfig::class), + $c->get(FolderManager::class), + $c->get(LoggerInterface::class), ); } diff --git a/lib/BackgroundJob/ExpireGroupVersions.php b/lib/BackgroundJob/ExpireGroupVersions.php index 7f733bf2c..5140c7a64 100644 --- a/lib/BackgroundJob/ExpireGroupVersions.php +++ b/lib/BackgroundJob/ExpireGroupVersions.php @@ -24,15 +24,23 @@ namespace OCA\GroupFolders\BackgroundJob; +use OCA\GroupFolders\AppInfo\Application; +use OCA\GroupFolders\Folder\FolderManager; use OCA\GroupFolders\Versions\GroupVersionsExpireManager; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; +use OCP\IConfig; +use Psr\Log\LoggerInterface; class ExpireGroupVersions extends TimedJob { - private GroupVersionsExpireManager $expireManager; - - public function __construct(GroupVersionsExpireManager $expireManager, ITimeFactory $timeFactory) { - parent::__construct($timeFactory); + public function __construct( + ITimeFactory $time, + private GroupVersionsExpireManager $expireManager, + private IConfig $config, + private FolderManager $folderManager, + private LoggerInterface $logger, + ) { + parent::__construct($time); // Run once per hour $this->setInterval(60 * 60); @@ -42,7 +50,42 @@ public function __construct(GroupVersionsExpireManager $expireManager, ITimeFact $this->expireManager = $expireManager; } + /** + * Expiring groupfolder versions can be quite expensive. + * We need to limit the amount of folders we expire per run. + */ protected function run($argument) { - $this->expireManager->expireAll(); + $lastFolder = (int)$this->config->getAppValue(Application::APP_ID, 'cron_last_folder_index', '0'); + $folders = $this->folderManager->getAllFolders(); + + $folderCount = count($folders); + $currentRunHour = (int)date('G', $this->time->getTime()); + + // Calculate folders to process in the remaining hours, ensure at least one folder is processed + $toDo = max(1, (int)ceil(($folderCount - $lastFolder) / (24 - $currentRunHour))); + + // If there are no folders, we don't need to do anything + if ($folderCount === 0) { + $this->logger->debug('No folders to expire', ['app' => 'cron']); + return; + } + + // If we would go over the end of the list, wrap around + if ($lastFolder >= $folderCount) { + $lastFolder = 0; + } + + // Save the updated folder index BEFORE processing the folders + $this->config->setAppValue(Application::APP_ID, 'cron_last_folder_index', (string)($lastFolder + $toDo)); + + // Determine the set of folders to process + $folderSet = array_slice($folders, $lastFolder, $toDo); + $folderIDs = array_map(function ($folder) { + return $folder['id']; + }, $folderSet); + + // Log and start the expiration process + $this->logger->debug('Expiring versions for ' . count($folderSet) . ' folders', ['app' => 'cron', 'folders' => $folderIDs]); + $this->expireManager->expireFolders($folderSet); } } diff --git a/lib/Versions/GroupVersionsExpireManager.php b/lib/Versions/GroupVersionsExpireManager.php index d631e4139..541e0479a 100644 --- a/lib/Versions/GroupVersionsExpireManager.php +++ b/lib/Versions/GroupVersionsExpireManager.php @@ -61,6 +61,13 @@ public function expireAll(): void { } } + public function expireFolders(array $folders): void { + foreach ($folders as $folder) { + $this->emit(self::class, 'enterFolder', [$folder]); + $this->expireFolder($folder); + } + } + /** * @param array{acl: bool, groups: array>, id: int, mount_point: mixed, quota: int, size: 0} $folder */