|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OC\Core\Command\TaskProcessing; |
| 10 | + |
| 11 | +use OC\Core\Command\Base; |
| 12 | +use OC\TaskProcessing\Db\TaskMapper; |
| 13 | +use OC\TaskProcessing\Manager; |
| 14 | +use OCP\Files\AppData\IAppDataFactory; |
| 15 | +use Psr\Log\LoggerInterface; |
| 16 | +use Symfony\Component\Console\Input\InputArgument; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Output\OutputInterface; |
| 19 | + |
| 20 | +class Cleanup extends Base { |
| 21 | + private \OCP\Files\IAppData $appData; |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + protected Manager $taskProcessingManager, |
| 25 | + private TaskMapper $taskMapper, |
| 26 | + private LoggerInterface $logger, |
| 27 | + IAppDataFactory $appDataFactory, |
| 28 | + ) { |
| 29 | + parent::__construct(); |
| 30 | + $this->appData = $appDataFactory->get('core'); |
| 31 | + } |
| 32 | + |
| 33 | + protected function configure() { |
| 34 | + $this |
| 35 | + ->setName('taskprocessing:task:cleanup') |
| 36 | + ->setDescription('cleanup old tasks') |
| 37 | + ->addArgument( |
| 38 | + 'maxAgeSeconds', |
| 39 | + InputArgument::OPTIONAL, |
| 40 | + // default is not defined as an argument default value because we want to show a nice "4 months" value |
| 41 | + 'delete tasks that are older than this number of seconds, defaults to ' . Manager::MAX_TASK_AGE_SECONDS . ' (4 months)', |
| 42 | + ); |
| 43 | + parent::configure(); |
| 44 | + } |
| 45 | + |
| 46 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 47 | + $maxAgeSeconds = $input->getArgument('maxAgeSeconds') ?? Manager::MAX_TASK_AGE_SECONDS; |
| 48 | + $output->writeln('<comment>Cleanup up tasks older than ' . $maxAgeSeconds . ' seconds and the related output files</comment>'); |
| 49 | + |
| 50 | + $taskIdsToCleanup = []; |
| 51 | + try { |
| 52 | + $fileCleanupGenerator = $this->taskProcessingManager->cleanupTaskProcessingTaskFiles($maxAgeSeconds); |
| 53 | + foreach ($fileCleanupGenerator as $cleanedUpEntry) { |
| 54 | + $output->writeln( |
| 55 | + "<info>\t - " . 'Deleted appData/core/TaskProcessing/' . $cleanedUpEntry['file_name'] |
| 56 | + . ' (fileId: ' . $cleanedUpEntry['file_id'] . ', taskId: ' . $cleanedUpEntry['task_id'] . ')</info>' |
| 57 | + ); |
| 58 | + } |
| 59 | + $taskIdsToCleanup = $fileCleanupGenerator->getReturn(); |
| 60 | + } catch (\Exception $e) { |
| 61 | + $this->logger->warning('Failed to delete stale task processing tasks files', ['exception' => $e]); |
| 62 | + $output->writeln('<warning>Failed to delete stale task processing tasks files</warning>'); |
| 63 | + } |
| 64 | + try { |
| 65 | + $deletedTaskCount = $this->taskMapper->deleteOlderThan($maxAgeSeconds); |
| 66 | + foreach ($taskIdsToCleanup as $taskId) { |
| 67 | + $output->writeln("<info>\t - " . 'Deleted task ' . $taskId . ' from the database</info>'); |
| 68 | + } |
| 69 | + $output->writeln("<comment>\t - " . 'Deleted ' . $deletedTaskCount . ' tasks from the database</comment>'); |
| 70 | + } catch (\OCP\DB\Exception $e) { |
| 71 | + $this->logger->warning('Failed to delete stale task processing tasks', ['exception' => $e]); |
| 72 | + $output->writeln('<warning>Failed to delete stale task processing tasks</warning>'); |
| 73 | + } |
| 74 | + try { |
| 75 | + $textToImageDeletedFileNames = $this->taskProcessingManager->clearFilesOlderThan($this->appData->getFolder('text2image'), $maxAgeSeconds); |
| 76 | + foreach ($textToImageDeletedFileNames as $entry) { |
| 77 | + $output->writeln("<info>\t - " . 'Deleted appData/core/text2image/' . $entry . '</info>'); |
| 78 | + } |
| 79 | + } catch (\OCP\Files\NotFoundException $e) { |
| 80 | + // noop |
| 81 | + } |
| 82 | + try { |
| 83 | + $audioToTextDeletedFileNames = $this->taskProcessingManager->clearFilesOlderThan($this->appData->getFolder('audio2text'), $maxAgeSeconds); |
| 84 | + foreach ($audioToTextDeletedFileNames as $entry) { |
| 85 | + $output->writeln("<info>\t - " . 'Deleted appData/core/audio2text/' . $entry . '</info>'); |
| 86 | + } |
| 87 | + } catch (\OCP\Files\NotFoundException $e) { |
| 88 | + // noop |
| 89 | + } |
| 90 | + |
| 91 | + return 0; |
| 92 | + } |
| 93 | +} |
0 commit comments