Skip to content
Open
1 change: 1 addition & 0 deletions apps/settings/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
'OCA\\Settings\\SetupChecks\\SystemIs64bit' => $baseDir . '/../lib/SetupChecks/SystemIs64bit.php',
'OCA\\Settings\\SetupChecks\\TaskProcessingPickupSpeed' => $baseDir . '/../lib/SetupChecks/TaskProcessingPickupSpeed.php',
'OCA\\Settings\\SetupChecks\\TaskProcessingSuccessRate' => $baseDir . '/../lib/SetupChecks/TaskProcessingSuccessRate.php',
'OCA\\Settings\\SetupChecks\\TaskProcessingWorkerIsRunning' => $baseDir . '/../lib/SetupChecks/TaskProcessingWorkerIsRunning.php',
'OCA\\Settings\\SetupChecks\\TempSpaceAvailable' => $baseDir . '/../lib/SetupChecks/TempSpaceAvailable.php',
'OCA\\Settings\\SetupChecks\\TransactionIsolation' => $baseDir . '/../lib/SetupChecks/TransactionIsolation.php',
'OCA\\Settings\\SetupChecks\\TwoFactorConfiguration' => $baseDir . '/../lib/SetupChecks/TwoFactorConfiguration.php',
Expand Down
1 change: 1 addition & 0 deletions apps/settings/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class ComposerStaticInitSettings
'OCA\\Settings\\SetupChecks\\SystemIs64bit' => __DIR__ . '/..' . '/../lib/SetupChecks/SystemIs64bit.php',
'OCA\\Settings\\SetupChecks\\TaskProcessingPickupSpeed' => __DIR__ . '/..' . '/../lib/SetupChecks/TaskProcessingPickupSpeed.php',
'OCA\\Settings\\SetupChecks\\TaskProcessingSuccessRate' => __DIR__ . '/..' . '/../lib/SetupChecks/TaskProcessingSuccessRate.php',
'OCA\\Settings\\SetupChecks\\TaskProcessingWorkerIsRunning' => __DIR__ . '/..' . '/../lib/SetupChecks/TaskProcessingWorkerIsRunning.php',
'OCA\\Settings\\SetupChecks\\TempSpaceAvailable' => __DIR__ . '/..' . '/../lib/SetupChecks/TempSpaceAvailable.php',
'OCA\\Settings\\SetupChecks\\TransactionIsolation' => __DIR__ . '/..' . '/../lib/SetupChecks/TransactionIsolation.php',
'OCA\\Settings\\SetupChecks\\TwoFactorConfiguration' => __DIR__ . '/..' . '/../lib/SetupChecks/TwoFactorConfiguration.php',
Expand Down
4 changes: 4 additions & 0 deletions apps/settings/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
use OCA\Settings\SetupChecks\SupportedDatabase;
use OCA\Settings\SetupChecks\SystemIs64bit;
use OCA\Settings\SetupChecks\TaskProcessingPickupSpeed;
use OCA\Settings\SetupChecks\TaskProcessingSuccessRate;
use OCA\Settings\SetupChecks\TaskProcessingWorkerIsRunning;
use OCA\Settings\SetupChecks\TempSpaceAvailable;
use OCA\Settings\SetupChecks\TransactionIsolation;
use OCA\Settings\SetupChecks\TwoFactorConfiguration;
Expand Down Expand Up @@ -212,6 +214,8 @@ public function register(IRegistrationContext $context): void {
$context->registerSetupCheck(SupportedDatabase::class);
$context->registerSetupCheck(SystemIs64bit::class);
$context->registerSetupCheck(TaskProcessingPickupSpeed::class);
$context->registerSetupCheck(TaskProcessingSuccessRate::class);
$context->registerSetupCheck(TaskProcessingWorkerIsRunning::class);
$context->registerSetupCheck(TempSpaceAvailable::class);
$context->registerSetupCheck(TransactionIsolation::class);
$context->registerSetupCheck(TwoFactorConfiguration::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use OCP\TaskProcessing\IManager;

class TaskProcessingPickupSpeed implements ISetupCheck {
public const MAX_SLOW_PERCENTAGE = 0.2;
public const MAX_SLOW_PERCENTAGE = 0.1;

public const MAX_DAYS = 14;

Expand Down
4 changes: 2 additions & 2 deletions apps/settings/lib/SetupChecks/TaskProcessingSuccessRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use OCP\TaskProcessing\Task;

class TaskProcessingSuccessRate implements ISetupCheck {
public const MAX_FAILURE_PERCENTAGE = 0.2;
public const MAX_FAILURE_PERCENTAGE = 0.1;

public const MAX_DAYS = 14;

Expand All @@ -33,7 +33,7 @@ public function getCategory(): string {
}

public function getName(): string {
return $this->l10n->t('Task Processing pickup speed');
return $this->l10n->t('Task Processing success rate');
}

public function run(): SetupResult {
Expand Down
74 changes: 74 additions & 0 deletions apps/settings/lib/SetupChecks/TaskProcessingWorkerIsRunning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Settings\SetupChecks;

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\SetupCheck\ISetupCheck;
use OCP\SetupCheck\SetupResult;
use OCP\TaskProcessing\IManager;

class TaskProcessingWorkerIsRunning implements ISetupCheck {

public const HAS_TASKS_IN_LAST_X_DAYS = 7;
public const IS_RUNNING_IN_LAST_X_MINUTES = 5;

public function __construct(
private readonly IL10N $l10n,
private readonly IManager $taskProcessingManager,
private readonly ITimeFactory $timeFactory,
private readonly IAppConfig $appConfig,
) {
}

#[\Override]
public function getCategory(): string {
return 'ai';
}

#[\Override]
public function getName(): string {
return $this->l10n->t('Task Processing worker status');
}

#[\Override]
public function run(): SetupResult {
$lastNDays = self::HAS_TASKS_IN_LAST_X_DAYS;
$tasks = $this->taskProcessingManager->getTasks(userId: '', scheduleAfter: $this->timeFactory->now()->getTimestamp() - (60 * 60 * 24 * $lastNDays));
$taskCount = count($tasks);
if ($taskCount === 0) {
// In case taskprocessing is not used at all
return SetupResult::success(
$this->l10n->n(
'No scheduled tasks in the last day.',
'No scheduled tasks in the last %n days.',
$lastNDays
)
);
}
$lastIteration = (int)$this->appConfig->getValueString('core', 'ai.taskprocessing_worker_last_iteration', lazy: true);
if ($lastIteration > $this->timeFactory->now()->getTimestamp() - (60 * self::IS_RUNNING_IN_LAST_X_MINUTES)) {
return SetupResult::success(
$this->l10n->n('The Task Processing worker has run in the last minute.', 'The Task Processing worker has run in the last %n minutes.', self::IS_RUNNING_IN_LAST_X_MINUTES)
);
}

if ($lastIteration > 0) {
return SetupResult::warning(
$this->l10n->t('The Task Processing worker does not seem to be running. The last run was at %s.', [date('Y-m-d H:i:s', $lastIteration)])
);
}

return SetupResult::warning(
$this->l10n->t('The Task Processing worker does not seem to be running. It seems it has never run so far.')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public function testPass(): void {
for ($i = 0; $i < 100; $i++) {
$task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
$task->setStartedAt(0);
if ($i < 15) {
$task->setScheduledAt(60 * 5); // 15% get 5mins
if ($i < 5) {
$task->setScheduledAt(60 * 5); // 5% get 5mins
} else {
$task->setScheduledAt(60); // the rest gets 1min
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public function testPass(): void {
$task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
$task->setStartedAt(0);
$task->setEndedAt(1);
if ($i < 15) {
$task->setStatus(Task::STATUS_FAILED); // 15% get status FAILED
if ($i < 5) {
$task->setStatus(Task::STATUS_FAILED); // 5% get status FAILED
} else {
$task->setStatus(Task::STATUS_SUCCESSFUL);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Settings\Tests;

use OCA\Settings\SetupChecks\TaskProcessingWorkerIsRunning;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\SetupCheck\SetupResult;
use OCP\TaskProcessing\IManager;
use OCP\TaskProcessing\Task;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class TaskProcessingWorkerIsRunningTest extends TestCase {
private IL10N&MockObject $l10n;
private ITimeFactory&MockObject $timeFactory;
private IManager&MockObject $taskProcessingManager;
private IAppConfig&MockObject $appConfig;

private TaskProcessingWorkerIsRunning $check;

protected function setUp(): void {
parent::setUp();

$this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
$this->timeFactory = $this->getMockBuilder(ITimeFactory::class)->getMock();
$this->taskProcessingManager = $this->getMockBuilder(IManager::class)->getMock();
$this->appConfig = $this->getMockBuilder(IAppConfig::class)->getMock();

$this->check = new TaskProcessingWorkerIsRunning(
$this->l10n,
$this->taskProcessingManager,
$this->timeFactory,
$this->appConfig
);
}

public function testPass(): void {
$tasks = [];
for ($i = 0; $i < 10; $i++) {
$task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
$task->setStartedAt($this->timeFactory->now()->getTimestamp());
$task->setScheduledAt($this->timeFactory->now()->getTimestamp());
$task->setEndedAt($this->timeFactory->now()->getTimestamp());
$task->setStatus(Task::STATUS_SUCCESSFUL);
$tasks[] = $task;
}
$this->taskProcessingManager->method('getTasks')->willReturn($tasks);
$this->timeFactory->method('now')->willReturn(new \DateTimeImmutable());
$this->appConfig->method('getValueString')->willReturn((string)$this->timeFactory->now()->getTimestamp());

$this->assertEquals(SetupResult::SUCCESS, $this->check->run()->getSeverity());
}

public function testFail(): void {
$tasks = [];
for ($i = 0; $i < 10; $i++) {
$task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
$task->setStartedAt($this->timeFactory->now()->getTimestamp());
$task->setScheduledAt($this->timeFactory->now()->getTimestamp());
$task->setEndedAt($this->timeFactory->now()->getTimestamp());
$task->setStatus(Task::STATUS_SUCCESSFUL);
$tasks[] = $task;
}
$this->taskProcessingManager->method('getTasks')->willReturn($tasks);
$this->timeFactory->method('now')->willReturn(new \DateTimeImmutable());
$this->appConfig->method('getValueString')->willReturn((string)($this->timeFactory->now()->getTimestamp() - 60 * 10));

$this->assertEquals(SetupResult::WARNING, $this->check->run()->getSeverity());
}
}
15 changes: 12 additions & 3 deletions core/Command/TaskProcessing/WorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

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;
use OCP\TaskProcessing\IManager;
Expand All @@ -23,6 +25,8 @@ class WorkerCommand extends Base {
public function __construct(
private readonly IManager $taskProcessingManager,
private readonly LoggerInterface $logger,
private readonly IAppConfig $appConfig,
private readonly ITimeFactory $timeFactory,
) {
parent::__construct();
}
Expand Down Expand Up @@ -61,32 +65,37 @@ protected function configure(): void {
}

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<string> $taskTypes */
$taskTypes = $input->getOption('taskTypes');
$lastConfigStorageTime = 0;

if ($timeout > 0) {
$output->writeln('<info>Task processing worker will stop after ' . $timeout . ' seconds</info>');
}

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;
}

// Handle SIGTERM/SIGINT gracefully
try {
$this->abortIfInterrupted();
} catch (InterruptedException $e) {
} catch (InterruptedException) {
$output->writeln('<info>Task processing worker stopped</info>');
break;
}

if ($lastConfigStorageTime < $this->timeFactory->now()->getTimestamp() - 60) {
$this->appConfig->setValueString('core', 'ai.taskprocessing_worker_last_iteration', (string)$this->timeFactory->now()->getTimestamp(), lazy: true);
$lastConfigStorageTime = $this->timeFactory->now()->getTimestamp();
}
$processedTask = $this->processNextTask($output, $taskTypes);

if ($once) {
Expand Down
9 changes: 8 additions & 1 deletion tests/Core/Command/TaskProcessing/WorkerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
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;
use OCP\TaskProcessing\IManager;
Expand All @@ -24,14 +26,19 @@
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 {
parent::setUp();

$this->manager = $this->createMock(IManager::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->command = new WorkerCommand($this->manager, $this->logger);
$this->appConfig = $this->createMock(IAppConfig::class);
$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);
}

/**
Expand Down
Loading