-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat(SetupChecks): Add check for TaskProcessing pickup speed #53001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
apps/settings/lib/SetupChecks/TaskProcessingPickupSpeed.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Settings\SetupChecks; | ||
|
|
||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\IL10N; | ||
| use OCP\SetupCheck\ISetupCheck; | ||
| use OCP\SetupCheck\SetupResult; | ||
| use OCP\TaskProcessing\IManager; | ||
|
|
||
| class TaskProcessingPickupSpeed implements ISetupCheck { | ||
| public const MAX_SLOW_PERCENTAGE = 0.2; | ||
| public const TIME_SPAN = 24; | ||
|
|
||
| public function __construct( | ||
| private IL10N $l10n, | ||
| private IManager $taskProcessingManager, | ||
| private ITimeFactory $timeFactory, | ||
| ) { | ||
| } | ||
|
|
||
| public function getCategory(): string { | ||
| return 'ai'; | ||
| } | ||
|
|
||
| public function getName(): string { | ||
| return $this->l10n->t('Task Processing pickup speed'); | ||
| } | ||
|
|
||
| public function run(): SetupResult { | ||
| $tasks = $this->taskProcessingManager->getTasks(userId: '', scheduleAfter: $this->timeFactory->now()->getTimestamp() - 60 * 60 * self::TIME_SPAN); // userId: '' means no filter, whereas null would mean guest | ||
| $taskCount = count($tasks); | ||
| if ($taskCount === 0) { | ||
| return SetupResult::success($this->l10n->t('No scheduled tasks in the last {hours} hours.', ['hours' => self::TIME_SPAN])); | ||
| } | ||
| $slowCount = 0; | ||
| foreach ($tasks as $task) { | ||
| if ($task->getStartedAt() === null) { | ||
| continue; // task was not picked up yet | ||
| } | ||
| if ($task->getScheduledAt() === null) { | ||
| continue; // task was not scheduled yet -- should not happen, but the API specifies null as return value | ||
| } | ||
| $pickupDelay = $task->getScheduledAt() - $task->getStartedAt(); | ||
| if ($pickupDelay > 60 * 4) { | ||
| $slowCount++; // task pickup took longer than 4 minutes | ||
| } | ||
| } | ||
|
|
||
| if ($slowCount / $taskCount < self::MAX_SLOW_PERCENTAGE) { | ||
| return SetupResult::success($this->l10n->t('Task pickup speed is ok in the last {hours} hours.', ['hours' => self::TIME_SPAN])); | ||
| } else { | ||
| 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'); | ||
| } | ||
| } | ||
| } |
73 changes: 73 additions & 0 deletions
73
apps/settings/tests/SetupChecks/TaskProcessingPickupSpeedTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\Settings\Tests; | ||
|
|
||
| use OCA\Settings\SetupChecks\TaskProcessingPickupSpeed; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\IL10N; | ||
| use OCP\SetupCheck\SetupResult; | ||
| use OCP\TaskProcessing\IManager; | ||
| use OCP\TaskProcessing\Task; | ||
| use Test\TestCase; | ||
|
|
||
| class TaskProcessingPickupSpeedTest extends TestCase { | ||
| private IL10N $l10n; | ||
| private ITimeFactory $timeFactory; | ||
| private IManager $taskProcessingManager; | ||
|
|
||
| private TaskProcessingPickupSpeed $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->check = new TaskProcessingPickupSpeed( | ||
| $this->l10n, | ||
| $this->taskProcessingManager, | ||
| $this->timeFactory, | ||
| ); | ||
| } | ||
|
|
||
| public function testPass(): void { | ||
| $tasks = []; | ||
| 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 | ||
| } else { | ||
| $task->setScheduledAt(60); // the rest gets 1min | ||
| } | ||
| $tasks[] = $task; | ||
| } | ||
| $this->taskProcessingManager->method('getTasks')->willReturn($tasks); | ||
|
|
||
| $this->assertEquals(SetupResult::SUCCESS, $this->check->run()->getSeverity()); | ||
| } | ||
|
|
||
| public function testFail(): void { | ||
| $tasks = []; | ||
| for ($i = 0; $i < 100; $i++) { | ||
| $task = new Task('test', ['test' => 'test'], 'settings', 'user' . $i); | ||
| $task->setStartedAt(0); | ||
| if ($i < 30) { | ||
| $task->setScheduledAt(60 * 5); // 30% get 5mins | ||
| } else { | ||
| $task->setScheduledAt(60); // the rest gets 1min | ||
| } | ||
| $tasks[] = $task; | ||
| } | ||
| $this->taskProcessingManager->method('getTasks')->willReturn($tasks); | ||
|
|
||
| $this->assertEquals(SetupResult::WARNING, $this->check->run()->getSeverity()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