RemoveOldTasksBackgroundJob.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
  5. *
  6. * @author Marcel Klehr <mklehr@gmx.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace OC\TextProcessing;
  24. use OC\TextProcessing\Db\TaskMapper;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\BackgroundJob\TimedJob;
  27. use OCP\DB\Exception;
  28. use Psr\Log\LoggerInterface;
  29. class RemoveOldTasksBackgroundJob extends TimedJob {
  30. public const MAX_TASK_AGE_SECONDS = 60 * 50 * 24 * 7; // 1 week
  31. public function __construct(
  32. ITimeFactory $timeFactory,
  33. private TaskMapper $taskMapper,
  34. private LoggerInterface $logger,
  35. ) {
  36. parent::__construct($timeFactory);
  37. $this->setInterval(60 * 60 * 24);
  38. }
  39. /**
  40. * @param mixed $argument
  41. * @inheritDoc
  42. */
  43. protected function run($argument) {
  44. try {
  45. $this->taskMapper->deleteOlderThan(self::MAX_TASK_AGE_SECONDS);
  46. } catch (Exception $e) {
  47. $this->logger->warning('Failed to delete stale language model tasks', ['exception' => $e]);
  48. }
  49. }
  50. }