2025-05-20 12:40:23 -04:00
< ? 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 ;
2025-10-13 08:06:31 -04:00
public const MAX_DAYS = 14 ;
2025-05-20 12:40:23 -04:00
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 {
2025-10-13 08:06:31 -04:00
$taskCount = 0 ;
$lastNDays = 1 ;
2025-10-13 08:44:23 -04:00
while ( $taskCount === 0 && $lastNDays < self :: MAX_DAYS ) {
2025-10-13 08:06:31 -04:00
$lastNDays ++ ;
2025-10-14 05:52:37 -04:00
// userId: '' means no filter, whereas null would mean guest
$tasks = $this -> taskProcessingManager -> getTasks ( userId : '' , scheduleAfter : $this -> timeFactory -> now () -> getTimestamp () - ( 60 * 60 * 24 * $lastNDays ));
2025-10-13 08:06:31 -04:00
$taskCount = count ( $tasks );
}
2025-05-20 12:40:23 -04:00
if ( $taskCount === 0 ) {
2025-10-13 08:06:31 -04:00
return SetupResult :: success (
$this -> l10n -> n (
2025-10-14 02:42:56 -04:00
'No scheduled tasks in the last day.' ,
'No scheduled tasks in the last %n days.' ,
$lastNDays
2025-10-13 08:06:31 -04:00
)
);
2025-05-20 12:40:23 -04:00
}
$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
}
}
2025-10-14 05:52:49 -04:00
if (( $slowCount / $taskCount ) < self :: MAX_SLOW_PERCENTAGE ) {
2025-10-13 08:06:31 -04:00
return SetupResult :: success (
$this -> l10n -> n (
2025-10-14 02:42:56 -04:00
'The task pickup speed has been ok in the last day.' ,
'The task pickup speed has been ok in the last %n days.' ,
$lastNDays
2025-10-13 08:06:31 -04:00
)
);
2025-05-20 12:40:23 -04:00
} else {
2025-10-13 08:06:31 -04:00
return SetupResult :: warning (
$this -> l10n -> n (
2025-10-14 02:42:56 -04:00
'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
2025-10-13 08:06:31 -04:00
),
'https://docs.nextcloud.com/server/latest/admin_manual/ai/overview.html#improve-ai-task-pickup-speed'
);
2025-05-20 12:40:23 -04:00
}
}
}