Skip to content

Commit e507998

Browse files
committed
Add metadata generation job
Signed-off-by: Louis Chemineau <louis@chmn.me>
1 parent 9c3350b commit e507998

6 files changed

Lines changed: 157 additions & 2 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2023 Louis Chemineau <louis@chmn.me>
6+
*
7+
* @author Louis Chemineau <louis@chmn.me>
8+
*
9+
* @license AGPL-3.0-or-later
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
namespace OC\Core\BackgroundJobs;
27+
28+
use OCP\AppFramework\Utility\ITimeFactory;
29+
use OCP\BackgroundJob\IJobList;
30+
use OCP\BackgroundJob\TimedJob;
31+
use OCP\Files\Folder;
32+
use OCP\Files\IRootFolder;
33+
use OCP\FilesMetadata\IFilesMetadataManager;
34+
use OCP\IConfig;
35+
use OCP\IUserManager;
36+
37+
class GenerateMetadataJob extends TimedJob {
38+
public function __construct(
39+
ITimeFactory $time,
40+
private IConfig $config,
41+
private IRootFolder $rootFolder,
42+
private IUserManager $userManager,
43+
private IFilesMetadataManager $filesMetadataManager,
44+
private IJobList $jobList,
45+
) {
46+
parent::__construct($time);
47+
48+
$this->setTimeSensitivity(\OCP\BackgroundJob\IJob::TIME_INSENSITIVE);
49+
$this->setInterval(24 * 3600);
50+
}
51+
52+
protected function run(mixed $argument): void {
53+
$users = $this->userManager->search('');
54+
$lastMappedUser = $this->config->getAppValue('core', 'metadataGenerationLastHandledUser', '');
55+
56+
if ($lastMappedUser === '') {
57+
$lastMappedUser = $users[array_key_first($users)]->getUID();
58+
}
59+
60+
$startTime = null;
61+
foreach ($users as $user) {
62+
if ($startTime === null) {
63+
// Skip all user before lastMappedUser.
64+
if ($lastMappedUser !== $user->getUID()) {
65+
continue;
66+
}
67+
68+
$startTime = time();
69+
}
70+
71+
// Stop if execution time is more than one hour.
72+
if (time() - $startTime > 60 * 60) {
73+
return;
74+
}
75+
76+
$this->scanFilesForUser($user->getUID());
77+
$this->config->setAppValue('core', 'metadataGenerationLastHandledUser', $user->getUID());
78+
}
79+
80+
$this->jobList->remove(GenerateMetadataJob::class);
81+
}
82+
83+
private function scanFilesForUser(string $userId): void {
84+
$userFolder = $this->rootFolder->getUserFolder($userId);
85+
$this->scanFolder($userFolder);
86+
}
87+
88+
private function scanFolder(Folder $folder): void {
89+
// Do not scan share and other moveable mounts.
90+
if ($folder->getMountPoint() instanceof \OC\Files\Mount\MoveableMount) {
91+
return;
92+
}
93+
94+
foreach ($folder->getDirectoryListing() as $node) {
95+
if ($node instanceof Folder) {
96+
$this->scanFolder($node);
97+
continue;
98+
}
99+
100+
$this->filesMetadataManager->refreshMetadata(
101+
$node,
102+
IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
103+
);
104+
}
105+
}
106+
}

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@
10021002
'OC\\Core\\BackgroundJobs\\BackgroundCleanupUpdaterBackupsJob' => $baseDir . '/core/BackgroundJobs/BackgroundCleanupUpdaterBackupsJob.php',
10031003
'OC\\Core\\BackgroundJobs\\CheckForUserCertificates' => $baseDir . '/core/BackgroundJobs/CheckForUserCertificates.php',
10041004
'OC\\Core\\BackgroundJobs\\CleanupLoginFlowV2' => $baseDir . '/core/BackgroundJobs/CleanupLoginFlowV2.php',
1005+
'OC\\Core\\BackgroundJobs\\GenerateMetadataJob' => $baseDir . '/core/BackgroundJobs/GenerateMetadataJob.php',
10051006
'OC\\Core\\BackgroundJobs\\LookupServerSendCheckBackgroundJob' => $baseDir . '/core/BackgroundJobs/LookupServerSendCheckBackgroundJob.php',
10061007
'OC\\Core\\BackgroundJobs\\MigrateMetadataJob' => $baseDir . '/core/BackgroundJobs/MigrateMetadataJob.php',
10071008
'OC\\Core\\Command\\App\\Disable' => $baseDir . '/core/Command/App/Disable.php',
@@ -1603,6 +1604,7 @@
16031604
'OC\\RepairException' => $baseDir . '/lib/private/RepairException.php',
16041605
'OC\\Repair\\AddBruteForceCleanupJob' => $baseDir . '/lib/private/Repair/AddBruteForceCleanupJob.php',
16051606
'OC\\Repair\\AddCleanupUpdaterBackupsJob' => $baseDir . '/lib/private/Repair/AddCleanupUpdaterBackupsJob.php',
1607+
'OC\\Repair\\AddMetadataGenerationJob' => $baseDir . '/lib/private/Repair/AddMetadataGenerationJob.php',
16061608
'OC\\Repair\\AddMetadataMigrationJob' => $baseDir . '/lib/private/Repair/AddMetadataMigrationJob.php',
16071609
'OC\\Repair\\AddRemoveOldTasksBackgroundJob' => $baseDir . '/lib/private/Repair/AddRemoveOldTasksBackgroundJob.php',
16081610
'OC\\Repair\\CleanTags' => $baseDir . '/lib/private/Repair/CleanTags.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
10351035
'OC\\Core\\BackgroundJobs\\BackgroundCleanupUpdaterBackupsJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/BackgroundCleanupUpdaterBackupsJob.php',
10361036
'OC\\Core\\BackgroundJobs\\CheckForUserCertificates' => __DIR__ . '/../../..' . '/core/BackgroundJobs/CheckForUserCertificates.php',
10371037
'OC\\Core\\BackgroundJobs\\CleanupLoginFlowV2' => __DIR__ . '/../../..' . '/core/BackgroundJobs/CleanupLoginFlowV2.php',
1038+
'OC\\Core\\BackgroundJobs\\GenerateMetadataJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/GenerateMetadataJob.php',
10381039
'OC\\Core\\BackgroundJobs\\LookupServerSendCheckBackgroundJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/LookupServerSendCheckBackgroundJob.php',
10391040
'OC\\Core\\BackgroundJobs\\MigrateMetadataJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/MigrateMetadataJob.php',
10401041
'OC\\Core\\Command\\App\\Disable' => __DIR__ . '/../../..' . '/core/Command/App/Disable.php',
@@ -1636,6 +1637,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
16361637
'OC\\RepairException' => __DIR__ . '/../../..' . '/lib/private/RepairException.php',
16371638
'OC\\Repair\\AddBruteForceCleanupJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddBruteForceCleanupJob.php',
16381639
'OC\\Repair\\AddCleanupUpdaterBackupsJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddCleanupUpdaterBackupsJob.php',
1640+
'OC\\Repair\\AddMetadataGenerationJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddMetadataGenerationJob.php',
16391641
'OC\\Repair\\AddMetadataMigrationJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddMetadataMigrationJob.php',
16401642
'OC\\Repair\\AddRemoveOldTasksBackgroundJob' => __DIR__ . '/../../..' . '/lib/private/Repair/AddRemoveOldTasksBackgroundJob.php',
16411643
'OC\\Repair\\CleanTags' => __DIR__ . '/../../..' . '/lib/private/Repair/CleanTags.php',

