Delete.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\Helper\QuestionHelper;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Question\ConfirmationQuestion;
  15. class Delete extends Base {
  16. public function __construct(
  17. protected IJobList $jobList,
  18. ) {
  19. parent::__construct();
  20. }
  21. protected function configure(): void {
  22. $this
  23. ->setName('background-job:delete')
  24. ->setDescription('Remove a background job from database')
  25. ->addArgument(
  26. 'job-id',
  27. InputArgument::REQUIRED,
  28. 'The ID of the job in the database'
  29. );
  30. }
  31. protected function execute(InputInterface $input, OutputInterface $output): int {
  32. $jobId = (int)$input->getArgument('job-id');
  33. $job = $this->jobList->getById($jobId);
  34. if ($job === null) {
  35. $output->writeln('<error>Job with ID ' . $jobId . ' could not be found in the database</error>');
  36. return 1;
  37. }
  38. $output->writeln('Job class: ' . get_class($job));
  39. $output->writeln('Arguments: ' . json_encode($job->getArgument()));
  40. $output->writeln('');
  41. $question = new ConfirmationQuestion(
  42. '<comment>Do you really want to delete this background job ? It could create some misbehaviours in Nextcloud.</comment> (y/N) ', false,
  43. '/^(y|Y)/i'
  44. );
  45. /** @var QuestionHelper $helper */
  46. $helper = $this->getHelper('question');
  47. if (!$helper->ask($input, $output, $question)) {
  48. $output->writeln('aborted.');
  49. return 0;
  50. }
  51. $this->jobList->remove($job, $job->getArgument());
  52. return 0;
  53. }
  54. }