diff options
author | Julien Veyssier <julien-nc@posteo.net> | 2024-08-17 00:28:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-17 00:28:00 +0200 |
commit | 8d4968168f8a173fd9ac5406b084319523ed5b50 (patch) | |
tree | d2453bfe73c46fb3eb509175dc82a52b8caf8bdb | |
parent | c8e09d158bd419167fe10d023bd42fc7c8894b82 (diff) | |
parent | 8638f3b587661cc79c47611e780ab5ca935a9e4f (diff) | |
download | nextcloud-server-8d4968168f8a173fd9ac5406b084319523ed5b50.tar.gz nextcloud-server-8d4968168f8a173fd9ac5406b084319523ed5b50.zip |
Merge pull request #47277 from nextcloud/enh/noid/occ-bg-job-worker-max-time
[occ:bg-job:worker] Add 'stop_after' option
-rw-r--r-- | core/Command/Background/JobWorker.php | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/core/Command/Background/JobWorker.php b/core/Command/Background/JobWorker.php index 61f21c5548c..8e14397d350 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; + } } |