Skip to content

Commit 840c71b

Browse files
authored
Merge pull request #59990 from nextcloud/backport/59958/stable32
[stable32] Feat: Better reporting if something is wrong with taskprocessing
2 parents dbab25c + 98a1f1b commit 840c71b

11 files changed

Lines changed: 185 additions & 11 deletions

File tree

apps/settings/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
'OCA\\Settings\\SetupChecks\\SystemIs64bit' => $baseDir . '/../lib/SetupChecks/SystemIs64bit.php',
137137
'OCA\\Settings\\SetupChecks\\TaskProcessingPickupSpeed' => $baseDir . '/../lib/SetupChecks/TaskProcessingPickupSpeed.php',
138138
'OCA\\Settings\\SetupChecks\\TaskProcessingSuccessRate' => $baseDir . '/../lib/SetupChecks/TaskProcessingSuccessRate.php',
139+
'OCA\\Settings\\SetupChecks\\TaskProcessingWorkerIsRunning' => $baseDir . '/../lib/SetupChecks/TaskProcessingWorkerIsRunning.php',
139140
'OCA\\Settings\\SetupChecks\\TempSpaceAvailable' => $baseDir . '/../lib/SetupChecks/TempSpaceAvailable.php',
140141
'OCA\\Settings\\SetupChecks\\TransactionIsolation' => $baseDir . '/../lib/SetupChecks/TransactionIsolation.php',
141142
'OCA\\Settings\\SetupChecks\\TwoFactorConfiguration' => $baseDir . '/../lib/SetupChecks/TwoFactorConfiguration.php',

apps/settings/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class ComposerStaticInitSettings
151151
'OCA\\Settings\\SetupChecks\\SystemIs64bit' => __DIR__ . '/..' . '/../lib/SetupChecks/SystemIs64bit.php',
152152
'OCA\\Settings\\SetupChecks\\TaskProcessingPickupSpeed' => __DIR__ . '/..' . '/../lib/SetupChecks/TaskProcessingPickupSpeed.php',
153153
'OCA\\Settings\\SetupChecks\\TaskProcessingSuccessRate' => __DIR__ . '/..' . '/../lib/SetupChecks/TaskProcessingSuccessRate.php',
154+
'OCA\\Settings\\SetupChecks\\TaskProcessingWorkerIsRunning' => __DIR__ . '/..' . '/../lib/SetupChecks/TaskProcessingWorkerIsRunning.php',
154155
'OCA\\Settings\\SetupChecks\\TempSpaceAvailable' => __DIR__ . '/..' . '/../lib/SetupChecks/TempSpaceAvailable.php',
155156
'OCA\\Settings\\SetupChecks\\TransactionIsolation' => __DIR__ . '/..' . '/../lib/SetupChecks/TransactionIsolation.php',
156157
'OCA\\Settings\\SetupChecks\\TwoFactorConfiguration' => __DIR__ . '/..' . '/../lib/SetupChecks/TwoFactorConfiguration.php',

