aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2025-04-07 18:12:42 +0200
committerRobin Appelman <robin@icewind.nl>2025-04-07 19:35:41 +0200
commitc89e3c2f74d6f0bf253dddc18a4a4d305821cbe7 (patch)
tree77ae38024de782c713f8e0f9ff2a45f64f75b9d5
parent084487bdd5ac052fc831509780ce2ffe5517eb41 (diff)
downloadnextcloud-server-files-cache-node.tar.gz
nextcloud-server-files-cache-node.zip
feat: move file cache to a background jobfiles-cache-node
Signed-off-by: Robin Appelman <robin@icewind.nl>
-rw-r--r--core/BackgroundJobs/FileCacheGcJob.php58
-rw-r--r--lib/base.php17
-rw-r--r--lib/composer/composer/autoload_classmap.php2
-rw-r--r--lib/composer/composer/autoload_static.php2
-rw-r--r--lib/private/Cache/File.php12
-rw-r--r--lib/private/Repair/NC32/AddFileCacheGcBackgroundJob.php29
6 files changed, 98 insertions, 22 deletions
diff --git a/core/BackgroundJobs/FileCacheGcJob.php b/core/BackgroundJobs/FileCacheGcJob.php
new file mode 100644
index 00000000000..e0c8a1d7881
--- /dev/null
+++ b/core/BackgroundJobs/FileCacheGcJob.php
@@ -0,0 +1,58 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OC\Core\BackgroundJobs;
+
+use OC\Cache\File;
+use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\BackgroundJob\TimedJob;
+use OCP\IAppConfig;
+use OCP\IUserManager;
+use Psr\Log\LoggerInterface;
+
+class FileCacheGcJob extends TimedJob {
+ public function __construct(
+ ITimeFactory $time,
+ private readonly LoggerInterface $logger,
+ private readonly IAppConfig $appConfig,
+ private readonly IUserManager $userManager,
+ ) {
+ parent::__construct($time);
+
+ $this->setTimeSensitivity(self::TIME_INSENSITIVE);
+ $this->setInterval(24 * 60 * 60);
+ }
+
+ protected function run(mixed $argument): void {
+ $offset = $this->appConfig->getValueInt('core', 'files_gc_offset');
+
+ $users = $this->userManager->getSeenUsers($offset);
+ $start = time();
+ $count = 0;
+ foreach ($users as $user) {
+ $cache = new File();
+ try {
+ $cache->gc($user);
+ } catch (\Exception $e) {
+ $this->logger->warning('Exception when running cache gc.', [
+ 'app' => 'core',
+ 'exception' => $e,
+ ]);
+ }
+ $count++;
+ $now = time();
+
+ // almost time for the next job run, stop early and save our location
+ if ($now - $start > 23 * 60 * 60) {
+ $this->appConfig->setValueInt('core', 'files_gc_offset', $offset + $count);
+ return;
+ }
+ }
+ $this->appConfig->setValueInt('core', 'files_gc_offset', 0);
+ }
+}
diff --git a/lib/base.php b/lib/base.php
index bda6b6e01a1..46a58c56e8a 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -874,23 +874,6 @@ class OC {
$throttler = Server::get(IThrottler::class);
$throttler->resetDelay($request->getRemoteAddress(), 'login', ['user' => $uid]);
}
-
- try {
- $cache = new \OC\Cache\File();
- $cache->gc();
- } catch (\OC\ServerNotAvailableException $e) {
- // not a GC exception, pass it on
- throw $e;
- } catch (\OC\ForbiddenException $e) {
- // filesystem blocked for this request, ignore
- } catch (\Exception $e) {
- // a GC exception should not prevent users from using OC,
- // so log the exception
- Server::get(LoggerInterface::class)->warning('Exception when running cache gc.', [
- 'app' => 'core',
- 'exception' => $e,
- ]);
- }
});
}
}
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index 52e3075e413..eab0432a0cf 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -1196,6 +1196,7 @@ return array(
'OC\\Core\\BackgroundJobs\\BackgroundCleanupUpdaterBackupsJob' => $baseDir . '/core/BackgroundJobs/BackgroundCleanupUpdaterBackupsJob.php',
'OC\\Core\\BackgroundJobs\\CheckForUserCertificates' => $baseDir . '/core/BackgroundJobs/CheckForUserCertificates.php',
'OC\\Core\\BackgroundJobs\\CleanupLoginFlowV2' => $baseDir . '/core/BackgroundJobs/CleanupLoginFlowV2.php',
+ 'OC\\Core\\BackgroundJobs\\FileCacheGcJob' => $baseDir . '/core/BackgroundJobs/FileCacheGcJob.php',
'OC\\Core\\BackgroundJobs\\GenerateMetadataJob' => $baseDir . '/core/BackgroundJobs/GenerateMetadataJob.php',
'OC\\Core\\BackgroundJobs\\LookupServerSendCheckBackgroundJob' => $baseDir . '/core/BackgroundJobs/LookupServerSendCheckBackgroundJob.php',
'OC\\Core\\Command\\App\\Disable' => $baseDir . '/core/Command/App/Disable.php',
@@ -1903,6 +1904,7 @@ return array(
'OC\\Repair\\NC29\\SanitizeAccountProperties' => $baseDir . '/lib/private/Repair/NC29/SanitizeAccountProperties.php',
'OC\\Repair\\NC29\\SanitizeAccountPropertiesJob' => $baseDir . '/lib/private/Repair/NC29/SanitizeAccountPropertiesJob.php',
'OC\\Repair\\NC30\\RemoveLegacyDatadirFile' => $baseDir . '/lib/private/Repair/NC30/RemoveLegacyDatadirFile.php',
+ 'OC\\Repair\\NC32\\AddFileCacheGcBackgroundJob' => $baseDir . '/lib/private/Repair/NC32/AddFileCacheGcBackgroundJob.php',
'OC\\Repair\\OldGroupMembershipShares' => $baseDir . '/lib/private/Repair/OldGroupMembershipShares.php',
'OC\\Repair\\Owncloud\\CleanPreviews' => $baseDir . '/lib/private/Repair/Owncloud/CleanPreviews.php',
'OC\\Repair\\Owncloud\\CleanPreviewsBackgroundJob' => $baseDir . '/lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index e98bc3e1aaa..0423d777479 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -1245,6 +1245,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\BackgroundJobs\\BackgroundCleanupUpdaterBackupsJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/BackgroundCleanupUpdaterBackupsJob.php',
'OC\\Core\\BackgroundJobs\\CheckForUserCertificates' => __DIR__ . '/../../..' . '/core/BackgroundJobs/CheckForUserCertificates.php',
'OC\\Core\\BackgroundJobs\\CleanupLoginFlowV2' => __DIR__ . '/../../..' . '/core/BackgroundJobs/CleanupLoginFlowV2.php',
+ 'OC\\Core\\BackgroundJobs\\FileCacheGcJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/FileCacheGcJob.php',
'OC\\Core\\BackgroundJobs\\GenerateMetadataJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/GenerateMetadataJob.php',
'OC\\Core\\BackgroundJobs\\LookupServerSendCheckBackgroundJob' => __DIR__ . '/../../..' . '/core/BackgroundJobs/LookupServerSendCheckBackgroundJob.php',
'OC\\Core\\Command\\App\\Disable' => __DIR__ . '/../../..' . '/core/Command/App/Disable.php',
@@ -1952,6 +1953,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Repair\\NC29\\SanitizeAccountProperties' => __DIR__ . '/../../..' . '/lib/private/Repair/NC29/SanitizeAccountProperties.php',
'OC\\Repair\\NC29\\SanitizeAccountPropertiesJob' => __DIR__ . '/../../..' . '/lib/private/Repair/NC29/SanitizeAccountPropertiesJob.php',
'OC\\Repair\\NC30\\RemoveLegacyDatadirFile' => __DIR__ . '/../../..' . '/lib/private/Repair/NC30/RemoveLegacyDatadirFile.php',
+ 'OC\\Repair\\NC32\\AddFileCacheGcBackgroundJob' => __DIR__ . '/../../..' . '/lib/private/Repair/NC32/AddFileCacheGcBackgroundJob.php',
'OC\\Repair\\OldGroupMembershipShares' => __DIR__ . '/../../..' . '/lib/private/Repair/OldGroupMembershipShares.php',
'OC\\Repair\\Owncloud\\CleanPreviews' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/CleanPreviews.php',
'OC\\Repair\\Owncloud\\CleanPreviewsBackgroundJob' => __DIR__ . '/../../..' . '/lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php',
diff --git a/lib/private/Cache/File.php b/lib/private/Cache/File.php
index 99b14e92787..e8c436636ab 100644
--- a/lib/private/Cache/File.php
+++ b/lib/private/Cache/File.php
@@ -28,12 +28,14 @@ class File implements ICache {
* @throws \OC\ForbiddenException
* @throws \OC\User\NoUserException
*/
- protected function getStorage() {
+ protected function getStorage(?IUser $user = null): Folder {
if ($this->storage !== null) {
return $this->storage;
}
- $session = Server::get(IUserSession::class);
- $user = $session->getUser();
+ if (!$user) {
+ $session = Server::get(IUserSession::class);
+ $user = $session->getUser();
+ }
$rootFolder = Server::get(IRootFolder::class);
if ($user) {
$userId = $user->getUID();
@@ -156,8 +158,8 @@ class File implements ICache {
* Runs GC
* @throws \OC\ForbiddenException
*/
- public function gc() {
- $storage = $this->getStorage();
+ public function gc(?IUser $user = null) {
+ $storage = $this->getStorage($user);
// extra hour safety, in case of stray part chunks that take longer to write,
// because touch() is only called after the chunk was finished
diff --git a/lib/private/Repair/NC32/AddFileCacheGcBackgroundJob.php b/lib/private/Repair/NC32/AddFileCacheGcBackgroundJob.php
new file mode 100644
index 00000000000..b6b0af1bf32
--- /dev/null
+++ b/lib/private/Repair/NC32/AddFileCacheGcBackgroundJob.php
@@ -0,0 +1,29 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OC\Repair\NC32;
+
+use OC\Core\BackgroundJobs\FileCacheGcJob;
+use OCP\BackgroundJob\IJobList;
+use OCP\Migration\IOutput;
+use OCP\Migration\IRepairStep;
+
+class AddFileCacheGcBackgroundJob implements IRepairStep {
+ public function __construct(
+ private readonly IJobList $jobList,
+ ) {
+ }
+
+ public function getName(): string {
+ return 'Add background job to cleanup file cache';
+ }
+
+ public function run(IOutput $output) {
+ $this->jobList->add(FileCacheGcJob::class);
+ }
+}