Skip to content

Commit

Permalink
better users cycle
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl authored and backportbot-nextcloud[bot] committed Jan 11, 2024
1 parent 2b378f6 commit 4beccd6
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions core/BackgroundJobs/GenerateMetadataJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCP\BackgroundJob\TimedJob;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\IConfig;
use OCP\IUserManager;
Expand All @@ -53,38 +54,33 @@ public function __construct(

protected function run(mixed $argument): void {
$users = $this->userManager->search('');
$lastMappedUser = $this->config->getAppValue('core', 'metadataGenerationLastHandledUser', '');
$lastHandledUser = $this->config->getAppValue('core', 'metadataGenerationLastHandledUser', '');

if ($lastMappedUser === '') {
$user = array_key_first($users);
if ($user === null) {
return;
}

$lastMappedUser = $users[$user]->getUID();
}

$startTime = null;
// we'll only start timer once we have found a valid user to handle
// meaning NOW if we have not handled any user from a previous run
$startTime = ($lastHandledUser === '') ? time() : null;
foreach ($users as $user) {
$userId = $user->getUID();

// if we already handled a previous run, we start timer only when we face the last handled user
if ($startTime === null) {
// Skip all user before lastMappedUser.
if ($lastMappedUser !== $user->getUID()) {
continue;
if ($userId === $lastHandledUser) {
$startTime = time();
}

$startTime = time();
continue;
}

$this->config->setAppValue('core', 'metadataGenerationLastHandledUser', $userId);
$this->scanFilesForUser($user->getUID());

// Stop if execution time is more than one hour.
if (time() - $startTime > 60 * 60) {
if (time() - $startTime > 3600) {
return;
}

$this->scanFilesForUser($user->getUID());
$this->config->setAppValue('core', 'metadataGenerationLastHandledUser', $user->getUID());
}

$this->jobList->remove(GenerateMetadataJob::class);
$this->config->deleteAppValue('core', 'metadataGenerationLastHandledUser');
}

private function scanFilesForUser(string $userId): void {
Expand All @@ -105,12 +101,16 @@ private function scanFolder(Folder $folder): void {
}

try {
$this->filesMetadataManager->refreshMetadata(
$node,
IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
);
} catch (\Throwable $ex) {
$this->logger->warning("Error while generating metadata for fileid ".$node->getId(), ['exception' => $ex]);
$this->filesMetadataManager->getMetadata($node->getId(), false);
} catch (FilesMetadataNotFoundException) {
try {
$this->filesMetadataManager->refreshMetadata(
$node,
IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
);
} catch (\Throwable $ex) {
$this->logger->warning("Error while generating metadata for fileid " . $node->getId(), ['exception' => $ex]);
}
}
}
}
Expand Down

0 comments on commit 4beccd6

Please sign in to comment.