apps/settings/lib/AppInfo/Application.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
use OCA\Settings\SetupChecks\SupportedDatabase;
7373
use OCA\Settings\SetupChecks\SystemIs64bit;
7474
use OCA\Settings\SetupChecks\TaskProcessingPickupSpeed;
75+
use OCA\Settings\SetupChecks\TaskProcessingSuccessRate;
76+
use OCA\Settings\SetupChecks\TaskProcessingWorkerIsRunning;
7577
use OCA\Settings\SetupChecks\TempSpaceAvailable;
7678
use OCA\Settings\SetupChecks\TransactionIsolation;
7779
use OCA\Settings\SetupChecks\TwoFactorConfiguration;
@@ -212,6 +214,8 @@ public function register(IRegistrationContext $context): void {
212214
$context->registerSetupCheck(SupportedDatabase::class);
213215
$context->registerSetupCheck(SystemIs64bit::class);
214216
$context->registerSetupCheck(TaskProcessingPickupSpeed::class);
217+
$context->registerSetupCheck(TaskProcessingSuccessRate::class);
218+
$context->registerSetupCheck(TaskProcessingWorkerIsRunning::class);
215219
$context->registerSetupCheck(TempSpaceAvailable::class);
216220
$context->registerSetupCheck(TransactionIsolation::class);
217221
$context->registerSetupCheck(TwoFactorConfiguration::class);

apps/settings/lib/SetupChecks/TaskProcessingPickupSpeed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use OCP\TaskProcessing\IManager;
1717

1818
class TaskProcessingPickupSpeed implements ISetupCheck {
19-
public const MAX_SLOW_PERCENTAGE = 0.2;
19+
public const MAX_SLOW_PERCENTAGE = 0.1;
2020

2121
public const MAX_DAYS = 14;
2222

apps/settings/lib/SetupChecks/TaskProcessingSuccessRate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use OCP\TaskProcessing\Task;
1818

1919
class TaskProcessingSuccessRate implements ISetupCheck {
20-
public const MAX_FAILURE_PERCENTAGE = 0.2;
20+
public const MAX_FAILURE_PERCENTAGE = 0.1;
2121

2222
public const MAX_DAYS = 14;
2323

@@ -33,7 +33,7 @@ public function getCategory(): string {
3333
}
3434

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

3939
public function run(): SetupResult {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Settings\SetupChecks;
11+
12+
use OCP\AppFramework\Utility\ITimeFactory;
13+
use OCP\IAppConfig;
14+
use OCP\IL10N;
15+
use OCP\SetupCheck\ISetupCheck;
16+
use OCP\SetupCheck\SetupResult;
17+
use OCP\TaskProcessing\IManager;
18+
19+
class TaskProcessingWorkerIsRunning implements ISetupCheck {
20+
21+
public const HAS_TASKS_IN_LAST_X_DAYS = 7;
22+
public const IS_RUNNING_IN_LAST_X_MINUTES = 5;
23+
24+
public function __construct(
25+
private readonly IL10N $l10n,
26+
private readonly IManager $taskProcessingManager,
27+
private readonly ITimeFactory $timeFactory,
28+
private readonly IAppConfig $appConfig,
29+
) {
30+
}
31+
32+
#[\Override]
33+
public function getCategory(): string {
34+
return 'ai';
35+
}
36+
37+
#[\Override]
38+
public function getName(): string {
39+
return $this->l10n->t('Task Processing worker status');
40+
}
41+
42+
#[\Override]
43+
public function run(): SetupResult {
44+
$lastNDays = self::HAS_TASKS_IN_LAST_X_DAYS;
45+
$tasks = $this->taskProcessingManager->getTasks(userId: '', scheduleAfter: $this->timeFactory->now()->getTimestamp() - (60 * 60 * 24 * $lastNDays));
46+
$taskCount = count($tasks);
47+
if ($taskCount === 0) {
48+
// In case taskprocessing is not used at all
49+
return SetupResult::success(
50+
$this->l10n->n(
51+
'No scheduled tasks in the last day.',
52+
'No scheduled tasks in the last %n days.',
53+
$lastNDays
54+
)
55+
);
56+
}
57+
$lastIteration = (int)$this->appConfig->getValueString('core', 'ai.taskprocessing_worker_last_iteration', lazy: true);
58+
if ($lastIteration > $this->timeFactory->now()->getTimestamp() - (60 * self::IS_RUNNING_IN_LAST_X_MINUTES)) {
59+
return SetupResult::success(
60+
$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)
61+
);
62+
}
63+
64+
if ($lastIteration > 0) {
65+
return SetupResult::warning(
66+
$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)])
67+
);
68+
}
69+
70+
return SetupResult::warning(
71+
$this->l10n->t('The Task Processing worker does not seem to be running. It seems it has never run so far.')
72+
);
73+
}
74+
}

