Skip to content

Commit

Permalink
Merge pull request #32552 from nextcloud/enh/improve-job-handling-com…
Browse files Browse the repository at this point in the history
…mands

Improve job handling through occ
  • Loading branch information
come-nc authored Jul 12, 2022
2 parents 6527e82 + 0386f42 commit c20d34b
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 116 deletions.
6 changes: 5 additions & 1 deletion core/Command/Background/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$job = $this->jobList->getById($jobId);
if ($job === null) {
$output->writeln('<error>Something went wrong when trying to retrieve Job with ID ' . $jobId . ' from database</error>');
return 1;
}
$job->execute($this->jobList, $this->logger);
$job = $this->jobList->getById($jobId);

if ($lastRun !== $job->getLastRun()) {
if (($job === null) || ($lastRun !== $job->getLastRun())) {
$output->writeln('<info>Job executed!</info>');
$output->writeln('');

Expand Down
86 changes: 86 additions & 0 deletions core/Command/Background/ListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2022, Côme Chilliet <[email protected]>
*
* @author Côme Chilliet <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Core\Command\Background;

use OC\Core\Command\Base;
use OCP\BackgroundJob\IJobList;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ListCommand extends Base {
protected IJobList $jobList;

public function __construct(IJobList $jobList) {
parent::__construct();
$this->jobList = $jobList;
}

protected function configure(): void {
$this
->setName('background-job:list')
->setDescription('List background jobs')
->addOption(
'class',
'c',
InputOption::VALUE_OPTIONAL,
'Job class to search for',
null
)->addOption(
'limit',
'l',
InputOption::VALUE_OPTIONAL,
'Number of jobs to retrieve',
'10'
)->addOption(
'offset',
'o',
InputOption::VALUE_OPTIONAL,
'Offset for retrieving jobs',
'0'
)
;
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$jobs = $this->jobList->getJobs($input->getOption('class'), (int)$input->getOption('limit'), (int)$input->getOption('offset'));
$this->writeTableInOutputFormat($input, $output, $this->formatJobs($jobs));
return 0;
}

protected function formatJobs(array $jobs): array {
return array_map(
fn ($job) => [
'id' => $job->getId(),
'class' => get_class($job),
'last_run' => date(DATE_ATOM, $job->getLastRun()),
'argument' => json_encode($job->getArgument()),
],
$jobs
);
}
}
25 changes: 23 additions & 2 deletions core/Command/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
namespace OC\Core\Command;

use OC\Core\Command\User\ListCommand;
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -54,7 +55,7 @@ protected function configure() {
;
}

protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, array $items, string $prefix = ' - ') {
protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, array $items, string $prefix = ' - '): void {
switch ($input->getOption('output')) {
case self::OUTPUT_FORMAT_JSON:
$output->writeln(json_encode($items));
Expand Down Expand Up @@ -84,6 +85,26 @@ protected function writeArrayInOutputFormat(InputInterface $input, OutputInterfa
}
}

protected function writeTableInOutputFormat(InputInterface $input, OutputInterface $output, array $items): void {
switch ($input->getOption('output')) {
case self::OUTPUT_FORMAT_JSON:
$output->writeln(json_encode($items));
break;
case self::OUTPUT_FORMAT_JSON_PRETTY:
$output->writeln(json_encode($items, JSON_PRETTY_PRINT));
break;
default:
$table = new Table($output);
$table->setRows($items);
if (!empty($items) && is_string(array_key_first(reset($items)))) {
$table->setHeaders(array_keys(reset($items)));
}
$table->render();
break;
}
}


/**
* @param mixed $item
*/
Expand Down
1 change: 1 addition & 0 deletions core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
$application->add(new OC\Core\Command\Background\WebCron(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Background\Ajax(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Background\Job(\OC::$server->getJobList(), \OC::$server->getLogger()));
$application->add(new OC\Core\Command\Background\ListCommand(\OC::$server->getJobList()));

$application->add(\OC::$server->query(\OC\Core\Command\Broadcast\Test::class));

Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@
'OC\\Core\\Command\\Background\\Base' => $baseDir . '/core/Command/Background/Base.php',
'OC\\Core\\Command\\Background\\Cron' => $baseDir . '/core/Command/Background/Cron.php',
'OC\\Core\\Command\\Background\\Job' => $baseDir . '/core/Command/Background/Job.php',
'OC\\Core\\Command\\Background\\ListCommand' => $baseDir . '/core/Command/Background/ListCommand.php',
'OC\\Core\\Command\\Background\\WebCron' => $baseDir . '/core/Command/Background/WebCron.php',
'OC\\Core\\Command\\Base' => $baseDir . '/core/Command/Base.php',
'OC\\Core\\Command\\Broadcast\\Test' => $baseDir . '/core/Command/Broadcast/Test.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Command\\Background\\Base' => __DIR__ . '/../../..' . '/core/Command/Background/Base.php',
'OC\\Core\\Command\\Background\\Cron' => __DIR__ . '/../../..' . '/core/Command/Background/Cron.php',
'OC\\Core\\Command\\Background\\Job' => __DIR__ . '/../../..' . '/core/Command/Background/Job.php',
'OC\\Core\\Command\\Background\\ListCommand' => __DIR__ . '/../../..' . '/core/Command/Background/ListCommand.php',
'OC\\Core\\Command\\Background\\WebCron' => __DIR__ . '/../../..' . '/core/Command/Background/WebCron.php',
'OC\\Core\\Command\\Base' => __DIR__ . '/../../..' . '/core/Command/Base.php',
'OC\\Core\\Command\\Broadcast\\Test' => __DIR__ . '/../../..' . '/core/Command/Broadcast/Test.php',
Expand Down
Loading

0 comments on commit c20d34b

Please sign in to comment.