Browse Source

Add command to list jobs

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
tags/v25.0.0beta1
Côme Chilliet 2 years ago
parent
commit
3d01179907

+ 95
- 0
core/Command/Background/ListCommand.php View File

@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2021, Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @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\IJob;
use OCP\BackgroundJob\IJobList;
use OCP\ILogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ListCommand extends Base {
/** @var IJobList */
protected $jobList;
/** @var ILogger */
protected $logger;

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

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->writeArrayInOutputFormat($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' => $job->getLastRun(),
'argument' => json_encode($job->getArgument()),
],
$jobs
);
}
}

+ 1
- 0
core/register_command.php View File

@@ -90,6 +90,7 @@ if (\OC::$server->getConfig()->getSystemValue('installed', false)) {
$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(), \OC::$server->getLogger()));

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


+ 1
- 0
lib/composer/composer/autoload_classmap.php View File

@@ -859,6 +859,7 @@ return array(
'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',

+ 1
- 0
lib/composer/composer/autoload_static.php View File

@@ -892,6 +892,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',

+ 17
- 1
lib/private/BackgroundJob/JobList.php View File

@@ -166,9 +166,25 @@ class JobList implements IJobList {
* memory problems when creating too many instances.
*/
public function getAll() {
return $this->getJobs(null, null, 0);
}

public function getJobs($job, ?int $limit, int $offset): array {
$query = $this->connection->getQueryBuilder();
$query->select('*')
->from('jobs');
->from('jobs')
->setMaxResults($limit)
->setFirstResult($offset);

if ($job !== null) {
if ($job instanceof IJob) {
$class = get_class($job);
} else {
$class = $job;
}
$query->where($query->expr()->eq('class', $query->createNamedParameter($class)));
}

$result = $query->executeQuery();

$jobs = [];

+ 10
- 8
lib/public/BackgroundJob/IJobList.php View File

@@ -82,6 +82,15 @@ interface IJobList {
*/
public function getAll();

/**
* Get jobs matching the search
*
* @param \OCP\BackgroundJob\IJob|class-string<IJob>|null $job
* @return \OCP\BackgroundJob\IJob[]
* @since 25.0.0
*/
public function getJobs($job, ?int $limit, int $offset): array;

/**
* get the next job in the list
*
@@ -99,8 +108,6 @@ interface IJobList {
public function getById($id);

/**
* @param int $id
* @return array|null
* @since 23.0.0
*/
public function getDetailsById(int $id): ?array;
@@ -108,7 +115,6 @@ interface IJobList {
/**
* set the job that was last ran to the current time
*
* @param \OCP\BackgroundJob\IJob $job
* @since 7.0.0
*/
public function setLastJob(IJob $job);
@@ -116,7 +122,6 @@ interface IJobList {
/**
* Remove the reservation for a job
*
* @param IJob $job
* @since 9.1.0
*/
public function unlockJob(IJob $job);
@@ -124,7 +129,6 @@ interface IJobList {
/**
* set the lastRun of $job to now
*
* @param IJob $job
* @since 7.0.0
*/
public function setLastRun(IJob $job);
@@ -132,8 +136,7 @@ interface IJobList {
/**
* set the run duration of $job
*
* @param IJob $job
* @param $timeTaken
* @param int $timeTaken
* @since 12.0.0
*/
public function setExecutionTime(IJob $job, $timeTaken);
@@ -141,7 +144,6 @@ interface IJobList {
/**
* Reset the $job so it executes on the next trigger
*
* @param IJob $job
* @since 23.0.0
*/
public function resetBackgroundJob(IJob $job): void;

Loading…
Cancel
Save