-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathTaskProcessingPickupSpeed.php
More file actions
89 lines (78 loc) · 2.63 KB
/
TaskProcessingPickupSpeed.php
File metadata and controls
89 lines (78 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?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.1;
public const MAX_DAYS = 14;
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 {
$taskCount = 0;
$lastNDays = 1;
while ($taskCount === 0 && $lastNDays < self::MAX_DAYS) {
$lastNDays++;
// userId: '' means no filter, whereas null would mean guest
$tasks = $this->taskProcessingManager->getTasks(userId: '', scheduleAfter: $this->timeFactory->now()->getTimestamp() - (60 * 60 * 24 * $lastNDays));
$taskCount = count($tasks);
}
if ($taskCount === 0) {
return SetupResult::success(
$this->l10n->n(
'No scheduled tasks in the last day.',
'No scheduled tasks in the last %n days.',
$lastNDays
)
);
}
$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->n(
'The task pickup speed has been ok in the last day.',
'The task pickup speed has been ok in the last %n days.',
$lastNDays
)
);
} else {
return SetupResult::warning(
$this->l10n->n(
'The task pickup speed has been slow in the last day. Many tasks took longer than 4 minutes to be picked up. Consider setting up a worker to process tasks in the background.',
'The task pickup speed has been slow in the last %n days. Many tasks took longer than 4 minutes to be picked up. Consider setting up a worker to process tasks in the background.',
$lastNDays
),
'https://docs.nextcloud.com/server/latest/admin_manual/ai/overview.html#improve-ai-task-pickup-speed'
);
}
}
}