mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-17 22:39:03 +02:00
N°4692 - Enable parallelization of multiple CRON jobs
This commit is contained in:
@@ -155,6 +155,8 @@ function RunTask(BackgroundTask $oTask, $iTimeLimit)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
|
* @param bool $bDebug
|
||||||
*
|
*
|
||||||
* @throws \ArchivedObjectException
|
* @throws \ArchivedObjectException
|
||||||
* @throws \CoreCannotSaveObjectException
|
* @throws \CoreCannotSaveObjectException
|
||||||
@@ -173,9 +175,19 @@ function CronExec($bDebug )
|
|||||||
$iMaxDuration = MetaModel::GetConfig()->Get('cron_max_execution_time');
|
$iMaxDuration = MetaModel::GetConfig()->Get('cron_max_execution_time');
|
||||||
$iTimeLimit = $iStarted + $iMaxDuration;
|
$iTimeLimit = $iStarted + $iMaxDuration;
|
||||||
$iCronSleep = MetaModel::GetConfig()->Get('cron_sleep');
|
$iCronSleep = MetaModel::GetConfig()->Get('cron_sleep');
|
||||||
$iMaxCronProcess = MetaModel::GetConfig()->Get('cron.max_processes');
|
$iMaxCronProcess = max(MetaModel::GetConfig()->Get('cron.max_processes'), 1);
|
||||||
|
|
||||||
|
// Allow a time slot for every task
|
||||||
|
// knowing that there are $iMaxCronProcess running in parallel for the amount of tasks
|
||||||
|
$oSearch = new DBObjectSearch('BackgroundTask');
|
||||||
|
$oSearch->AddCondition('status', 'active');
|
||||||
|
$oTasks = new DBObjectSet($oSearch);
|
||||||
|
$iCount = $oTasks->Count();
|
||||||
|
$iTotalAvailableTime = $iMaxDuration * $iMaxCronProcess;
|
||||||
|
$iTimeSlot = (int)($iTotalAvailableTime / max($iCount, 1));
|
||||||
|
|
||||||
CronLog::Debug("Planned duration = $iMaxDuration seconds");
|
CronLog::Debug("Planned duration = $iMaxDuration seconds");
|
||||||
|
CronLog::Debug("Planned duration per task = $iTimeSlot seconds");
|
||||||
CronLog::Debug("Loop pause = $iCronSleep seconds");
|
CronLog::Debug("Loop pause = $iCronSleep seconds");
|
||||||
|
|
||||||
ReSyncProcesses($bDebug);
|
ReSyncProcesses($bDebug);
|
||||||
@@ -212,7 +224,7 @@ function CronExec($bDebug )
|
|||||||
CronLog::Debug($sDebugMessage);
|
CronLog::Debug($sDebugMessage);
|
||||||
}
|
}
|
||||||
$aRunTasks = [];
|
$aRunTasks = [];
|
||||||
while ($aTasks != []) {
|
while (count($aTasks) > 0) {
|
||||||
$oTask = array_shift($aTasks);
|
$oTask = array_shift($aTasks);
|
||||||
$sTaskClass = $oTask->Get('class_name');
|
$sTaskClass = $oTask->Get('class_name');
|
||||||
|
|
||||||
@@ -226,14 +238,17 @@ function CronExec($bDebug )
|
|||||||
$aRunTasks[] = $sTaskClass;
|
$aRunTasks[] = $sTaskClass;
|
||||||
|
|
||||||
// N°3219 for each process will use a specific CMDBChange object with a specific track info
|
// N°3219 for each process will use a specific CMDBChange object with a specific track info
|
||||||
// Any BackgroundProcess can overrides this as needed
|
// Any BackgroundProcess can override this as needed
|
||||||
CMDBObject::SetCurrentChangeFromParams("Background task ($sTaskClass)");
|
CMDBObject::SetCurrentChangeFromParams("Background task ($sTaskClass)");
|
||||||
|
|
||||||
// Run the task and record its next run time
|
// Run the task and record its next run time
|
||||||
$oNow = new DateTime();
|
$oNow = new DateTime();
|
||||||
CronLog::Debug(">> === ".$oNow->format('Y-m-d H:i:s').sprintf(" Start task:%-'=49s", ' '.$sTaskClass.' '));
|
CronLog::Debug(">> === ".$oNow->format('Y-m-d H:i:s').sprintf(" Start task:%-'=49s", ' '.$sTaskClass.' '));
|
||||||
try {
|
try
|
||||||
$sMessage = RunTask($oTask, $iTimeLimit);
|
{
|
||||||
|
// The limit of time for this task corresponds to the time slot allowed for every task
|
||||||
|
// but limited to the cron job time limit
|
||||||
|
$sMessage = RunTask($oTask, min($iTimeLimit, time() + $iTimeSlot));
|
||||||
} catch (MySQLHasGoneAwayException $e)
|
} catch (MySQLHasGoneAwayException $e)
|
||||||
{
|
{
|
||||||
CronLog::Error("ERROR : 'MySQL has gone away' thrown when processing $sTaskClass (error_code=".$e->getCode().")");
|
CronLog::Error("ERROR : 'MySQL has gone away' thrown when processing $sTaskClass (error_code=".$e->getCode().")");
|
||||||
@@ -495,7 +510,7 @@ try
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Limit the number of cron process to run in parallel
|
// Limit the number of cron process to run in parallel
|
||||||
$iMaxCronProcess = MetaModel::GetConfig()->Get('cron.max_processes');
|
$iMaxCronProcess = max(MetaModel::GetConfig()->Get('cron.max_processes'), 1);
|
||||||
$bCanRun = false;
|
$bCanRun = false;
|
||||||
$iProcessNumber = 0;
|
$iProcessNumber = 0;
|
||||||
for ($i = 0; $i < $iMaxCronProcess; $i++) {
|
for ($i = 0; $i < $iMaxCronProcess; $i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user