Skip to content

Commit

Permalink
feat: spread group folder expiry background job
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>

[skip ci]
  • Loading branch information
skjnldsv authored and backportbot[bot] committed Jul 16, 2024
1 parent e887cef commit c6343db
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,11 @@ public function register(IRegistrationContext $context): void {
$context->registerService(\OCA\GroupFolders\BackgroundJob\ExpireGroupVersions::class, function (IAppContainer $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),
);
}

Expand Down
51 changes: 46 additions & 5 deletions lib/BackgroundJob/ExpireGroupVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@

namespace OCA\GroupFolders\BackgroundJob;

use OCA\GroupFolders\AppInfo\Application;
use OCA\GroupFolders\Folder\FolderManager;
use OCA\GroupFolders\Versions\GroupVersionsExpireManager;
use OCP\BackgroundJob\TimedJob;
use OCP\AppFramework\Utility\ITimeFactory;

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);
Expand All @@ -42,7 +48,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);
}
}
7 changes: 7 additions & 0 deletions lib/Versions/GroupVersionsExpireManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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{id: int, mount_point: string, groups: array<empty, empty>|array<array-key, int>, quota: int, size: int, acl: bool} $folder
*/
Expand Down

0 comments on commit c6343db

Please sign in to comment.