diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 43c9c3e48..4145888d6 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..360b6894c 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\IAppConfig; +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 IAppConfig $appConfig, + 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 = $this->appConfig->getValueInt(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->appConfig->setValueInt(Application::APP_ID, 'cron_last_folder_index', $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 */