|
| 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 | +} |
0 commit comments