ListCommand.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 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\InputInterface;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class ListCommand extends Base {
  14. public function __construct(
  15. protected IJobList $jobList,
  16. ) {
  17. parent::__construct();
  18. }
  19. protected function configure(): void {
  20. $this
  21. ->setName('background-job:list')
  22. ->setDescription('List background jobs')
  23. ->addOption(
  24. 'class',
  25. 'c',
  26. InputOption::VALUE_OPTIONAL,
  27. 'Job class to search for',
  28. null
  29. )->addOption(
  30. 'limit',
  31. 'l',
  32. InputOption::VALUE_OPTIONAL,
  33. 'Number of jobs to retrieve',
  34. '500'
  35. )->addOption(
  36. 'offset',
  37. 'o',
  38. InputOption::VALUE_OPTIONAL,
  39. 'Offset for retrieving jobs',
  40. '0'
  41. )
  42. ;
  43. parent::configure();
  44. }
  45. protected function execute(InputInterface $input, OutputInterface $output): int {
  46. $limit = (int)$input->getOption('limit');
  47. $jobsInfo = $this->formatJobs($this->jobList->getJobsIterator($input->getOption('class'), $limit, (int)$input->getOption('offset')));
  48. $this->writeTableInOutputFormat($input, $output, $jobsInfo);
  49. if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN && count($jobsInfo) >= $limit) {
  50. $output->writeln("\n<comment>Output is currently limited to " . $limit . " jobs. Specify `-l, --limit[=LIMIT]` to override.</comment>");
  51. }
  52. return 0;
  53. }
  54. protected function formatJobs(iterable $jobs): array {
  55. $jobsInfo = [];
  56. foreach ($jobs as $job) {
  57. $jobsInfo[] = [
  58. 'id' => $job->getId(),
  59. 'class' => get_class($job),
  60. 'last_run' => date(DATE_ATOM, $job->getLastRun()),
  61. 'argument' => json_encode($job->getArgument()),
  62. ];
  63. }
  64. return $jobsInfo;
  65. }
  66. }