RemoveOldTasksBackgroundJob.php 1.0 KB

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 * 60 * 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. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  23. }
  24. /**
  25. * @param mixed $argument
  26. * @inheritDoc
  27. */
  28. protected function run($argument) {
  29. try {
  30. $this->taskMapper->deleteOlderThan(self::MAX_TASK_AGE_SECONDS);
  31. } catch (Exception $e) {
  32. $this->logger->warning('Failed to delete stale language model tasks', ['exception' => $e]);
  33. }
  34. }
  35. }