aboutsummaryrefslogtreecommitdiffstats
path: root/core/Command/Background
diff options
context:
space:
mode:
Diffstat (limited to 'core/Command/Background')
-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
5 files changed, 61 insertions, 18 deletions
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;
}