ListCommand.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022, Côme Chilliet <come.chilliet@nextcloud.com>
  5. *
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Command\Background;
  25. use OC\Core\Command\Base;
  26. use OCP\BackgroundJob\IJobList;
  27. use Symfony\Component\Console\Input\InputInterface;
  28. use Symfony\Component\Console\Input\InputOption;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. class ListCommand extends Base {
  31. public function __construct(
  32. protected IJobList $jobList,
  33. ) {
  34. parent::__construct();
  35. }
  36. protected function configure(): void {
  37. $this
  38. ->setName('background-job:list')
  39. ->setDescription('List background jobs')
  40. ->addOption(
  41. 'class',
  42. 'c',
  43. InputOption::VALUE_OPTIONAL,
  44. 'Job class to search for',
  45. null
  46. )->addOption(
  47. 'limit',
  48. 'l',
  49. InputOption::VALUE_OPTIONAL,
  50. 'Number of jobs to retrieve',
  51. '500'
  52. )->addOption(
  53. 'offset',
  54. 'o',
  55. InputOption::VALUE_OPTIONAL,
  56. 'Offset for retrieving jobs',
  57. '0'
  58. )
  59. ;
  60. parent::configure();
  61. }
  62. protected function execute(InputInterface $input, OutputInterface $output): int {
  63. $limit = (int)$input->getOption('limit');
  64. $jobsInfo = $this->formatJobs($this->jobList->getJobsIterator($input->getOption('class'), $limit, (int)$input->getOption('offset')));
  65. $this->writeTableInOutputFormat($input, $output, $jobsInfo);
  66. if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN && count($jobsInfo) >= $limit) {
  67. $output->writeln("\n<comment>Output is currently limited to " . $limit . " jobs. Specify `-l, --limit[=LIMIT]` to override.</comment>");
  68. }
  69. return 0;
  70. }
  71. protected function formatJobs(iterable $jobs): array {
  72. $jobsInfo = [];
  73. foreach ($jobs as $job) {
  74. $jobsInfo[] = [
  75. 'id' => $job->getId(),
  76. 'class' => get_class($job),
  77. 'last_run' => date(DATE_ATOM, $job->getLastRun()),
  78. 'argument' => json_encode($job->getArgument()),
  79. ];
  80. }
  81. return $jobsInfo;
  82. }
  83. }