Skip to content

Commit c26110c

Browse files
committed
better users cycle
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent 10b9b20 commit c26110c

1 file changed

Lines changed: 26 additions & 26 deletions

File tree

core/BackgroundJobs/GenerateMetadataJob.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCP\BackgroundJob\TimedJob;
3131
use OCP\Files\Folder;
3232
use OCP\Files\IRootFolder;
33+
use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
3334
use OCP\FilesMetadata\IFilesMetadataManager;
3435
use OCP\IConfig;
3536
use OCP\IUserManager;
@@ -53,38 +54,33 @@ public function __construct(
5354

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

58-
if ($lastMappedUser === '') {
59-
$user = array_key_first($users);
60-
if ($user === null) {
61-
return;
62-
}
63-
64-
$lastMappedUser = $users[$user]->getUID();
65-
}
66-
67-
$startTime = null;
59+
// we'll only start timer once we have found a valid user to handle
60+
// meaning NOW if we have not handled any user from a previous run
61+
$startTime = ($lastHandledUser === '') ? time() : null;
6862
foreach ($users as $user) {
63+
$userId = $user->getUID();
64+
65+
// if we already handled a previous run, we start timer only when we face the last handled user
6966
if ($startTime === null) {
70-
// Skip all user before lastMappedUser.
71-
if ($lastMappedUser !== $user->getUID()) {
72-
continue;
67+
if ($userId === $lastHandledUser) {
68+
$startTime = time();
7369
}
74-
75-
$startTime = time();
70+
continue;
7671
}
7772

73+
$this->config->setAppValue('core', 'metadataGenerationLastHandledUser', $userId);
74+
$this->scanFilesForUser($user->getUID());
75+
7876
// Stop if execution time is more than one hour.
79-
if (time() - $startTime > 60 * 60) {
77+
if (time() - $startTime > 3600) {
8078
return;
8179
}
82-
83-
$this->scanFilesForUser($user->getUID());
84-
$this->config->setAppValue('core', 'metadataGenerationLastHandledUser', $user->getUID());
8580
}
8681

8782
$this->jobList->remove(GenerateMetadataJob::class);
83+
$this->config->deleteAppValue('core', 'metadataGenerationLastHandledUser');
8884
}
8985

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

107103
try {
108-
$this->filesMetadataManager->refreshMetadata(
109-
$node,
110-
IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
111-
);
112-
} catch (\Throwable $ex) {
113-
$this->logger->warning("Error while generating metadata for fileid ".$node->getId(), ['exception' => $ex]);
104+
$this->filesMetadataManager->getMetadata($node->getId(), false);
105+
} catch (FilesMetadataNotFoundException) {
106+
try {
107+
$this->filesMetadataManager->refreshMetadata(
108+
$node,
109+
IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
110+
);
111+
} catch (\Throwable $ex) {
112+
$this->logger->warning("Error while generating metadata for fileid " . $node->getId(), ['exception' => $ex]);
113+
}
114114
}
115115
}
116116
}

0 commit comments

Comments
 (0)