ListCommand.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. protected IJobList $jobList;
  32. public function __construct(IJobList $jobList) {
  33. parent::__construct();
  34. $this->jobList = $jobList;
  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. '10'
  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. $jobs = $this->jobList->getJobsIterator($input->getOption('class'), (int)$input->getOption('limit'), (int)$input->getOption('offset'));
  64. $this->writeTableInOutputFormat($input, $output, $this->formatJobs($jobs));
  65. return 0;
  66. }
  67. protected function formatJobs(iterable $jobs): array {
  68. $jobsInfo = [];
  69. foreach ($jobs as $job) {
  70. $jobsInfo[] = [
  71. 'id' => $job->getId(),
  72. 'class' => get_class($job),
  73. 'last_run' => date(DATE_ATOM, $job->getLastRun()),
  74. 'argument' => json_encode($job->getArgument()),
  75. ];
  76. }
  77. return $jobsInfo;
  78. }
  79. }