aboutsummaryrefslogtreecommitdiffstats
path: root/core/Command/Background
diff options
context:
space:
mode:
Diffstat (limited to 'core/Command/Background')
-rw-r--r--core/Command/Background/Ajax.php32
-rw-r--r--core/Command/Background/Base.php76
-rw-r--r--core/Command/Background/Cron.php32
-rw-r--r--core/Command/Background/Delete.php65
-rw-r--r--core/Command/Background/Job.php139
-rw-r--r--core/Command/Background/JobBase.php82
-rw-r--r--core/Command/Background/JobWorker.php176
-rw-r--r--core/Command/Background/ListCommand.php73
-rw-r--r--core/Command/Background/Mode.php46
-rw-r--r--core/Command/Background/WebCron.php32
10 files changed, 581 insertions, 172 deletions
diff --git a/core/Command/Background/Ajax.php b/core/Command/Background/Ajax.php
deleted file mode 100644
index 5dc94d939d7..00000000000
--- a/core/Command/Background/Ajax.php
+++ /dev/null
@@ -1,32 +0,0 @@
-<?php
-/**
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 Christian Kampka <christian@kampka.net>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-namespace OC\Core\Command\Background;
-
-class Ajax extends Base {
- protected function getMode() {
- return 'ajax';
- }
-}
diff --git a/core/Command/Background/Base.php b/core/Command/Background/Base.php
deleted file mode 100644
index b80fd751177..00000000000
--- a/core/Command/Background/Base.php
+++ /dev/null
@@ -1,76 +0,0 @@
-<?php
-/**
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 Christian Kampka <christian@kampka.net>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-namespace OC\Core\Command\Background;
-
-use OCP\IConfig;
-
-use Symfony\Component\Console\Command\Command;
-use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Output\OutputInterface;
-
-/**
- * An abstract base class for configuring the background job mode
- * from the command line interface.
- * Subclasses will override the getMode() function to specify the mode to configure.
- */
-abstract class Base extends Command {
- abstract protected function getMode();
-
- /**
- * @var \OCP\IConfig
- */
- protected $config;
-
- /**
- * @param \OCP\IConfig $config
- */
- public function __construct(IConfig $config) {
- parent::__construct();
- $this->config = $config;
- }
-
- protected function configure() {
- $mode = $this->getMode();
- $this
- ->setName("background:$mode")
- ->setDescription("Use $mode to run background jobs");
- }
-
- /**
- * Executing this command will set the background job mode for owncloud.
- * The mode to set is specified by the concrete sub class by implementing the
- * getMode() function.
- *
- * @param InputInterface $input
- * @param OutputInterface $output
- */
- protected function execute(InputInterface $input, OutputInterface $output): int {
- $mode = $this->getMode();
- $this->config->setAppValue('core', 'backgroundjobs_mode', $mode);
- $output->writeln("Set mode for background jobs to '$mode'");
- return 0;
- }
-}
diff --git a/core/Command/Background/Cron.php b/core/Command/Background/Cron.php
deleted file mode 100644
index 9dbb4f855e5..00000000000
--- a/core/Command/Background/Cron.php
+++ /dev/null
@@ -1,32 +0,0 @@
-<?php
-/**
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 Christian Kampka <christian@kampka.net>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-namespace OC\Core\Command\Background;
-
-class Cron extends Base {
- protected function getMode() {
- return 'cron';
- }
-}
diff --git a/core/Command/Background/Delete.php b/core/Command/Background/Delete.php
new file mode 100644
index 00000000000..50ae309065b
--- /dev/null
+++ b/core/Command/Background/Delete.php
@@ -0,0 +1,65 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OC\Core\Command\Background;
+
+use OC\Core\Command\Base;
+use OCP\BackgroundJob\IJobList;
+use Symfony\Component\Console\Helper\QuestionHelper;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Question\ConfirmationQuestion;
+
+class Delete extends Base {
+ public function __construct(
+ protected IJobList $jobList,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ $this
+ ->setName('background-job:delete')
+ ->setDescription('Remove a background job from database')
+ ->addArgument(
+ 'job-id',
+ InputArgument::REQUIRED,
+ 'The ID of the job in the database'
+ );
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output): int {
+ $jobId = (int)$input->getArgument('job-id');
+
+ $job = $this->jobList->getById($jobId);
+ if ($job === null) {
+ $output->writeln('<error>Job with ID ' . $jobId . ' could not be found in the database</error>');
+ return 1;
+ }
+
+ $output->writeln('Job class: ' . get_class($job));
+ $output->writeln('Arguments: ' . json_encode($job->getArgument()));
+ $output->writeln('');
+
+ $question = new ConfirmationQuestion(
+ '<comment>Do you really want to delete this background job ? It could create some misbehaviours in Nextcloud.</comment> (y/N) ', false,
+ '/^(y|Y)/i'
+ );
+
+ /** @var QuestionHelper $helper */
+ $helper = $this->getHelper('question');
+ if (!$helper->ask($input, $output, $question)) {
+ $output->writeln('aborted.');
+ return 0;
+ }
+
+ $this->jobList->remove($job, $job->getArgument());
+ return 0;
+ }
+}
diff --git a/core/Command/Background/Job.php b/core/Command/Background/Job.php
new file mode 100644
index 00000000000..9a862f5a13a
--- /dev/null
+++ b/core/Command/Background/Job.php
@@ -0,0 +1,139 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OC\Core\Command\Background;
+
+use OCP\BackgroundJob\IJob;
+use OCP\BackgroundJob\IJobList;
+use OCP\BackgroundJob\QueuedJob;
+use OCP\BackgroundJob\TimedJob;
+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 Job extends Command {
+ public function __construct(
+ protected IJobList $jobList,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ $this
+ ->setName('background-job:execute')
+ ->setDescription('Execute a single background job manually')
+ ->addArgument(
+ 'job-id',
+ InputArgument::REQUIRED,
+ 'The ID of the job in the database'
+ )
+ ->addOption(
+ 'force-execute',
+ null,
+ InputOption::VALUE_NONE,
+ 'Force execute the background job, independent from last run and being reserved'
+ )
+ ;
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output): int {
+ $jobId = (int)$input->getArgument('job-id');
+
+ $job = $this->jobList->getById($jobId);
+ if ($job === null) {
+ $output->writeln('<error>Job with ID ' . $jobId . ' could not be found in the database</error>');
+ return 1;
+ }
+
+ $this->printJobInfo($jobId, $job, $output);
+ $output->writeln('');
+
+ $lastRun = $job->getLastRun();
+ if ($input->getOption('force-execute')) {
+ $lastRun = 0;
+ $output->writeln('<comment>Forcing execution of the job</comment>');
+ $output->writeln('');
+
+ $this->jobList->resetBackgroundJob($job);
+ }
+
+ $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;
+ }
+ /** @psalm-suppress DeprecatedMethod Calling execute until it is removed, then will switch to start */
+ $job->execute($this->jobList);
+ $job = $this->jobList->getById($jobId);
+
+ if (($job === null) || ($lastRun !== $job->getLastRun())) {
+ $output->writeln('<info>Job executed!</info>');
+ $output->writeln('');
+
+ if ($job instanceof TimedJob) {
+ $this->printJobInfo($jobId, $job, $output);
+ }
+ } else {
+ $output->writeln('<comment>Job was not executed because it is not due</comment>');
+ $output->writeln('Specify the <question>--force-execute</question> option to run it anyway');
+ }
+
+ return 0;
+ }
+
+ protected function printJobInfo(int $jobId, IJob $job, OutputInterface $output): void {
+ $row = $this->jobList->getDetailsById($jobId);
+
+ $lastRun = new \DateTime();
+ $lastRun->setTimestamp((int)$row['last_run']);
+ $lastChecked = new \DateTime();
+ $lastChecked->setTimestamp((int)$row['last_checked']);
+ $reservedAt = new \DateTime();
+ $reservedAt->setTimestamp((int)$row['reserved_at']);
+
+ $output->writeln('Job class: ' . get_class($job));
+ $output->writeln('Arguments: ' . json_encode($job->getArgument()));
+
+ $isTimedJob = $job instanceof TimedJob;
+ if ($isTimedJob) {
+ $output->writeln('Type: timed');
+ } elseif ($job instanceof QueuedJob) {
+ $output->writeln('Type: queued');
+ } else {
+ $output->writeln('Type: job');
+ }
+
+ $output->writeln('');
+ $output->writeln('Last checked: ' . $lastChecked->format(\DateTimeInterface::ATOM));
+ if ((int)$row['reserved_at'] === 0) {
+ $output->writeln('Reserved at: -');
+ } else {
+ $output->writeln('Reserved at: <comment>' . $reservedAt->format(\DateTimeInterface::ATOM) . '</comment>');
+ }
+ $output->writeln('Last executed: ' . $lastRun->format(\DateTimeInterface::ATOM));
+ $output->writeln('Last duration: ' . $row['execution_duration']);
+
+ if ($isTimedJob) {
+ $reflection = new \ReflectionClass($job);
+ $intervalProperty = $reflection->getProperty('interval');
+ $intervalProperty->setAccessible(true);
+ $interval = $intervalProperty->getValue($job);
+
+ $nextRun = new \DateTime();
+ $nextRun->setTimestamp($row['last_run'] + $interval);
+
+ if ($nextRun > new \DateTime()) {
+ $output->writeln('Next execution: <comment>' . $nextRun->format(\DateTimeInterface::ATOM) . '</comment>');
+ } else {
+ $output->writeln('Next execution: <info>' . $nextRun->format(\DateTimeInterface::ATOM) . '</info>');
+ }
+ }
+ }
+}
diff --git a/core/Command/Background/JobBase.php b/core/Command/Background/JobBase.php
new file mode 100644
index 00000000000..81d16f874eb
--- /dev/null
+++ b/core/Command/Background/JobBase.php
@@ -0,0 +1,82 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+
+namespace OC\Core\Command\Background;
+
+use OC\Core\Command\Base;
+use OCP\BackgroundJob\IJob;
+use OCP\BackgroundJob\IJobList;
+use OCP\BackgroundJob\QueuedJob;
+use OCP\BackgroundJob\TimedJob;
+use Psr\Log\LoggerInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+abstract class JobBase extends Base {
+
+ public function __construct(
+ protected IJobList $jobList,
+ protected LoggerInterface $logger,
+ ) {
+ parent::__construct();
+ }
+
+ protected function printJobInfo(int $jobId, IJob $job, OutputInterface $output): void {
+ $row = $this->jobList->getDetailsById($jobId);
+
+ if ($row === null) {
+ return;
+ }
+
+ $lastRun = new \DateTime();
+ $lastRun->setTimestamp((int)$row['last_run']);
+ $lastChecked = new \DateTime();
+ $lastChecked->setTimestamp((int)$row['last_checked']);
+ $reservedAt = new \DateTime();
+ $reservedAt->setTimestamp((int)$row['reserved_at']);
+
+ $output->writeln('Job class: ' . get_class($job));
+ $output->writeln('Arguments: ' . json_encode($job->getArgument()));
+
+ $isTimedJob = $job instanceof TimedJob;
+ if ($isTimedJob) {
+ $output->writeln('Type: timed');
+ } elseif ($job instanceof QueuedJob) {
+ $output->writeln('Type: queued');
+ } else {
+ $output->writeln('Type: job');
+ }
+
+ $output->writeln('');
+ $output->writeln('Last checked: ' . $lastChecked->format(\DateTimeInterface::ATOM));
+ if ((int)$row['reserved_at'] === 0) {
+ $output->writeln('Reserved at: -');
+ } else {
+ $output->writeln('Reserved at: <comment>' . $reservedAt->format(\DateTimeInterface::ATOM) . '</comment>');
+ }
+ $output->writeln('Last executed: ' . $lastRun->format(\DateTimeInterface::ATOM));
+ $output->writeln('Last duration: ' . $row['execution_duration']);
+
+ if ($isTimedJob) {
+ $reflection = new \ReflectionClass($job);
+ $intervalProperty = $reflection->getProperty('interval');
+ $intervalProperty->setAccessible(true);
+ $interval = $intervalProperty->getValue($job);
+
+ $nextRun = new \DateTime();
+ $nextRun->setTimestamp((int)$row['last_run'] + $interval);
+
+ if ($nextRun > new \DateTime()) {
+ $output->writeln('Next execution: <comment>' . $nextRun->format(\DateTimeInterface::ATOM) . '</comment>');
+ } else {
+ $output->writeln('Next execution: <info>' . $nextRun->format(\DateTimeInterface::ATOM) . '</info>');
+ }
+ }
+ }
+}
diff --git a/core/Command/Background/JobWorker.php b/core/Command/Background/JobWorker.php
new file mode 100644
index 00000000000..8289021887b
--- /dev/null
+++ b/core/Command/Background/JobWorker.php
@@ -0,0 +1,176 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OC\Core\Command\Background;
+
+use OC\Core\Command\InterruptedException;
+use OC\Files\SetupManager;
+use OCP\BackgroundJob\IJobList;
+use OCP\ITempManager;
+use Psr\Log\LoggerInterface;
+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 JobWorker extends JobBase {
+
+ public function __construct(
+ protected IJobList $jobList,
+ protected LoggerInterface $logger,
+ private ITempManager $tempManager,
+ private SetupManager $setupManager,
+ ) {
+ parent::__construct($jobList, $logger);
+ }
+ protected function configure(): void {
+ parent::configure();
+
+ $this
+ ->setName('background-job:worker')
+ ->setDescription('Run a background job worker')
+ ->addArgument(
+ 'job-classes',
+ InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
+ 'The classes of the jobs to look for in the database'
+ )
+ ->addOption(
+ 'once',
+ null,
+ InputOption::VALUE_NONE,
+ 'Only execute the worker once (as a regular cron execution would do it)'
+ )
+ ->addOption(
+ 'interval',
+ 'i',
+ InputOption::VALUE_OPTIONAL,
+ 'Interval in seconds in which the worker should repeat already processed jobs (set to 0 for no repeat)',
+ 5
+ )
+ ->addOption(
+ 'stop_after',
+ 't',
+ InputOption::VALUE_OPTIONAL,
+ 'Duration after which the worker should stop and exit. The worker won\'t kill a potential running job, it will exit after this job has finished running (supported values are: "30" or "30s" for 30 seconds, "10m" for 10 minutes and "2h" for 2 hours)'
+ )
+ ;
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output): int {
+ $startTime = time();
+ $stopAfterOptionValue = $input->getOption('stop_after');
+ $stopAfterSeconds = $stopAfterOptionValue === null
+ ? null
+ : $this->parseStopAfter($stopAfterOptionValue);
+ if ($stopAfterSeconds !== null) {
+ $output->writeln('<info>Background job worker will stop after ' . $stopAfterSeconds . ' seconds</info>');
+ }
+
+ $jobClasses = $input->getArgument('job-classes');
+ $jobClasses = empty($jobClasses) ? null : $jobClasses;
+
+ if ($jobClasses !== null) {
+ // at least one class is invalid
+ foreach ($jobClasses as $jobClass) {
+ if (!class_exists($jobClass)) {
+ $output->writeln('<error>Invalid job class: ' . $jobClass . '</error>');
+ return 1;
+ }
+ }
+ }
+
+ while (true) {
+ // Stop if we exceeded stop_after value
+ if ($stopAfterSeconds !== null && ($startTime + $stopAfterSeconds) < time()) {
+ $output->writeln('stop_after time has been exceeded, exiting...', OutputInterface::VERBOSITY_VERBOSE);
+ break;
+ }
+ // Handle canceling of the process
+ try {
+ $this->abortIfInterrupted();
+ } catch (InterruptedException $e) {
+ $output->writeln('<info>Background job worker stopped</info>');
+ break;
+ }
+
+ $this->printSummary($input, $output);
+
+ usleep(50000);
+ $job = $this->jobList->getNext(false, $jobClasses);
+ if (!$job) {
+ if ($input->getOption('once') === true) {
+ if ($jobClasses === null) {
+ $output->writeln('No job is currently queued', OutputInterface::VERBOSITY_VERBOSE);
+ } else {
+ $output->writeln('No job of classes [' . implode(', ', $jobClasses) . '] is currently queued', OutputInterface::VERBOSITY_VERBOSE);
+ }
+ $output->writeln('Exiting...', OutputInterface::VERBOSITY_VERBOSE);
+ break;
+ }
+
+ $output->writeln('Waiting for new jobs to be queued', OutputInterface::VERBOSITY_VERBOSE);
+ // Re-check interval for new jobs
+ sleep(1);
+ continue;
+ }
+
+ $output->writeln('Running job ' . get_class($job) . ' with ID ' . $job->getId());
+
+ if ($output->isVerbose()) {
+ $this->printJobInfo($job->getId(), $job, $output);
+ }
+
+ /** @psalm-suppress DeprecatedMethod Calling execute until it is removed, then will switch to start */
+ $job->execute($this->jobList);
+
+ $output->writeln('Job ' . $job->getId() . ' has finished', OutputInterface::VERBOSITY_VERBOSE);
+
+ // clean up after unclean jobs
+ $this->setupManager->tearDown();
+ $this->tempManager->clean();
+
+ $this->jobList->setLastJob($job);
+ $this->jobList->unlockJob($job);
+
+ if ($input->getOption('once') === true) {
+ break;
+ }
+ }
+
+ return 0;
+ }
+
+ private function printSummary(InputInterface $input, OutputInterface $output): void {
+ if (!$output->isVeryVerbose()) {
+ return;
+ }
+ $output->writeln('<comment>Summary</comment>');
+
+ $counts = [];
+ foreach ($this->jobList->countByClass() as $row) {
+ $counts[] = $row;
+ }
+ $this->writeTableInOutputFormat($input, $output, $counts);
+ }
+
+ private function parseStopAfter(string $value): ?int {
+ if (is_numeric($value)) {
+ return (int)$value;
+ }
+ if (preg_match("/^(\d+)s$/i", $value, $matches)) {
+ return (int)$matches[0];
+ }
+ if (preg_match("/^(\d+)m$/i", $value, $matches)) {
+ return 60 * ((int)$matches[0]);
+ }
+ if (preg_match("/^(\d+)h$/i", $value, $matches)) {
+ return 60 * 60 * ((int)$matches[0]);
+ }
+ return null;
+ }
+}
diff --git a/core/Command/Background/ListCommand.php b/core/Command/Background/ListCommand.php
new file mode 100644
index 00000000000..c8efbfef5c7
--- /dev/null
+++ b/core/Command/Background/ListCommand.php
@@ -0,0 +1,73 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+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 {
+ public function __construct(
+ protected IJobList $jobList,
+ ) {
+ parent::__construct();
+ }
+
+ 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',
+ '500'
+ )->addOption(
+ 'offset',
+ 'o',
+ InputOption::VALUE_OPTIONAL,
+ 'Offset for retrieving jobs',
+ '0'
+ )
+ ;
+ parent::configure();
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output): int {
+ $limit = (int)$input->getOption('limit');
+ $jobsInfo = $this->formatJobs($this->jobList->getJobsIterator($input->getOption('class'), $limit, (int)$input->getOption('offset')));
+ $this->writeTableInOutputFormat($input, $output, $jobsInfo);
+ if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN && count($jobsInfo) >= $limit) {
+ $output->writeln("\n<comment>Output is currently limited to " . $limit . ' jobs. Specify `-l, --limit[=LIMIT]` to override.</comment>');
+ }
+ return 0;
+ }
+
+ protected function formatJobs(iterable $jobs): array {
+ $jobsInfo = [];
+ foreach ($jobs as $job) {
+ $jobsInfo[] = [
+ 'id' => $job->getId(),
+ 'class' => get_class($job),
+ 'last_run' => date(DATE_ATOM, $job->getLastRun()),
+ 'argument' => json_encode($job->getArgument()),
+ ];
+ }
+ return $jobsInfo;
+ }
+}
diff --git a/core/Command/Background/Mode.php b/core/Command/Background/Mode.php
new file mode 100644
index 00000000000..4c0f40bb4a2
--- /dev/null
+++ b/core/Command/Background/Mode.php
@@ -0,0 +1,46 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
+ * SPDX-License-Identifier: MIT
+ */
+namespace OC\Core\Command\Background;
+
+use OCP\IAppConfig;
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class Mode extends Command {
+ public function __construct(
+ private IAppConfig $appConfig,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ $this
+ ->setName('background:cron')
+ ->setAliases(['background:ajax', 'background:webcron'])
+ ->setDescription('Use cron, ajax or webcron to run background jobs');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output): int {
+ /** @var 'background:cron'|'background:ajax'|'background:webcron' $command */
+ $command = $input->getArgument('command');
+
+ $mode = match ($command) {
+ 'background:cron' => 'cron',
+ 'background:ajax' => 'ajax',
+ 'background:webcron' => 'webcron',
+ };
+
+ $this->appConfig->setValueString('core', 'backgroundjobs_mode', $mode);
+ $output->writeln("Set mode for background jobs to '" . $mode . "'");
+
+ return 0;
+ }
+}
diff --git a/core/Command/Background/WebCron.php b/core/Command/Background/WebCron.php
deleted file mode 100644
index 7da379b6a53..00000000000
--- a/core/Command/Background/WebCron.php
+++ /dev/null
@@ -1,32 +0,0 @@
-<?php
-/**
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 Christian Kampka <christian@kampka.net>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-namespace OC\Core\Command\Background;
-
-class WebCron extends Base {
- protected function getMode() {
- return 'webcron';
- }
-}