diff options
Diffstat (limited to 'core/Command/Background')
-rw-r--r-- | core/Command/Background/Ajax.php | 32 | ||||
-rw-r--r-- | core/Command/Background/Base.php | 69 | ||||
-rw-r--r-- | core/Command/Background/Cron.php | 32 | ||||
-rw-r--r-- | core/Command/Background/Delete.php | 65 | ||||
-rw-r--r-- | core/Command/Background/Job.php | 55 | ||||
-rw-r--r-- | core/Command/Background/JobBase.php | 82 | ||||
-rw-r--r-- | core/Command/Background/JobWorker.php | 176 | ||||
-rw-r--r-- | core/Command/Background/ListCommand.php | 38 | ||||
-rw-r--r-- | core/Command/Background/Mode.php | 46 | ||||
-rw-r--r-- | core/Command/Background/WebCron.php | 32 |
10 files changed, 399 insertions, 228 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 dca7b58a5fc..00000000000 --- a/core/Command/Background/Base.php +++ /dev/null @@ -1,69 +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(); - protected 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 index 823498cf8ca..9a862f5a13a 100644 --- a/core/Command/Background/Job.php +++ b/core/Command/Background/Job.php @@ -2,32 +2,16 @@ 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/>. - * + * 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\ILogger; +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; @@ -35,14 +19,10 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class Job extends Command { - protected IJobList $jobList; - protected ILogger $logger; - - public function __construct(IJobList $jobList, - ILogger $logger) { + public function __construct( + protected IJobList $jobList, + ) { parent::__construct(); - $this->jobList = $jobList; - $this->logger = $logger; } protected function configure(): void { @@ -64,7 +44,7 @@ class Job extends Command { } protected function execute(InputInterface $input, OutputInterface $output): int { - $jobId = (int) $input->getArgument('job-id'); + $jobId = (int)$input->getArgument('job-id'); $job = $this->jobList->getById($jobId); if ($job === null) { @@ -89,14 +69,15 @@ class Job extends Command { $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); + /** @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 \OC\BackgroundJob\TimedJob || $job instanceof \OCP\BackgroundJob\TimedJob) { + if ($job instanceof TimedJob) { $this->printJobInfo($jobId, $job, $output); } } else { @@ -107,23 +88,23 @@ class Job extends Command { return 0; } - protected function printJobInfo(int $jobId, IJob $job, OutputInterface$output): void { + protected function printJobInfo(int $jobId, IJob $job, OutputInterface $output): void { $row = $this->jobList->getDetailsById($jobId); $lastRun = new \DateTime(); - $lastRun->setTimestamp((int) $row['last_run']); + $lastRun->setTimestamp((int)$row['last_run']); $lastChecked = new \DateTime(); - $lastChecked->setTimestamp((int) $row['last_checked']); + $lastChecked->setTimestamp((int)$row['last_checked']); $reservedAt = new \DateTime(); - $reservedAt->setTimestamp((int) $row['reserved_at']); + $reservedAt->setTimestamp((int)$row['reserved_at']); $output->writeln('Job class: ' . get_class($job)); $output->writeln('Arguments: ' . json_encode($job->getArgument())); - $isTimedJob = $job instanceof \OC\BackgroundJob\TimedJob || $job instanceof \OCP\BackgroundJob\TimedJob; + $isTimedJob = $job instanceof TimedJob; if ($isTimedJob) { $output->writeln('Type: timed'); - } elseif ($job instanceof \OC\BackgroundJob\QueuedJob || $job instanceof \OCP\BackgroundJob\QueuedJob) { + } elseif ($job instanceof QueuedJob) { $output->writeln('Type: queued'); } else { $output->writeln('Type: job'); @@ -131,7 +112,7 @@ class Job extends Command { $output->writeln(''); $output->writeln('Last checked: ' . $lastChecked->format(\DateTimeInterface::ATOM)); - if ((int) $row['reserved_at'] === 0) { + if ((int)$row['reserved_at'] === 0) { $output->writeln('Reserved at: -'); } else { $output->writeln('Reserved at: <comment>' . $reservedAt->format(\DateTimeInterface::ATOM) . '</comment>'); 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 index 4116bfa0ff1..c8efbfef5c7 100644 --- a/core/Command/Background/ListCommand.php +++ b/core/Command/Background/ListCommand.php @@ -2,25 +2,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2022, Côme Chilliet <come.chilliet@nextcloud.com> - * - * @author Côme Chilliet <come.chilliet@nextcloud.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/>. - * + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\Core\Command\Background; @@ -32,11 +15,10 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class ListCommand extends Base { - protected IJobList $jobList; - - public function __construct(IJobList $jobList) { + public function __construct( + protected IJobList $jobList, + ) { parent::__construct(); - $this->jobList = $jobList; } protected function configure(): void { @@ -54,7 +36,7 @@ class ListCommand extends Base { 'l', InputOption::VALUE_OPTIONAL, 'Number of jobs to retrieve', - '10' + '500' )->addOption( 'offset', 'o', @@ -67,8 +49,12 @@ class ListCommand extends Base { } protected function execute(InputInterface $input, OutputInterface $output): int { - $jobs = $this->jobList->getJobsIterator($input->getOption('class'), (int)$input->getOption('limit'), (int)$input->getOption('offset')); - $this->writeTableInOutputFormat($input, $output, $this->formatJobs($jobs)); + $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; } 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'; - } -} |