N°9104 - Be able to trigger cron remotely and asynchronously

This commit is contained in:
odain
2026-03-04 14:54:56 +01:00
parent 14d748aa97
commit fce1348d44
14 changed files with 424 additions and 65 deletions

100
webservices/CronService.php Normal file
View File

@@ -0,0 +1,100 @@
<?php
class CronService
{
public const HELP_MESSAGE = "php cron.php --auth_user=<login> --auth_pwd=<password> [--param_file=<file>] [--verbose=1] [--debug=1] [--status_only=1]";
private static CronService $oInstance;
//used for test only
private ?iTopMutex $oMutex = null;
protected function __construct()
{
}
final public static function GetInstance(): CronService
{
if (!isset(self::$oInstance)) {
self::$oInstance = new CronService();
}
return self::$oInstance;
}
final public static function SetInstance(?CronService $oInstance): void
{
self::$oInstance = $oInstance;
}
private function GetMutex(): iTopMutex
{
if (! is_null($this->oMutex)) {
return $this->oMutex;
}
return new iTopMutex('cron');
}
private function IsMutexLocked(): bool
{
return $this->GetMutex()->IsLocked();
}
public function IsStarted(string $sErrorFile): bool
{
$aLines = utils::ReadTail($sErrorFile);
$sLine = array_shift($aLines);
return (preg_match('/Starting: (.*)$/', $sLine));
}
public function IsStopped(string $sErrorFile): bool
{
$aLines = utils::ReadTail($sErrorFile);
$sLine = array_shift($aLines);
return (preg_match('/Exiting: (.*)$/', $sLine));
}
public function IsFailed(string $sLogFile): bool
{
return ! is_null($this->GetErrorMessage($sLogFile));
}
public function GetErrorMessage(string $sLogFile): ?string
{
$aLines = utils::ReadTail($sLogFile);
$sLine = array_shift($aLines);
if (preg_match('/ERROR: (.*)$/', $sLine, $aMatches)) {
return $aMatches[1];
}
if (preg_match('/Exiting: (.*)$/', $sLine, $aMatches)) {
//stopped
return null;
}
$aStoppedMessages = [
'Already running',
'Access restricted to administrators',
'A maintenance is ongoing',
'Maintenance detected',
'iTop is not yet installed',
];
foreach ($aStoppedMessages as $sMsg) {
if (preg_match("/$sMsg/", $sLine, $aMatches)) {
return $sLine;
}
}
if (false !== strpos($sLine, self::HELP_MESSAGE)) {
$aLines = utils::ReadTail($sLogFile, 5);
foreach ($aLines as $sLine) {
if (preg_match('/ERROR: (.*)$/', $sLine, $aMatches)) {
return $aMatches[1];
}
}
}
return null;
}
}