Skip to content

Commit c89e3c2

Browse files
committed
feat: move file cache to a background job
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 084487b commit c89e3c2

6 files changed

Lines changed: 98 additions & 22 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OC\Core\BackgroundJobs;
10+
11+
use OC\Cache\File;
12+
use OCP\AppFramework\Utility\ITimeFactory;
13+
use OCP\BackgroundJob\TimedJob;
14+
use OCP\IAppConfig;
15+
use OCP\IUserManager;
16+
use Psr\Log\LoggerInterface;
17+
18+
class FileCacheGcJob extends TimedJob {
19+
public function __construct(
20+
ITimeFactory $time,
21+
private readonly LoggerInterface $logger,
22+
private readonly IAppConfig $appConfig,
23+
private readonly IUserManager $userManager,
24+
) {
25+
parent::__construct($time);
26+
27+
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
28+
$this->setInterval(24 * 60 * 60);
29+
}
30+
31+
protected function run(mixed $argument): void {
32+
$offset = $this->appConfig->getValueInt('core', 'files_gc_offset');
33+
34+
$users = $this->userManager->getSeenUsers($offset);
35+
$start = time();
36+
$count = 0;
37+
foreach ($users as $user) {
38+
$cache = new File();
39+
try {
40+
$cache->gc($user);
41+
} catch (\Exception $e) {
42+
$this->logger->warning('Exception when running cache gc.', [
43+
'app' => 'core',
44+
'exception' => $e,
45+
]);
46+
}
47+
$count++;
48+
$now = time();
49+
50+
// almost time for the next job run, stop early and save our location
51+
if ($now - $start > 23 * 60 * 60) {
52+
$this->appConfig->setValueInt('core', 'files_gc_offset', $offset + $count);
53+
return;
54+
}
55+
}
56+
$this->appConfig->setValueInt('core', 'files_gc_offset', 0);
57+
}
58+
}

