|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * @copyright Copyright (c) 2024, Maxence Lange <maxence@artificial-owl.com> |
| 6 | + * |
| 7 | + * @author Maxence Lange <maxence@artificial-owl.com> |
| 8 | + * |
| 9 | + * @license GNU AGPL version 3 or any later version |
| 10 | + * |
| 11 | + * This program is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU Affero General Public License as |
| 13 | + * published by the Free Software Foundation, either version 3 of the |
| 14 | + * License, or (at your option) any later version. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Affero General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Affero General Public License |
| 22 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +namespace OC\Core\Command\Background; |
| 27 | + |
| 28 | +use OC\Core\Command\Base; |
| 29 | +use OCP\BackgroundJob\IJobList; |
| 30 | +use Symfony\Component\Console\Input\InputArgument; |
| 31 | +use Symfony\Component\Console\Input\InputInterface; |
| 32 | +use Symfony\Component\Console\Output\OutputInterface; |
| 33 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 34 | + |
| 35 | +class Delete extends Base { |
| 36 | + public function __construct( |
| 37 | + protected IJobList $jobList, |
| 38 | + ) { |
| 39 | + parent::__construct(); |
| 40 | + } |
| 41 | + |
| 42 | + protected function configure(): void { |
| 43 | + $this |
| 44 | + ->setName('background-job:delete') |
| 45 | + ->setDescription('Remove a background job from database') |
| 46 | + ->addArgument( |
| 47 | + 'job-id', |
| 48 | + InputArgument::REQUIRED, |
| 49 | + 'The ID of the job in the database' |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 54 | + $jobId = (int) $input->getArgument('job-id'); |
| 55 | + |
| 56 | + $job = $this->jobList->getById($jobId); |
| 57 | + if ($job === null) { |
| 58 | + $output->writeln('<error>Job with ID ' . $jobId . ' could not be found in the database</error>'); |
| 59 | + return 1; |
| 60 | + } |
| 61 | + |
| 62 | + $output->writeln('Job class: ' . get_class($job)); |
| 63 | + $output->writeln('Arguments: ' . json_encode($job->getArgument())); |
| 64 | + $output->writeln(''); |
| 65 | + |
| 66 | + $question = new ConfirmationQuestion( |
| 67 | + '<comment>Do you really want to delete this background job ? It could create some misbehaviours in Nextcloud.</comment> (y/N) ', false, |
| 68 | + '/^(y|Y)/i' |
| 69 | + ); |
| 70 | + |
| 71 | + $helper = $this->getHelper('question'); |
| 72 | + if (!$helper->ask($input, $output, $question)) { |
| 73 | + $output->writeln('aborted.'); |
| 74 | + return 0; |
| 75 | + } |
| 76 | + |
| 77 | + $this->jobList->remove($job, $job->getArgument()); |
| 78 | + return 0; |
| 79 | + } |
| 80 | +} |
0 commit comments