From 1bbd660648755fa09ccfb14485c62eba8d703ae1 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Tue, 28 Apr 2026 15:33:22 +0200 Subject: [PATCH] fix(TaskProcessingWorker): Only store config value every 60s Signed-off-by: Marcel Klehr --- core/Command/TaskProcessing/WorkerCommand.php | 14 ++++++++++---- .../Command/TaskProcessing/WorkerCommandTest.php | 6 +++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/core/Command/TaskProcessing/WorkerCommand.php b/core/Command/TaskProcessing/WorkerCommand.php index 15719f5245f..a5171b36c26 100644 --- a/core/Command/TaskProcessing/WorkerCommand.php +++ b/core/Command/TaskProcessing/WorkerCommand.php @@ -10,6 +10,7 @@ namespace OC\Core\Command\TaskProcessing; use OC\Core\Command\Base; use OC\Core\Command\InterruptedException; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\IAppConfig; use OCP\TaskProcessing\Exception\Exception; use OCP\TaskProcessing\Exception\NotFoundException; @@ -25,6 +26,7 @@ class WorkerCommand extends Base { private readonly IManager $taskProcessingManager, private readonly LoggerInterface $logger, private readonly IAppConfig $appConfig, + private readonly ITimeFactory $timeFactory, ) { parent::__construct(); } @@ -63,12 +65,13 @@ class WorkerCommand extends Base { } protected function execute(InputInterface $input, OutputInterface $output): int { - $startTime = time(); + $startTime = $this->timeFactory->now()->getTimestamp(); $timeout = (int)$input->getOption('timeout'); $interval = (int)$input->getOption('interval'); $once = $input->getOption('once') === true; /** @var list $taskTypes */ $taskTypes = $input->getOption('taskTypes'); + $lastConfigStorageTime = 0; if ($timeout > 0) { $output->writeln('Task processing worker will stop after ' . $timeout . ' seconds'); @@ -76,7 +79,7 @@ class WorkerCommand extends Base { while (true) { // Stop if timeout exceeded - if ($timeout > 0 && ($startTime + $timeout) < time()) { + if ($timeout > 0 && ($startTime + $timeout) < $this->timeFactory->now()->getTimestamp()) { $output->writeln('Timeout reached, exiting...', OutputInterface::VERBOSITY_VERBOSE); break; } @@ -84,12 +87,15 @@ class WorkerCommand extends Base { // Handle SIGTERM/SIGINT gracefully try { $this->abortIfInterrupted(); - } catch (InterruptedException $e) { + } catch (InterruptedException) { $output->writeln('Task processing worker stopped'); break; } - $this->appConfig->setValueString('core', 'taskprocessing_worker_last_iteration', (string)time(), lazy: true); + if ($lastConfigStorageTime < $this->timeFactory->now()->getTimestamp() - 60) { + $this->appConfig->setValueString('core', 'taskprocessing_worker_last_iteration', (string)$this->timeFactory->now()->getTimestamp(), lazy: true); + $lastConfigStorageTime = $this->timeFactory->now()->getTimestamp(); + } $processedTask = $this->processNextTask($output, $taskTypes); if ($once) { diff --git a/tests/Core/Command/TaskProcessing/WorkerCommandTest.php b/tests/Core/Command/TaskProcessing/WorkerCommandTest.php index 70fa2d46d8d..0e41f15682f 100644 --- a/tests/Core/Command/TaskProcessing/WorkerCommandTest.php +++ b/tests/Core/Command/TaskProcessing/WorkerCommandTest.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace Tests\Core\Command\TaskProcessing; use OC\Core\Command\TaskProcessing\WorkerCommand; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\IAppConfig; use OCP\TaskProcessing\Exception\Exception; use OCP\TaskProcessing\Exception\NotFoundException; @@ -26,6 +27,7 @@ class WorkerCommandTest extends TestCase { private IManager&MockObject $manager; private LoggerInterface&MockObject $logger; private IAppConfig&MockObject $appConfig; + private ITimeFactory&MockObject $timeFactory; private WorkerCommand $command; protected function setUp(): void { @@ -34,7 +36,9 @@ class WorkerCommandTest extends TestCase { $this->manager = $this->createMock(IManager::class); $this->logger = $this->createMock(LoggerInterface::class); $this->appConfig = $this->createMock(IAppConfig::class); - $this->command = new WorkerCommand($this->manager, $this->logger, $this->appConfig); + $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->timeFactory->method('now')->willReturnCallback(fn() => new \DateTimeImmutable()); + $this->command = new WorkerCommand($this->manager, $this->logger, $this->appConfig, $this->timeFactory); } /**