PruneOutdatedSyncTokensJob.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\BackgroundJob;
  8. use OCA\DAV\AppInfo\Application;
  9. use OCA\DAV\CalDAV\CalDavBackend;
  10. use OCA\DAV\CardDAV\CardDavBackend;
  11. use OCP\AppFramework\Utility\ITimeFactory;
  12. use OCP\BackgroundJob\TimedJob;
  13. use OCP\IConfig;
  14. use Psr\Log\LoggerInterface;
  15. class PruneOutdatedSyncTokensJob extends TimedJob {
  16. public function __construct(
  17. ITimeFactory $timeFactory,
  18. private CalDavBackend $calDavBackend,
  19. private CardDavBackend $cardDavBackend,
  20. private IConfig $config,
  21. private LoggerInterface $logger,
  22. ) {
  23. parent::__construct($timeFactory);
  24. $this->setInterval(60 * 60 * 24); // One day
  25. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  26. }
  27. public function run($argument) {
  28. $limit = max(1, (int)$this->config->getAppValue(Application::APP_ID, 'totalNumberOfSyncTokensToKeep', '10000'));
  29. $retention = max(7, (int)$this->config->getAppValue(Application::APP_ID, 'syncTokensRetentionDays', '60')) * 24 * 3600;
  30. $prunedCalendarSyncTokens = $this->calDavBackend->pruneOutdatedSyncTokens($limit, $retention);
  31. $prunedAddressBookSyncTokens = $this->cardDavBackend->pruneOutdatedSyncTokens($limit, $retention);
  32. $this->logger->info('Pruned {calendarSyncTokensNumber} calendar sync tokens and {addressBooksSyncTokensNumber} address book sync tokens', [
  33. 'calendarSyncTokensNumber' => $prunedCalendarSyncTokens,
  34. 'addressBooksSyncTokensNumber' => $prunedAddressBookSyncTokens
  35. ]);
  36. }
  37. }