Skip to content

Commit 1015b7c

Browse files
authored
Merge pull request #53001 from nextcloud/feat/setupcheck-task-pickup-speed
feat(SetupChecks): Add check for TaskProcessing pickup speed
2 parents a48bc55 + f8f2695 commit 1015b7c

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

apps/settings/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
'OCA\\Settings\\SetupChecks\\SecurityHeaders' => $baseDir . '/../lib/SetupChecks/SecurityHeaders.php',
130130
'OCA\\Settings\\SetupChecks\\SupportedDatabase' => $baseDir . '/../lib/SetupChecks/SupportedDatabase.php',
131131
'OCA\\Settings\\SetupChecks\\SystemIs64bit' => $baseDir . '/../lib/SetupChecks/SystemIs64bit.php',
132+
'OCA\\Settings\\SetupChecks\\TaskProcessingPickupSpeed' => $baseDir . '/../lib/SetupChecks/TaskProcessingPickupSpeed.php',
132133
'OCA\\Settings\\SetupChecks\\TempSpaceAvailable' => $baseDir . '/../lib/SetupChecks/TempSpaceAvailable.php',
133134
'OCA\\Settings\\SetupChecks\\TransactionIsolation' => $baseDir . '/../lib/SetupChecks/TransactionIsolation.php',
134135
'OCA\\Settings\\SetupChecks\\WellKnownUrls' => $baseDir . '/../lib/SetupChecks/WellKnownUrls.php',

apps/settings/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class ComposerStaticInitSettings
144144
'OCA\\Settings\\SetupChecks\\SecurityHeaders' => __DIR__ . '/..' . '/../lib/SetupChecks/SecurityHeaders.php',
145145
'OCA\\Settings\\SetupChecks\\SupportedDatabase' => __DIR__ . '/..' . '/../lib/SetupChecks/SupportedDatabase.php',
146146
'OCA\\Settings\\SetupChecks\\SystemIs64bit' => __DIR__ . '/..' . '/../lib/SetupChecks/SystemIs64bit.php',
147+
'OCA\\Settings\\SetupChecks\\TaskProcessingPickupSpeed' => __DIR__ . '/..' . '/../lib/SetupChecks/TaskProcessingPickupSpeed.php',
147148
'OCA\\Settings\\SetupChecks\\TempSpaceAvailable' => __DIR__ . '/..' . '/../lib/SetupChecks/TempSpaceAvailable.php',
148149
'OCA\\Settings\\SetupChecks\\TransactionIsolation' => __DIR__ . '/..' . '/../lib/SetupChecks/TransactionIsolation.php',
149150
'OCA\\Settings\\SetupChecks\\WellKnownUrls' => __DIR__ . '/..' . '/../lib/SetupChecks/WellKnownUrls.php',

