|
|
|
|
@ -9,16 +9,25 @@ declare(strict_types=1);
|
|
|
|
|
namespace OC\Core\Command\TaskProcessing;
|
|
|
|
|
|
|
|
|
|
use OC\Core\Command\Base;
|
|
|
|
|
use OC\TaskProcessing\Db\TaskMapper;
|
|
|
|
|
use OC\TaskProcessing\Manager;
|
|
|
|
|
use OCP\Files\AppData\IAppDataFactory;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
|
|
class Cleanup extends Base {
|
|
|
|
|
private \OCP\Files\IAppData $appData;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
protected Manager $taskProcessingManager,
|
|
|
|
|
private TaskMapper $taskMapper,
|
|
|
|
|
private LoggerInterface $logger,
|
|
|
|
|
IAppDataFactory $appDataFactory,
|
|
|
|
|
) {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->appData = $appDataFactory->get('core');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function configure() {
|
|
|
|
|
@ -37,20 +46,48 @@ class Cleanup extends Base {
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
|
|
|
|
$maxAgeSeconds = $input->getArgument('maxAgeSeconds') ?? Manager::MAX_TASK_AGE_SECONDS;
|
|
|
|
|
$output->writeln('<comment>Cleanup up tasks older than ' . $maxAgeSeconds . ' seconds and the related output files</comment>');
|
|
|
|
|
$cleanupResult = $this->taskProcessingManager->cleanupOldTasks($maxAgeSeconds);
|
|
|
|
|
foreach ($cleanupResult as $entry) {
|
|
|
|
|
if (isset($entry['task_id'], $entry['file_id'], $entry['file_name'])) {
|
|
|
|
|
$output->writeln("<info>\t - " . 'Deleted appData/core/TaskProcessing/' . $entry['file_name'] . ' (fileId: ' . $entry['file_id'] . ', taskId: ' . $entry['task_id'] . ')</info>');
|
|
|
|
|
} elseif (isset($entry['directory_name'])) {
|
|
|
|
|
$output->writeln("<info>\t - " . 'Deleted appData/core/' . $entry['directory_name'] . '/' . $entry['file_name'] . '</info>');
|
|
|
|
|
} elseif (isset($entry['deleted_task_count'])) {
|
|
|
|
|
$output->writeln("<comment>\t - " . 'Deleted ' . $entry['deleted_task_count'] . ' tasks from the database</comment>');
|
|
|
|
|
} elseif (isset($entry['deleted_task_id_list'])) {
|
|
|
|
|
foreach ($entry['deleted_task_id_list'] as $taskId) {
|
|
|
|
|
$output->writeln("<info>\t - " . 'Deleted task ' . $taskId . ' from the database</info>');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$taskIdsToCleanup = [];
|
|
|
|
|
try {
|
|
|
|
|
$fileCleanupGenerator = $this->taskProcessingManager->cleanupTaskProcessingTaskFiles($maxAgeSeconds);
|
|
|
|
|
foreach ($fileCleanupGenerator as $cleanedUpEntry) {
|
|
|
|
|
$output->writeln(
|
|
|
|
|
"<info>\t - " . 'Deleted appData/core/TaskProcessing/' . $cleanedUpEntry['file_name']
|
|
|
|
|
. ' (fileId: ' . $cleanedUpEntry['file_id'] . ', taskId: ' . $cleanedUpEntry['task_id'] . ')</info>'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$taskIdsToCleanup = $fileCleanupGenerator->getReturn();
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->logger->warning('Failed to delete stale task processing tasks files', ['exception' => $e]);
|
|
|
|
|
$output->writeln('<warning>Failed to delete stale task processing tasks files</warning>');
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$deletedTaskCount = $this->taskMapper->deleteOlderThan($maxAgeSeconds);
|
|
|
|
|
foreach ($taskIdsToCleanup as $taskId) {
|
|
|
|
|
$output->writeln("<info>\t - " . 'Deleted task ' . $taskId . ' from the database</info>');
|
|
|
|
|
}
|
|
|
|
|
$output->writeln("<comment>\t - " . 'Deleted ' . $deletedTaskCount . ' tasks from the database</comment>');
|
|
|
|
|
} catch (\OCP\DB\Exception $e) {
|
|
|
|
|
$this->logger->warning('Failed to delete stale task processing tasks', ['exception' => $e]);
|
|
|
|
|
$output->writeln('<warning>Failed to delete stale task processing tasks</warning>');
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$textToImageDeletedFileNames = $this->taskProcessingManager->clearFilesOlderThan($this->appData->getFolder('text2image'), $maxAgeSeconds);
|
|
|
|
|
foreach ($textToImageDeletedFileNames as $entry) {
|
|
|
|
|
$output->writeln("<info>\t - " . 'Deleted appData/core/text2image/' . $entry . '</info>');
|
|
|
|
|
}
|
|
|
|
|
} catch (\OCP\Files\NotFoundException $e) {
|
|
|
|
|
// noop
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$audioToTextDeletedFileNames = $this->taskProcessingManager->clearFilesOlderThan($this->appData->getFolder('audio2text'), $maxAgeSeconds);
|
|
|
|
|
foreach ($audioToTextDeletedFileNames as $entry) {
|
|
|
|
|
$output->writeln("<info>\t - " . 'Deleted appData/core/audio2text/' . $entry . '</info>');
|
|
|
|
|
}
|
|
|
|
|
} catch (\OCP\Files\NotFoundException $e) {
|
|
|
|
|
// noop
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|