ListCommand.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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->getJobs($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(array $jobs): array {
  68. return array_map(
  69. fn ($job) => [
  70. 'id' => $job->getId(),
  71. 'class' => get_class($job),
  72. 'last_run' => date(DATE_ATOM, $job->getLastRun()),
  73. 'argument' => json_encode($job->getArgument()),
  74. ],
  75. $jobs
  76. );
  77. }
  78. }