diff options
author | Côme Chilliet <come.chilliet@nextcloud.com> | 2022-05-23 13:30:26 +0300 |
---|---|---|
committer | Côme Chilliet (Rebase PR Action) <come-nc@users.noreply.github.com> | 2022-07-11 12:46:23 +0300 |
commit | 3d0117990749bebe5394ff664d62494ae9a6fa1e (patch) | |
tree | 0eafa0b6fcc4d8d15654254ddd03db5209842996 | |
parent | 0b7779b6ff7dcb603d1088f3ea8fbf42ce5a98c4 (diff) |
Add command to list jobs
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
-rw-r--r-- | core/Command/Background/ListCommand.php | 95 | ||||
-rw-r--r-- | core/register_command.php | 1 | ||||
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | lib/private/BackgroundJob/JobList.php | 18 | ||||
-rw-r--r-- | lib/public/BackgroundJob/IJobList.php | 18 |
6 files changed, 125 insertions, 9 deletions
diff --git a/core/Command/Background/ListCommand.php b/core/Command/Background/ListCommand.php new file mode 100644 index 00000000000..a0bf611606f --- /dev/null +++ b/core/Command/Background/ListCommand.php @@ -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 + ); + } +} diff --git a/core/register_command.php b/core/register_command.php index de04fedf30d..25c368d915c 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -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)); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 49957c1f3ac..f1436219db0 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -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', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index bb9cb0de31f..f8733fbc73b 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -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', diff --git a/lib/private/BackgroundJob/JobList.php b/lib/private/BackgroundJob/JobList.php index fa47f9b6daa..8ba993646a9 100644 --- a/lib/private/BackgroundJob/JobList.php +++ b/lib/private/BackgroundJob/JobList.php @@ -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 = []; diff --git a/lib/public/BackgroundJob/IJobList.php b/lib/public/BackgroundJob/IJobList.php index eab37a03f36..7bc7420f35b 100644 --- a/lib/public/BackgroundJob/IJobList.php +++ b/lib/public/BackgroundJob/IJobList.php @@ -83,6 +83,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 * * @param bool $onlyTimeSensitive @@ -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; |