diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 46f82cf42..21f961b18 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; @@ -196,8 +197,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..bfac38974 100644 --- a/lib/BackgroundJob/ExpireGroupVersions.php +++ b/lib/BackgroundJob/ExpireGroupVersions.php @@ -24,14 +24,22 @@ 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\IAppConfig; +use Psr\Log\LoggerInterface; class ExpireGroupVersions extends TimedJob { - private GroupVersionsExpireManager $expireManager; - - public function __construct(GroupVersionsExpireManager $expireManager, ITimeFactory $timeFactory) { + public function __construct( + private ITimeFactory $timeFactory, + private GroupVersionsExpireManager $expireManager, + private IAppConfig $appConfig, + private FolderManager $folderManager, + private LoggerInterface $logger, + ) { parent::__construct($timeFactory); // Run once per hour @@ -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 = $this->appConfig->getValueInt(Application::APP_ID, 'cron_last_folder_index', 0); + $folders = $this->folderManager->getAllFolders(); + + $folderCount = count($folders); + $currentRunHour = (int)date('G', $this->timeFactory->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 + $toDo >= $folderCount) { + $lastFolder = 0; + } + + // Determine the set of folders to process + $folderSet = array_slice($folders, $lastFolder, $toDo); + $this->expireManager->expireFolders($folderSet); + + // Map IDs for logging + $folderIDs = array_map(function($folder) { + return $folder['id']; + }, $folderSet); + + // Save the updated folder index + $this->appConfig->setValueInt(Application::APP_ID, 'cron_last_folder_index', $lastFolder + $toDo); + $this->logger->debug('Expiring versions for ' . count($folderSet) . ' folders', ['app' => 'cron', 'folders' => $folderIDs]); } } 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 */