apps/settings/tests/SetupChecks/TaskProcessingPickupSpeedTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public function testPass(): void {
4242
for ($i = 0; $i < 100; $i++) {
4343
$task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
4444
$task->setStartedAt(0);
45-
if ($i < 15) {
46-
$task->setScheduledAt(60 * 5); // 15% get 5mins
45+
if ($i < 5) {
46+
$task->setScheduledAt(60 * 5); // 5% get 5mins
4747
} else {
4848
$task->setScheduledAt(60); // the rest gets 1min
4949
}

apps/settings/tests/SetupChecks/TaskProcessingSuccessRateTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public function testPass(): void {
4343
$task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
4444
$task->setStartedAt(0);
4545
$task->setEndedAt(1);
46-
if ($i < 15) {
47-
$task->setStatus(Task::STATUS_FAILED); // 15% get status FAILED
46+
if ($i < 5) {
47+
$task->setStatus(Task::STATUS_FAILED); // 5% get status FAILED
4848
} else {
4949
$task->setStatus(Task::STATUS_SUCCESSFUL);
5050
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
namespace OCA\Settings\Tests;
10+
11+
use OCA\Settings\SetupChecks\TaskProcessingWorkerIsRunning;
12+
use OCP\AppFramework\Utility\ITimeFactory;
13+
use OCP\IAppConfig;
14+
use OCP\IL10N;
15+
use OCP\SetupCheck\SetupResult;
16+
use OCP\TaskProcessing\IManager;
17+
use OCP\TaskProcessing\Task;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use Test\TestCase;
20+
21+
class TaskProcessingWorkerIsRunningTest extends TestCase {
22+
private IL10N&MockObject $l10n;
23+
private ITimeFactory&MockObject $timeFactory;
24+
private IManager&MockObject $taskProcessingManager;
25+
private IAppConfig&MockObject $appConfig;
26+
27+
private TaskProcessingWorkerIsRunning $check;
28+
29+
protected function setUp(): void {
30+
parent::setUp();
31+
32+
$this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
33+
$this->timeFactory = $this->getMockBuilder(ITimeFactory::class)->getMock();
34+
$this->taskProcessingManager = $this->getMockBuilder(IManager::class)->getMock();
35+
$this->appConfig = $this->getMockBuilder(IAppConfig::class)->getMock();
36+
37+
$this->check = new TaskProcessingWorkerIsRunning(
38+
$this->l10n,
39+
$this->taskProcessingManager,
40+
$this->timeFactory,
41+
$this->appConfig
42+
);
43+
}
44+
45+
public function testPass(): void {
46+
$tasks = [];
47+
for ($i = 0; $i < 10; $i++) {
48+
$task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
49+
$task->setStartedAt($this->timeFactory->now()->getTimestamp());
50+
$task->setScheduledAt($this->timeFactory->now()->getTimestamp());
51+
$task->setEndedAt($this->timeFactory->now()->getTimestamp());
52+
$task->setStatus(Task::STATUS_SUCCESSFUL);
53+
$tasks[] = $task;
54+
}
55+
$this->taskProcessingManager->method('getTasks')->willReturn($tasks);
56+
$this->timeFactory->method('now')->willReturn(new \DateTimeImmutable());
57+
$this->appConfig->method('getValueString')->willReturn((string)$this->timeFactory->now()->getTimestamp());
58+
59+
$this->assertEquals(SetupResult::SUCCESS, $this->check->run()->getSeverity());
60+
}
61+
62+
public function testFail(): void {
63+
$tasks = [];
64+
for ($i = 0; $i < 10; $i++) {
65+
$task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
66+
$task->setStartedAt($this->timeFactory->now()->getTimestamp());
67+
$task->setScheduledAt($this->timeFactory->now()->getTimestamp());
68+
$task->setEndedAt($this->timeFactory->now()->getTimestamp());
69+
$task->setStatus(Task::STATUS_SUCCESSFUL);
70+
$tasks[] = $task;
71+
}
72+
$this->taskProcessingManager->method('getTasks')->willReturn($tasks);
73+
$this->timeFactory->method('now')->willReturn(new \DateTimeImmutable());
74+
$this->appConfig->method('getValueString')->willReturn((string)($this->timeFactory->now()->getTimestamp() - 60 * 10));
75+
76+
$this->assertEquals(SetupResult::WARNING, $this->check->run()->getSeverity());
77+
}
78+
}

core/Command/TaskProcessing/WorkerCommand.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
use OC\Core\Command\Base;
1212
use OC\Core\Command\InterruptedException;
13+
use OCP\AppFramework\Utility\ITimeFactory;
14+
use OCP\IAppConfig;
1315
use OCP\TaskProcessing\Exception\Exception;
1416
use OCP\TaskProcessing\Exception\NotFoundException;
1517
use OCP\TaskProcessing\IManager;
@@ -23,6 +25,8 @@ class WorkerCommand extends Base {
2325
public function __construct(
2426
private readonly IManager $taskProcessingManager,
2527
private readonly LoggerInterface $logger,
28+
private readonly IAppConfig $appConfig,
29+
private readonly ITimeFactory $timeFactory,
2630
) {
2731
parent::__construct();
2832
}
@@ -61,32 +65,37 @@ protected function configure(): void {
6165
}
6266

6367
protected function execute(InputInterface $input, OutputInterface $output): int {
64-
$startTime = time();
68+
$startTime = $this->timeFactory->now()->getTimestamp();
6569
$timeout = (int)$input->getOption('timeout');
6670
$interval = (int)$input->getOption('interval');
6771
$once = $input->getOption('once') === true;
6872
/** @var list<string> $taskTypes */
6973
$taskTypes = $input->getOption('taskTypes');
74+
$lastConfigStorageTime = 0;
7075

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

7580
while (true) {
7681
// Stop if timeout exceeded
77-
if ($timeout > 0 && ($startTime + $timeout) < time()) {
82+
if ($timeout > 0 && ($startTime + $timeout) < $this->timeFactory->now()->getTimestamp()) {
7883
$output->writeln('Timeout reached, exiting...', OutputInterface::VERBOSITY_VERBOSE);
7984
break;
8085
}
8186

8287
// Handle SIGTERM/SIGINT gracefully
8388
try {
8489
$this->abortIfInterrupted();
85-
} catch (InterruptedException $e) {
90+
} catch (InterruptedException) {
8691
$output->writeln('<info>Task processing worker stopped</info>');
8792
break;
8893
}
8994

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

92101
if ($once) {

0 commit comments

Comments
 (0)