aboutsummaryrefslogtreecommitdiffstats
path: root/core/BackgroundJobs/FileCacheGcJob.php
blob: e0c8a1d78814fa9f6ff776ed71f972bb257ff4cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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);
	}
}