Delete.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Command\Background;
  8. use OC\Core\Command\Base;
  9. use OCP\BackgroundJob\IJobList;
  10. use Symfony\Component\Console\Input\InputArgument;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Symfony\Component\Console\Question\ConfirmationQuestion;
  14. class Delete extends Base {
  15. public function __construct(
  16. protected IJobList $jobList,
  17. ) {
  18. parent::__construct();
  19. }
  20. protected function configure(): void {
  21. $this
  22. ->setName('background-job:delete')
  23. ->setDescription('Remove a background job from database')
  24. ->addArgument(
  25. 'job-id',
  26. InputArgument::REQUIRED,
  27. 'The ID of the job in the database'
  28. );
  29. }
  30. protected function execute(InputInterface $input, OutputInterface $output): int {
  31. $jobId = (int) $input->getArgument('job-id');
  32. $job = $this->jobList->getById($jobId);
  33. if ($job === null) {
  34. $output->writeln('<error>Job with ID ' . $jobId . ' could not be found in the database</error>');
  35. return 1;
  36. }
  37. $output->writeln('Job class: ' . get_class($job));
  38. $output->writeln('Arguments: ' . json_encode($job->getArgument()));
  39. $output->writeln('');
  40. $question = new ConfirmationQuestion(
  41. '<comment>Do you really want to delete this background job ? It could create some misbehaviours in Nextcloud.</comment> (y/N) ', false,
  42. '/^(y|Y)/i'
  43. );
  44. $helper = $this->getHelper('question');
  45. if (!$helper->ask($input, $output, $question)) {
  46. $output->writeln('aborted.');
  47. return 0;
  48. }
  49. $this->jobList->remove($job, $job->getArgument());
  50. return 0;
  51. }
  52. }