PruneOutdatedSyncTokensJob.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. private IConfig $config;
  17. private LoggerInterface $logger;
  18. private CardDavBackend $cardDavBackend;
  19. private CalDavBackend $calDavBackend;
  20. public function __construct(ITimeFactory $timeFactory, CalDavBackend $calDavBackend, CardDavBackend $cardDavBackend, IConfig $config, LoggerInterface $logger) {
  21. parent::__construct($timeFactory);
  22. $this->calDavBackend = $calDavBackend;
  23. $this->cardDavBackend = $cardDavBackend;
  24. $this->config = $config;
  25. $this->logger = $logger;
  26. $this->setInterval(60 * 60 * 24); // One day
  27. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  28. }
  29. public function run($argument) {
  30. $limit = max(1, (int) $this->config->getAppValue(Application::APP_ID, 'totalNumberOfSyncTokensToKeep', '10000'));
  31. $retention = max(7, (int) $this->config->getAppValue(Application::APP_ID, 'syncTokensRetentionDays', '60')) * 24 * 3600;
  32. $prunedCalendarSyncTokens = $this->calDavBackend->pruneOutdatedSyncTokens($limit, $retention);
  33. $prunedAddressBookSyncTokens = $this->cardDavBackend->pruneOutdatedSyncTokens($limit, $retention);
  34. $this->logger->info('Pruned {calendarSyncTokensNumber} calendar sync tokens and {addressBooksSyncTokensNumber} address book sync tokens', [
  35. 'calendarSyncTokensNumber' => $prunedCalendarSyncTokens,
  36. 'addressBooksSyncTokensNumber' => $prunedAddressBookSyncTokens
  37. ]);
  38. }
  39. }