Cron parallelization

* Setup wait for multiple cron processes
 * Avoid waiting while tasks must be run
This commit is contained in:
Eric Espie
2022-07-04 12:26:26 +02:00
parent 3ab8bc71fe
commit 43bc77784a
2 changed files with 37 additions and 28 deletions

View File

@@ -2044,21 +2044,27 @@ JS
if (is_null($oConfig) || ContextTag::Check(ContextTag::TAG_CRON)) {
return;
}
// Use mutex to check if cron is running
// Limit the number of cron process to run in parallel
$iMaxCronProcess = $oConfig->Get('cron.max_process');
$iCount = 1;
$iMaxDuration = $oConfig->Get('cron_max_execution_time');
$iTimeLimit = time() + $iMaxDuration;
do {
$bIsRunning = false;
// Use all mutexes to check if cron is running
for ($i = 0; $i < $iMaxCronProcess; $i++) {
$sName = "cron#$i";
$oMutex = new iTopMutex(
'cron'.$oConfig->Get('db_name').$oConfig->Get('db_subname'),
$sName.$oConfig->Get('db_name').$oConfig->Get('db_subname'),
$oConfig->Get('db_host'),
$oConfig->Get('db_user'),
$oConfig->Get('db_pwd'),
$oConfig->Get('db_tls.enabled'),
$oConfig->Get('db_tls.ca')
);
$iCount = 1;
$iStarted = time();
$iMaxDuration = $oConfig->Get('cron_max_execution_time');
$iTimeLimit = $iStarted + $iMaxDuration;
while ($oMutex->IsLocked())
{
if ($oMutex->IsLocked()) {
$bIsRunning = true;
SetupLog::Info("Waiting for cron to stop ($iCount)");
$iCount++;
sleep(1);
@@ -2066,8 +2072,12 @@ JS
{
throw new Exception("Cannot enter $sMode mode, consider stopping the cron temporarily");
}
break;
}
}
} catch (Exception $e) {
} while ($bIsRunning);
}
catch (Exception $e) {
// Ignore errors
}
}