You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoveOldTasksBackgroundJob.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\TextToImage;
  24. use OC\TextToImage\Db\TaskMapper;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\BackgroundJob\TimedJob;
  27. use OCP\DB\Exception;
  28. use OCP\Files\AppData\IAppDataFactory;
  29. use OCP\Files\IAppData;
  30. use OCP\Files\NotFoundException;
  31. use OCP\Files\NotPermittedException;
  32. use Psr\Log\LoggerInterface;
  33. class RemoveOldTasksBackgroundJob extends TimedJob {
  34. public const MAX_TASK_AGE_SECONDS = 60 * 50 * 24 * 7; // 1 week
  35. private IAppData $appData;
  36. public function __construct(
  37. ITimeFactory $timeFactory,
  38. private TaskMapper $taskMapper,
  39. private LoggerInterface $logger,
  40. IAppDataFactory $appDataFactory,
  41. ) {
  42. parent::__construct($timeFactory);
  43. $this->appData = $appDataFactory->get('core');
  44. $this->setInterval(60 * 60 * 24);
  45. }
  46. /**
  47. * @param mixed $argument
  48. * @inheritDoc
  49. */
  50. protected function run($argument) {
  51. try {
  52. $deletedTasks = $this->taskMapper->deleteOlderThan(self::MAX_TASK_AGE_SECONDS);
  53. $folder = $this->appData->getFolder('text2image');
  54. foreach ($deletedTasks as $deletedTask) {
  55. try {
  56. $folder->getFolder((string)$deletedTask->getId())->delete();
  57. } catch (NotFoundException) {
  58. // noop
  59. } catch (NotPermittedException $e) {
  60. $this->logger->warning('Failed to delete stale text to image task files', ['exception' => $e]);
  61. }
  62. }
  63. } catch (Exception $e) {
  64. $this->logger->warning('Failed to delete stale text to image tasks', ['exception' => $e]);
  65. } catch(NotFoundException) {
  66. // noop
  67. }
  68. }
  69. }