lib/base.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -874,23 +874,6 @@ public static function registerCleanupHooks(\OC\SystemConfig $systemConfig): voi
874874
$throttler = Server::get(IThrottler::class);
875875
$throttler->resetDelay($request->getRemoteAddress(), 'login', ['user' => $uid]);
876876
}
877-
878-
try {
879-
$cache = new \OC\Cache\File();
880-
$cache->gc();
881-
} catch (\OC\ServerNotAvailableException $e) {
882-
// not a GC exception, pass it on
883-
throw $e;
884-
} catch (\OC\ForbiddenException $e) {
885-
// filesystem blocked for this request, ignore
886-
} catch (\Exception $e) {
887-
// a GC exception should not prevent users from using OC,
888-
// so log the exception
889-
Server::get(LoggerInterface::class)->warning('Exception when running cache gc.', [
890-
'app' => 'core',
891-
'exception' => $e,
892-
]);
893-
}
894877
});
895878
}
896879
}

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,7 @@
11961196
'OC\\Core\\BackgroundJobs\\BackgroundCleanupUpdaterBackupsJob' => $baseDir . '/core/BackgroundJobs/BackgroundCleanupUpdaterBackupsJob.php',
11971197
'OC\\Core\\BackgroundJobs\\CheckForUserCertificates' => $baseDir . '/core/BackgroundJobs/CheckForUserCertificates.php',
11981198
'OC\\Core\\BackgroundJobs\\CleanupLoginFlowV2' => $baseDir . '/core/BackgroundJobs/CleanupLoginFlowV2.php',
1199+
'OC\\Core\\BackgroundJobs\\FileCacheGcJob' => $baseDir . '/core/BackgroundJobs/FileCacheGcJob.php',
11991200
'OC\\Core\\BackgroundJobs\\GenerateMetadataJob' => $baseDir . '/core/BackgroundJobs/GenerateMetadataJob.php',
12001201
'OC\\Core\\BackgroundJobs\\LookupServerSendCheckBackgroundJob' => $baseDir . '/core/BackgroundJobs/LookupServerSendCheckBackgroundJob.php',
12011202
'OC\\Core\\Command\\App\\Disable' => $baseDir . '/core/Command/App/Disable.php',
@@ -1903,6 +1904,7 @@
19031904
'OC\\Repair\\NC29\\SanitizeAccountProperties' => $baseDir . '/lib/private/Repair/NC29/SanitizeAccountProperties.php',
19041905
'OC\\Repair\\NC29\\SanitizeAccountPropertiesJob' => $baseDir . '/lib/private/Repair/NC29/SanitizeAccountPropertiesJob.php',
19051906
'OC\\Repair\\NC30\\RemoveLegacyDatadirFile' => $baseDir . '/lib/private/Repair/NC30/RemoveLegacyDatadirFile.php',
1907+
'OC\\Repair\\NC32\\AddFileCacheGcBackgroundJob' => $baseDir . '/lib/private/Repair/NC32/AddFileCacheGcBackgroundJob.php',
19061908
'OC\\Repair\\OldGroupMembershipShares' => $baseDir . '/lib/private/Repair/OldGroupMembershipShares.php',
19071909
'OC\\Repair\\Owncloud\\CleanPreviews' => $baseDir . '/lib/private/Repair/Owncloud/CleanPreviews.php',
19081910
'OC\\Repair\\Owncloud\\CleanPreviewsBackgroundJob' => $baseDir . '/lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
12451245
'OC\\Core\\BackgroundJobs\\BackgroundCleanupUpdaterBackupsJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/BackgroundCleanupUpdaterBackupsJob.php',
12461246
'OC\\Core\\BackgroundJobs\\CheckForUserCertificates' => __DIR__ . '/../../..' . '/core/BackgroundJobs/CheckForUserCertificates.php',
12471247
'OC\\Core\\BackgroundJobs\\CleanupLoginFlowV2' => __DIR__ . '/../../..' . '/core/BackgroundJobs/CleanupLoginFlowV2.php',
1248+
'OC\\Core\\BackgroundJobs\\FileCacheGcJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/FileCacheGcJob.php',
12481249
'OC\\Core\\BackgroundJobs\\GenerateMetadataJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/GenerateMetadataJob.php',
12491250
'OC\\Core\\BackgroundJobs\\LookupServerSendCheckBackgroundJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/LookupServerSendCheckBackgroundJob.php',
12501251
'OC\\Core\\Command\\App\\Disable' => __DIR__ . '/../../..' . '/core/Command/App/Disable.php',
@@ -1952,6 +1953,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
19521953
'OC\\Repair\\NC29\\SanitizeAccountProperties' => __DIR__ . '/../../..' . '/lib/private/Repair/NC29/SanitizeAccountProperties.php',
19531954
'OC\\Repair\\NC29\\SanitizeAccountPropertiesJob' => __DIR__ . '/../../..' . '/lib/private/Repair/NC29/SanitizeAccountPropertiesJob.php',
19541955
'OC\\Repair\\NC30\\RemoveLegacyDatadirFile' => __DIR__ . '/../../..' . '/lib/private/Repair/NC30/RemoveLegacyDatadirFile.php',
1956+
'OC\\Repair\\NC32\\AddFileCacheGcBackgroundJob' => __DIR__ . '/../../..' . '/lib/private/Repair/NC32/AddFileCacheGcBackgroundJob.php',
19551957
'OC\\Repair\\OldGroupMembershipShares' => __DIR__ . '/../../..' . '/lib/private/Repair/OldGroupMembershipShares.php',
19561958
'OC\\Repair\\Owncloud\\CleanPreviews' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/CleanPreviews.php',
19571959
'OC\\Repair\\Owncloud\\CleanPreviewsBackgroundJob' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php',

lib/private/Cache/File.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ class File implements ICache {
2828
* @throws \OC\ForbiddenException
2929
* @throws \OC\User\NoUserException
3030
*/
31-
protected function getStorage() {
31+
protected function getStorage(?IUser $user = null): Folder {
3232
if ($this->storage !== null) {
3333
return $this->storage;
3434
}
35-
$session = Server::get(IUserSession::class);
36-
$user = $session->getUser();
35+
if (!$user) {
36+
$session = Server::get(IUserSession::class);
37+
$user = $session->getUser();
38+
}
3739
$rootFolder = Server::get(IRootFolder::class);
3840
if ($user) {
3941
$userId = $user->getUID();
@@ -156,8 +158,8 @@ public function clear($prefix = '') {
156158
* Runs GC
157159
* @throws \OC\ForbiddenException
158160
*/
159-
public function gc() {
160-
$storage = $this->getStorage();
161+
public function gc(?IUser $user = null) {
162+
$storage = $this->getStorage($user);
161163
// extra hour safety, in case of stray part chunks that take longer to write,
162164
// because touch() is only called after the chunk was finished
163165

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
namespace OC\Repair\NC32;
10+
11+
use OC\Core\BackgroundJobs\FileCacheGcJob;
12+
use OCP\BackgroundJob\IJobList;
13+
use OCP\Migration\IOutput;
14+
use OCP\Migration\IRepairStep;
15+
16+
class AddFileCacheGcBackgroundJob implements IRepairStep {
17+
public function __construct(
18+
private readonly IJobList $jobList,
19+
) {
20+
}
21+
22+
public function getName(): string {
23+
return 'Add background job to cleanup file cache';
24+
}
25+
26+
public function run(IOutput $output) {
27+
$this->jobList->add(FileCacheGcJob::class);
28+
}
29+
}

0 commit comments

Comments
 (0)