RemoveOldTasksBackgroundJob.php 983 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\TextProcessing;
  8. use OC\TextProcessing\Db\TaskMapper;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\BackgroundJob\TimedJob;
  11. use OCP\DB\Exception;
  12. use Psr\Log\LoggerInterface;
  13. class RemoveOldTasksBackgroundJob extends TimedJob {
  14. public const MAX_TASK_AGE_SECONDS = 60 * 50 * 24 * 7; // 1 week
  15. public function __construct(
  16. ITimeFactory $timeFactory,
  17. private TaskMapper $taskMapper,
  18. private LoggerInterface $logger,
  19. ) {
  20. parent::__construct($timeFactory);
  21. $this->setInterval(60 * 60 * 24);
  22. }
  23. /**
  24. * @param mixed $argument
  25. * @inheritDoc
  26. */
  27. protected function run($argument) {
  28. try {
  29. $this->taskMapper->deleteOlderThan(self::MAX_TASK_AGE_SECONDS);
  30. } catch (Exception $e) {
  31. $this->logger->warning('Failed to delete stale language model tasks', ['exception' => $e]);
  32. }
  33. }
  34. }