lib/private/Repair.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use OC\DB\ConnectionAdapter;
4747
use OC\Repair\AddBruteForceCleanupJob;
4848
use OC\Repair\AddCleanupUpdaterBackupsJob;
49+
use OC\Repair\AddMetadataGenerationJob;
4950
use OC\Repair\AddMetadataMigrationJob;
5051
use OC\Repair\CleanTags;
5152
use OC\Repair\ClearFrontendCaches;
@@ -213,6 +214,7 @@ public static function getRepairSteps(): array {
213214
\OCP\Server::get(AddMissingSecretJob::class),
214215
\OCP\Server::get(AddRemoveOldTasksBackgroundJob::class),
215216
\OCP\Server::get(AddMetadataMigrationJob::class),
217+
\OCP\Server::get(AddMetadataGenerationJob::class),
216218
];
217219
}
218220

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2018 Morris Jobke <hey@morrisjobke.de>
4+
*
5+
* @author Morris Jobke <hey@morrisjobke.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
namespace OC\Repair;
24+
25+
use OC\Core\BackgroundJobs\GenerateMetadataJob;
26+
use OCP\BackgroundJob\IJobList;
27+
use OCP\Migration\IOutput;
28+
use OCP\Migration\IRepairStep;
29+
30+
class AddMetadataGenerationJob implements IRepairStep {
31+
public function __construct(
32+
private IJobList $jobList,
33+
) {
34+
}
35+
36+
public function getName() {
37+
return 'Queue a job to generate metadata';
38+
}
39+
40+
public function run(IOutput $output) {
41+
$this->jobList->add(GenerateMetadataJob::class);
42+
}
43+
}

lib/private/Repair/AddMetadataMigrationJob.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22
/**
3-
* @copyright Copyright (c) 2018 Morris Jobke <hey@morrisjobke.de>
3+
* @copyright Copyright (c) 2023 Louis Chmn <louis@chmn.me>
44
*
5-
* @author Morris Jobke <hey@morrisjobke.de>
5+
* @author Louis Chmn <louis@chmn.me>
66
*
77
* @license GNU AGPL version 3 or any later version
88
*

0 commit comments

Comments
 (0)