appData = $appDataFactory->get('core'); } protected function configure() { $this ->setName('taskprocessing:task:cleanup') ->setDescription('cleanup old tasks') ->addArgument( 'maxAgeSeconds', InputArgument::OPTIONAL, // default is not defined as an argument default value because we want to show a nice "4 months" value 'delete tasks that are older than this number of seconds, defaults to ' . Manager::MAX_TASK_AGE_SECONDS . ' (4 months)', ); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output): int { $maxAgeSeconds = $input->getArgument('maxAgeSeconds') ?? Manager::MAX_TASK_AGE_SECONDS; $output->writeln('Cleanup up tasks older than ' . $maxAgeSeconds . ' seconds and the related output files'); $taskIdsToCleanup = []; try { $fileCleanupGenerator = $this->taskProcessingManager->cleanupTaskProcessingTaskFiles($maxAgeSeconds); foreach ($fileCleanupGenerator as $cleanedUpEntry) { $output->writeln( "\t - " . 'Deleted appData/core/TaskProcessing/' . $cleanedUpEntry['file_name'] . ' (fileId: ' . $cleanedUpEntry['file_id'] . ', taskId: ' . $cleanedUpEntry['task_id'] . ')' ); } $taskIdsToCleanup = $fileCleanupGenerator->getReturn(); } catch (\Exception $e) { $this->logger->warning('Failed to delete stale task processing tasks files', ['exception' => $e]); $output->writeln('Failed to delete stale task processing tasks files'); } try { $deletedTaskCount = $this->taskMapper->deleteOlderThan($maxAgeSeconds); foreach ($taskIdsToCleanup as $taskId) { $output->writeln("\t - " . 'Deleted task ' . $taskId . ' from the database'); } $output->writeln("\t - " . 'Deleted ' . $deletedTaskCount . ' tasks from the database'); } catch (\OCP\DB\Exception $e) { $this->logger->warning('Failed to delete stale task processing tasks', ['exception' => $e]); $output->writeln('Failed to delete stale task processing tasks'); } try { $textToImageDeletedFileNames = $this->taskProcessingManager->clearFilesOlderThan($this->appData->getFolder('text2image'), $maxAgeSeconds); foreach ($textToImageDeletedFileNames as $entry) { $output->writeln("\t - " . 'Deleted appData/core/text2image/' . $entry . ''); } } catch (NotFoundException $e) { // noop } try { $audioToTextDeletedFileNames = $this->taskProcessingManager->clearFilesOlderThan($this->appData->getFolder('audio2text'), $maxAgeSeconds); foreach ($audioToTextDeletedFileNames as $entry) { $output->writeln("\t - " . 'Deleted appData/core/audio2text/' . $entry . ''); } } catch (NotFoundException $e) { // noop } return 0; } }