aboutsummaryrefslogtreecommitdiffstats
path: root/core/Command/Background
diff options
context:
space:
mode:
Diffstat (limited to 'core/Command/Background')
-rw-r--r--core/Command/Background/Ajax.php12
-rw-r--r--core/Command/Background/Base.php50
-rw-r--r--core/Command/Background/Cron.php12
-rw-r--r--core/Command/Background/Delete.php4
-rw-r--r--core/Command/Background/Job.php18
-rw-r--r--core/Command/Background/JobBase.php19
-rw-r--r--core/Command/Background/JobWorker.php36
-rw-r--r--core/Command/Background/ListCommand.php2
-rw-r--r--core/Command/Background/Mode.php46
-rw-r--r--core/Command/Background/WebCron.php12
10 files changed, 107 insertions, 104 deletions
diff --git a/core/Command/Background/Ajax.php b/core/Command/Background/Ajax.php
deleted file mode 100644
index 16486649bf0..00000000000
--- a/core/Command/Background/Ajax.php
+++ /dev/null
@@ -1,12 +0,0 @@
-<?php
-/**
- * SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
- * SPDX-License-Identifier: MIT
- */
-namespace OC\Core\Command\Background;
-
-class Ajax extends Base {
- protected function getMode(): string {
- return 'ajax';
- }
-}
diff --git a/core/Command/Background/Base.php b/core/Command/Background/Base.php
deleted file mode 100644
index e9946ad4cc7..00000000000
--- a/core/Command/Background/Base.php
+++ /dev/null
@@ -1,50 +0,0 @@
-<?php
-/**
- * SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
- * SPDX-License-Identifier: MIT
- */
-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();
-
- public function __construct(
- protected IConfig $config,
- ) {
- parent::__construct();
- }
-
- protected function configure(): void {
- $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
- * @return int
- */
- 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 9f010b456fa..00000000000
--- a/core/Command/Background/Cron.php
+++ /dev/null
@@ -1,12 +0,0 @@
-<?php
-/**
- * SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
- * SPDX-License-Identifier: MIT
- */
-namespace OC\Core\Command\Background;
-
-class Cron extends Base {
- protected function getMode(): string {
- return 'cron';
- }
-}
diff --git a/core/Command/Background/Delete.php b/core/Command/Background/Delete.php
index 45148d2d414..50ae309065b 100644
--- a/core/Command/Background/Delete.php
+++ b/core/Command/Background/Delete.php
@@ -10,6 +10,7 @@ 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;
@@ -34,7 +35,7 @@ class Delete extends Base {
}
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) {
@@ -51,6 +52,7 @@ class Delete extends Base {
'/^(y|Y)/i'
);
+ /** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
if (!$helper->ask($input, $output, $question)) {
$output->writeln('aborted.');
diff --git a/core/Command/Background/Job.php b/core/Command/Background/Job.php
index 8e09a28dce6..9a862f5a13a 100644
--- a/core/Command/Background/Job.php
+++ b/core/Command/Background/Job.php
@@ -10,6 +10,8 @@ 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;
@@ -42,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) {
@@ -75,7 +77,7 @@ class Job extends Command {
$output->writeln('<info>Job executed!</info>');
$output->writeln('');
- if ($job instanceof \OCP\BackgroundJob\TimedJob) {
+ if ($job instanceof TimedJob) {
$this->printJobInfo($jobId, $job, $output);
}
} else {
@@ -90,19 +92,19 @@ class Job extends Command {
$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 \OCP\BackgroundJob\TimedJob;
+ $isTimedJob = $job instanceof TimedJob;
if ($isTimedJob) {
$output->writeln('Type: timed');
- } elseif ($job instanceof \OCP\BackgroundJob\QueuedJob) {
+ } elseif ($job instanceof QueuedJob) {
$output->writeln('Type: queued');
} else {
$output->writeln('Type: job');
@@ -110,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
index 756e0ad7b80..81d16f874eb 100644
--- a/core/Command/Background/JobBase.php
+++ b/core/Command/Background/JobBase.php
@@ -10,16 +10,19 @@ declare(strict_types=1);
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 \OC\Core\Command\Base {
+abstract class JobBase extends Base {
public function __construct(
protected IJobList $jobList,
- protected LoggerInterface $logger
+ protected LoggerInterface $logger,
) {
parent::__construct();
}
@@ -32,19 +35,19 @@ abstract class JobBase extends \OC\Core\Command\Base {
}
$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 \OCP\BackgroundJob\TimedJob;
+ $isTimedJob = $job instanceof TimedJob;
if ($isTimedJob) {
$output->writeln('Type: timed');
- } elseif ($job instanceof \OCP\BackgroundJob\QueuedJob) {
+ } elseif ($job instanceof QueuedJob) {
$output->writeln('Type: queued');
} else {
$output->writeln('Type: job');
@@ -52,7 +55,7 @@ abstract class JobBase extends \OC\Core\Command\Base {
$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/JobWorker.php b/core/Command/Background/JobWorker.php
index 61f21c5548c..8289021887b 100644
--- a/core/Command/Background/JobWorker.php
+++ b/core/Command/Background/JobWorker.php
@@ -52,10 +52,25 @@ class JobWorker extends JobBase {
'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;
@@ -70,6 +85,11 @@ class JobWorker extends JobBase {
}
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();
@@ -137,4 +157,20 @@ class JobWorker extends JobBase {
}
$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 c84931c81e4..c8efbfef5c7 100644
--- a/core/Command/Background/ListCommand.php
+++ b/core/Command/Background/ListCommand.php
@@ -53,7 +53,7 @@ class ListCommand extends Base {
$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>");
+ $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 1e3e5ca5f17..00000000000
--- a/core/Command/Background/WebCron.php
+++ /dev/null
@@ -1,12 +0,0 @@
-<?php
-/**
- * SPDX-FileCopyrightText: 2015 Christian Kampka <christian@kampka.net>
- * SPDX-License-Identifier: MIT
- */
-namespace OC\Core\Command\Background;
-
-class WebCron extends Base {
- protected function getMode(): string {
- return 'webcron';
- }
-}