From 8638f3b587661cc79c47611e780ab5ca935a9e4f Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Fri, 16 Aug 2024 13:23:10 +0200 Subject: [PATCH] feat(occ): add 'stop_after' option to stop the worker after some time Signed-off-by: Julien Veyssier --- core/Command/Background/JobWorker.php | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) 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('Background job worker will stop after ' . $stopAfterSeconds . ' seconds'); + } + $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; + } } -- 2.39.5