2026-04-28 05:43:03 -04:00
< ? php
declare ( strict_types = 1 );
/**
* SPDX - FileCopyrightText : 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX - License - Identifier : AGPL - 3.0 - or - later
*/
namespace OCA\Settings\SetupChecks ;
use OCP\AppFramework\Utility\ITimeFactory ;
use OCP\IAppConfig ;
use OCP\IL10N ;
use OCP\SetupCheck\ISetupCheck ;
use OCP\SetupCheck\SetupResult ;
use OCP\TaskProcessing\IManager ;
class TaskProcessingWorkerIsRunning implements ISetupCheck {
public const HAS_TASKS_IN_LAST_X_DAYS = 7 ;
public const IS_RUNNING_IN_LAST_X_MINUTES = 5 ;
public function __construct (
private readonly IL10N $l10n ,
private readonly IManager $taskProcessingManager ,
private readonly ITimeFactory $timeFactory ,
private readonly IAppConfig $appConfig ,
) {
}
2026-04-29 04:59:32 -04:00
#[\Override]
2026-04-28 05:43:03 -04:00
public function getCategory () : string {
return 'ai' ;
}
2026-04-29 04:59:32 -04:00
#[\Override]
2026-04-28 05:43:03 -04:00
public function getName () : string {
return $this -> l10n -> t ( 'Task Processing worker status' );
}
2026-04-29 04:59:32 -04:00
#[\Override]
2026-04-28 05:43:03 -04:00
public function run () : SetupResult {
$lastNDays = self :: HAS_TASKS_IN_LAST_X_DAYS ;
$tasks = $this -> taskProcessingManager -> getTasks ( userId : '' , scheduleAfter : $this -> timeFactory -> now () -> getTimestamp () - ( 60 * 60 * 24 * $lastNDays ));
$taskCount = count ( $tasks );
if ( $taskCount === 0 ) {
// In case taskprocessing is not used at all
return SetupResult :: success (
$this -> l10n -> n (
'No scheduled tasks in the last day.' ,
'No scheduled tasks in the last %n days.' ,
$lastNDays
)
);
}
2026-04-29 04:11:25 -04:00
$lastIteration = ( int ) $this -> appConfig -> getValueString ( 'core' , 'ai.taskprocessing_worker_last_iteration' , lazy : true );
2026-04-28 06:51:34 -04:00
if ( $lastIteration > $this -> timeFactory -> now () -> getTimestamp () - ( 60 * self :: IS_RUNNING_IN_LAST_X_MINUTES )) {
2026-04-28 05:43:03 -04:00
return SetupResult :: success (
2026-04-28 09:18:06 -04:00
$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 )
2026-04-28 05:43:03 -04:00
);
}
2026-04-28 09:18:06 -04:00
if ( $lastIteration > 0 ) {
return SetupResult :: warning (
$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 )])
);
}
2026-04-28 05:43:03 -04:00
return SetupResult :: warning (
2026-04-28 09:18:06 -04:00
$this -> l10n -> t ( 'The Task Processing worker does not seem to be running. It seems it has never run so far.' )
2026-04-28 05:43:03 -04:00
);
}
}