apps/settings/lib/AppInfo/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
use OCA\Settings\SetupChecks\SecurityHeaders;
7171
use OCA\Settings\SetupChecks\SupportedDatabase;
7272
use OCA\Settings\SetupChecks\SystemIs64bit;
73+
use OCA\Settings\SetupChecks\TaskProcessingPickupSpeed;
7374
use OCA\Settings\SetupChecks\TempSpaceAvailable;
7475
use OCA\Settings\SetupChecks\TransactionIsolation;
7576
use OCA\Settings\SetupChecks\WellKnownUrls;
@@ -206,6 +207,7 @@ public function register(IRegistrationContext $context): void {
206207
$context->registerSetupCheck(SchedulingTableSize::class);
207208
$context->registerSetupCheck(SupportedDatabase::class);
208209
$context->registerSetupCheck(SystemIs64bit::class);
210+
$context->registerSetupCheck(TaskProcessingPickupSpeed::class);
209211
$context->registerSetupCheck(TempSpaceAvailable::class);
210212
$context->registerSetupCheck(TransactionIsolation::class);
211213
$context->registerSetupCheck(PushService::class);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 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\IL10N;
14+
use OCP\SetupCheck\ISetupCheck;
15+
use OCP\SetupCheck\SetupResult;
16+
use OCP\TaskProcessing\IManager;
17+
18+
class TaskProcessingPickupSpeed implements ISetupCheck {
19+
public const MAX_SLOW_PERCENTAGE = 0.2;
20+
public const TIME_SPAN = 24;
21+
22+
public function __construct(
23+
private IL10N $l10n,
24+
private IManager $taskProcessingManager,
25+
private ITimeFactory $timeFactory,
26+
) {
27+
}
28+
29+
public function getCategory(): string {
30+
return 'ai';
31+
}
32+
33+
public function getName(): string {
34+
return $this->l10n->t('Task Processing pickup speed');
35+
}
36+
37+
public function run(): SetupResult {
38+
$tasks = $this->taskProcessingManager->getTasks(userId: '', scheduleAfter: $this->timeFactory->now()->getTimestamp() - 60 * 60 * self::TIME_SPAN); // userId: '' means no filter, whereas null would mean guest
39+
$taskCount = count($tasks);
40+
if ($taskCount === 0) {
41+
return SetupResult::success($this->l10n->t('No scheduled tasks in the last {hours} hours.', ['hours' => self::TIME_SPAN]));
42+
}
43+
$slowCount = 0;
44+
foreach ($tasks as $task) {
45+
if ($task->getStartedAt() === null) {
46+
continue; // task was not picked up yet
47+
}
48+
if ($task->getScheduledAt() === null) {
49+
continue; // task was not scheduled yet -- should not happen, but the API specifies null as return value
50+
}
51+
$pickupDelay = $task->getScheduledAt() - $task->getStartedAt();
52+
if ($pickupDelay > 60 * 4) {
53+
$slowCount++; // task pickup took longer than 4 minutes
54+
}
55+
}
56+
57+
if ($slowCount / $taskCount < self::MAX_SLOW_PERCENTAGE) {
58+
return SetupResult::success($this->l10n->t('Task pickup speed is ok in the last {hours} hours.', ['hours' => self::TIME_SPAN]));
59+
} else {
60+
return SetupResult::warning($this->l10n->t('Task pickup speed is slow in the last {hours} hours. Many tasks took longer than 4 min to get picked up. Consider setting up a worker to process tasks in the background.', ['hours' => self::TIME_SPAN]), 'https://docs.nextcloud.com/server/latest/admin_manual/ai/overview.html#improve-ai-task-pickup-speed');
61+
}
62+
}
63+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 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\TaskProcessingPickupSpeed;
12+
use OCP\AppFramework\Utility\ITimeFactory;
13+
use OCP\IL10N;
14+
use OCP\SetupCheck\SetupResult;
15+
use OCP\TaskProcessing\IManager;
16+
use OCP\TaskProcessing\Task;
17+
use Test\TestCase;
18+
19+
class TaskProcessingPickupSpeedTest extends TestCase {
20+
private IL10N $l10n;
21+
private ITimeFactory $timeFactory;
22+
private IManager $taskProcessingManager;
23+
24+
private TaskProcessingPickupSpeed $check;
25+
26+
protected function setUp(): void {
27+
parent::setUp();
28+
29+
$this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
30+
$this->timeFactory = $this->getMockBuilder(ITimeFactory::class)->getMock();
31+
$this->taskProcessingManager = $this->getMockBuilder(IManager::class)->getMock();
32+
33+
$this->check = new TaskProcessingPickupSpeed(
34+
$this->l10n,
35+
$this->taskProcessingManager,
36+
$this->timeFactory,
37+
);
38+
}
39+
40+
public function testPass(): void {
41+
$tasks = [];
42+
for ($i = 0; $i < 100; $i++) {
43+
$task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
44+
$task->setStartedAt(0);
45+
if ($i < 15) {
46+
$task->setScheduledAt(60 * 5); // 15% get 5mins
47+
} else {
48+
$task->setScheduledAt(60); // the rest gets 1min
49+
}
50+
$tasks[] = $task;
51+
}
52+
$this->taskProcessingManager->method('getTasks')->willReturn($tasks);
53+
54+
$this->assertEquals(SetupResult::SUCCESS, $this->check->run()->getSeverity());
55+
}
56+
57+
public function testFail(): void {
58+
$tasks = [];
59+
for ($i = 0; $i < 100; $i++) {
60+
$task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i);
61+
$task->setStartedAt(0);
62+
if ($i < 30) {
63+
$task->setScheduledAt(60 * 5); // 30% get 5mins
64+
} else {
65+
$task->setScheduledAt(60); // the rest gets 1min
66+
}
67+
$tasks[] = $task;
68+
}
69+
$this->taskProcessingManager->method('getTasks')->willReturn($tasks);
70+
71+
$this->assertEquals(SetupResult::WARNING, $this->check->run()->getSeverity());
72+
}
73+
}

0 commit comments

Comments
 (0)