mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-19 07:12:26 +02:00
Cron parallelization * better logs
This commit is contained in:
@@ -316,9 +316,14 @@ class MFCompiler
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
SetupLog::Info("Compiling $sTempTargetDir...");
|
||||||
$this->DoCompile($sTempTargetDir, $sFinalTargetDir, $oP = null, $bUseSymbolicLinks);
|
$this->DoCompile($sTempTargetDir, $sFinalTargetDir, $oP = null, $bUseSymbolicLinks);
|
||||||
} catch (Exception $e) {
|
}
|
||||||
if ($sTempTargetDir != $sFinalTargetDir) {
|
catch (Exception $e)
|
||||||
|
{
|
||||||
|
SetupLog::Info("Compiling error: ".$e->getMessage());
|
||||||
|
if ($sTempTargetDir != $sFinalTargetDir)
|
||||||
|
{
|
||||||
// Cleanup the temporary directory
|
// Cleanup the temporary directory
|
||||||
SetupUtils::rrmdir($sTempTargetDir);
|
SetupUtils::rrmdir($sTempTargetDir);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1987,6 +1987,9 @@ JS
|
|||||||
*/
|
*/
|
||||||
private static function WaitCronTermination($oConfig, $sMode)
|
private static function WaitCronTermination($oConfig, $sMode)
|
||||||
{
|
{
|
||||||
|
$iMaxDuration = $oConfig->Get('cron_max_execution_time');
|
||||||
|
// Avoid PHP stopping while waiting the cron
|
||||||
|
set_time_limit($iMaxDuration);
|
||||||
try {
|
try {
|
||||||
// Wait for cron to stop
|
// Wait for cron to stop
|
||||||
if (is_null($oConfig) || ContextTag::Check(ContextTag::TAG_CRON)) {
|
if (is_null($oConfig) || ContextTag::Check(ContextTag::TAG_CRON)) {
|
||||||
@@ -1995,7 +1998,6 @@ JS
|
|||||||
// Limit the number of cron process to run in parallel
|
// Limit the number of cron process to run in parallel
|
||||||
$iMaxCronProcess = $oConfig->Get('cron.max_processes');
|
$iMaxCronProcess = $oConfig->Get('cron.max_processes');
|
||||||
$iCount = 1;
|
$iCount = 1;
|
||||||
$iMaxDuration = $oConfig->Get('cron_max_execution_time');
|
|
||||||
$iTimeLimit = time() + $iMaxDuration;
|
$iTimeLimit = time() + $iMaxDuration;
|
||||||
do {
|
do {
|
||||||
$bIsRunning = false;
|
$bIsRunning = false;
|
||||||
@@ -2003,25 +2005,26 @@ JS
|
|||||||
for ($i = 0; $i < $iMaxCronProcess; $i++) {
|
for ($i = 0; $i < $iMaxCronProcess; $i++) {
|
||||||
$sName = "cron#$i";
|
$sName = "cron#$i";
|
||||||
|
|
||||||
$oMutex = new iTopMutex(
|
$oMutex = new iTopMutex(
|
||||||
$sName.$oConfig->Get('db_name').$oConfig->Get('db_subname'),
|
$sName.$oConfig->Get('db_name').$oConfig->Get('db_subname'),
|
||||||
$oConfig->Get('db_host'),
|
$oConfig->Get('db_host'),
|
||||||
$oConfig->Get('db_user'),
|
$oConfig->Get('db_user'),
|
||||||
$oConfig->Get('db_pwd'),
|
$oConfig->Get('db_pwd'),
|
||||||
$oConfig->Get('db_tls.enabled'),
|
$oConfig->Get('db_tls.enabled'),
|
||||||
$oConfig->Get('db_tls.ca')
|
$oConfig->Get('db_tls.ca')
|
||||||
);
|
);
|
||||||
if ($oMutex->IsLocked()) {
|
if ($oMutex->IsLocked()) {
|
||||||
$bIsRunning = true;
|
$bIsRunning = true;
|
||||||
SetupLog::Info("Waiting for cron to stop ($iCount)");
|
SetupLog::Info("Waiting for cron to stop ($iCount)");
|
||||||
$iCount++;
|
$iCount++;
|
||||||
sleep(1);
|
sleep(1);
|
||||||
if (time() > $iTimeLimit) {
|
if (time() > $iTimeLimit) {
|
||||||
throw new Exception("Cannot enter $sMode mode, consider stopping the cron temporarily");
|
SetupLog::Error("Cannot enter $sMode mode, consider stopping the cron temporarily");
|
||||||
}
|
throw new Exception("Cannot enter $sMode mode, consider stopping the cron temporarily");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while ($bIsRunning);
|
} while ($bIsRunning);
|
||||||
}
|
}
|
||||||
catch (Exception $e) {
|
catch (Exception $e) {
|
||||||
|
|||||||
@@ -7,10 +7,13 @@
|
|||||||
namespace Combodo\iTop\Service\Cron;
|
namespace Combodo\iTop\Service\Cron;
|
||||||
|
|
||||||
use LogAPI;
|
use LogAPI;
|
||||||
|
use Page;
|
||||||
|
|
||||||
class CronLog extends LogAPI
|
class CronLog extends LogAPI
|
||||||
{
|
{
|
||||||
public static $iProcessNumber = 0;
|
public static $iProcessNumber = 0;
|
||||||
|
private static $bDebug = false;
|
||||||
|
private static $oP = null;
|
||||||
|
|
||||||
const CHANNEL_DEFAULT = 'Cron';
|
const CHANNEL_DEFAULT = 'Cron';
|
||||||
/**
|
/**
|
||||||
@@ -27,4 +30,18 @@ class CronLog extends LogAPI
|
|||||||
$sMessage = 'cron'.str_pad(static::$iProcessNumber, 3).$sMessage;
|
$sMessage = 'cron'.str_pad(static::$iProcessNumber, 3).$sMessage;
|
||||||
parent::Log($sLevel, $sMessage, $sChannel, $aContext);
|
parent::Log($sLevel, $sMessage, $sChannel, $aContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function Debug($sMessage, $sChannel = null, $aContext = array())
|
||||||
|
{
|
||||||
|
if (self::$bDebug && self::$oP) {
|
||||||
|
self::$oP->p('cron'.str_pad(static::$iProcessNumber, 3).$sMessage);
|
||||||
|
}
|
||||||
|
parent::Debug($sMessage, $sChannel, $aContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function SetDebug(Page $oP, $bDebug)
|
||||||
|
{
|
||||||
|
self::$oP = $oP;
|
||||||
|
self::$bDebug = $bDebug;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -191,10 +191,8 @@ function CronExec($bDebug )
|
|||||||
$oTasks = new DBObjectSet($oSearch, ['next_run_date' => true]);
|
$oTasks = new DBObjectSet($oSearch, ['next_run_date' => true]);
|
||||||
|
|
||||||
$aTasks = [];
|
$aTasks = [];
|
||||||
if ($oTasks->CountExceeds(0))
|
if ($oTasks->CountExceeds(0)) {
|
||||||
{
|
$aDebugMessages = [];
|
||||||
$sCount = $oTasks->Count();
|
|
||||||
CronLog::Debug("$sCount Tasks planned to run now ($sNow):");
|
|
||||||
while ($oTask = $oTasks->Fetch()) {
|
while ($oTask = $oTasks->Fetch()) {
|
||||||
$sTaskName = $oTask->Get('class_name');
|
$sTaskName = $oTask->Get('class_name');
|
||||||
$oTaskMutex = new iTopMutex("cron_$sTaskName");
|
$oTaskMutex = new iTopMutex("cron_$sTaskName");
|
||||||
@@ -206,7 +204,12 @@ function CronExec($bDebug )
|
|||||||
$sStatus = $oTask->Get('status');
|
$sStatus = $oTask->Get('status');
|
||||||
$sLastRunDate = $oTask->Get('latest_run_date');
|
$sLastRunDate = $oTask->Get('latest_run_date');
|
||||||
$sNextRunDate = $oTask->Get('next_run_date');
|
$sNextRunDate = $oTask->Get('next_run_date');
|
||||||
CronLog::Debug(sprintf('Task Class: %1$-25.25s Status: %2$-7s Last Run: %3$-19s Next Run: %4$-19s', $sTaskName, $sStatus, $sLastRunDate, $sNextRunDate));
|
$aDebugMessages[] = sprintf('Task Class: %1$-25.25s Status: %2$-7s Last Run: %3$-19s Next Run: %4$-19s', $sTaskName, $sStatus, $sLastRunDate, $sNextRunDate);
|
||||||
|
}
|
||||||
|
$sCount = count($aDebugMessages);
|
||||||
|
CronLog::Debug("$sCount Tasks planned to run now ($sNow):");
|
||||||
|
foreach ($aDebugMessages as $sDebugMessage) {
|
||||||
|
CronLog::Debug($sDebugMessage);
|
||||||
}
|
}
|
||||||
$aRunTasks = [];
|
$aRunTasks = [];
|
||||||
while ($aTasks != []) {
|
while ($aTasks != []) {
|
||||||
@@ -248,7 +251,7 @@ function CronExec($bDebug )
|
|||||||
}
|
}
|
||||||
$oEnd = new DateTime();
|
$oEnd = new DateTime();
|
||||||
$sNextRunDate = $oTask->Get('next_run_date');
|
$sNextRunDate = $oTask->Get('next_run_date');
|
||||||
CronLog::Debug("<< === ".$oEnd->format('Y-m-d H:i:s').sprintf(" End of: %-'=42s", ' '.$sTaskClass.' ')." Next: $sNextRunDate");
|
CronLog::Debug("<< === ".$oEnd->format('Y-m-d H:i:s').sprintf(" End of: %-'=49s", ' '.$sTaskClass.' ')." Next: $sNextRunDate");
|
||||||
if (time() > $iTimeLimit)
|
if (time() > $iTimeLimit)
|
||||||
{
|
{
|
||||||
break 2;
|
break 2;
|
||||||
@@ -275,6 +278,7 @@ function CronExec($bDebug )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (count($aTasks) == 0) {
|
if (count($aTasks) == 0) {
|
||||||
|
CronLog::Debug("sleeping...");
|
||||||
sleep($iCronSleep);
|
sleep($iCronSleep);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -437,6 +441,11 @@ try
|
|||||||
{
|
{
|
||||||
utils::UseParamFile();
|
utils::UseParamFile();
|
||||||
|
|
||||||
|
// Allow verbosity on output (writing debug messages to the cron.log file is configured with log_level_min config parameter)
|
||||||
|
$bVerbose = utils::ReadParam('verbose', false, true /* Allow CLI */);
|
||||||
|
CronLog::SetDebug($oP, $bVerbose);
|
||||||
|
|
||||||
|
// Used to launch the BackgroundTask in debug mode
|
||||||
$bDebug = utils::ReadParam('debug', false, true /* Allow CLI */);
|
$bDebug = utils::ReadParam('debug', false, true /* Allow CLI */);
|
||||||
|
|
||||||
if ($bIsModeCLI) {
|
if ($bIsModeCLI) {
|
||||||
@@ -502,7 +511,8 @@ try
|
|||||||
CronLog::Info('Starting: '.time().' ('.date('Y-m-d H:i:s').')');
|
CronLog::Info('Starting: '.time().' ('.date('Y-m-d H:i:s').')');
|
||||||
CronExec($bDebug);
|
CronExec($bDebug);
|
||||||
} else {
|
} else {
|
||||||
CronLog::Debug("Already $iMaxCronProcess are running...");
|
CronLog::$iProcessNumber = $iMaxCronProcess + 1;
|
||||||
|
CronLog::Debug("The limit of $iMaxCronProcess cron process running in parallel is already reached");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user