|
|
|
|
@@ -18,17 +18,20 @@
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use Combodo\iTop\Application\WebPage\CLIPage;
|
|
|
|
|
use Combodo\iTop\Application\WebPage\Page;
|
|
|
|
|
use Combodo\iTop\Application\WebPage\WebPage;
|
|
|
|
|
use Combodo\iTop\Service\Cron\CronLog;
|
|
|
|
|
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
|
|
|
|
|
|
|
|
|
|
if (!defined('__DIR__')) {
|
|
|
|
|
define('__DIR__', dirname(__FILE__));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
require_once(__DIR__.'/../approot.inc.php');
|
|
|
|
|
|
|
|
|
|
const EXIT_CODE_ERROR = -1;
|
|
|
|
|
const EXIT_CODE_FATAL = -2;
|
|
|
|
|
// early exit
|
|
|
|
|
if (file_exists(READONLY_MODE_FILE))
|
|
|
|
|
{
|
|
|
|
|
if (file_exists(READONLY_MODE_FILE)) {
|
|
|
|
|
echo "iTop is read-only. Exiting...\n";
|
|
|
|
|
exit(EXIT_CODE_ERROR);
|
|
|
|
|
}
|
|
|
|
|
@@ -37,8 +40,7 @@ require_once(APPROOT.'/application/application.inc.php');
|
|
|
|
|
require_once(APPROOT.'/core/background.inc.php');
|
|
|
|
|
|
|
|
|
|
$sConfigFile = APPCONF.ITOP_DEFAULT_ENV.'/'.ITOP_CONFIG_FILE;
|
|
|
|
|
if (!file_exists($sConfigFile))
|
|
|
|
|
{
|
|
|
|
|
if (!file_exists($sConfigFile)) {
|
|
|
|
|
echo "iTop is not yet installed. Exiting...\n";
|
|
|
|
|
exit(EXIT_CODE_ERROR);
|
|
|
|
|
}
|
|
|
|
|
@@ -50,8 +52,7 @@ $oCtx = new ContextTag(ContextTag::TAG_CRON);
|
|
|
|
|
function ReadMandatoryParam($oP, $sParam, $sSanitizationFilter = 'parameter')
|
|
|
|
|
{
|
|
|
|
|
$sValue = utils::ReadParam($sParam, null, true, $sSanitizationFilter);
|
|
|
|
|
if (is_null($sValue))
|
|
|
|
|
{
|
|
|
|
|
if (is_null($sValue)) {
|
|
|
|
|
$oP->p("ERROR: Missing argument '$sParam'\n");
|
|
|
|
|
UsageAndExit($oP);
|
|
|
|
|
}
|
|
|
|
|
@@ -63,13 +64,10 @@ function UsageAndExit($oP)
|
|
|
|
|
{
|
|
|
|
|
$bModeCLI = ($oP instanceof CLIPage);
|
|
|
|
|
|
|
|
|
|
if ($bModeCLI)
|
|
|
|
|
{
|
|
|
|
|
if ($bModeCLI) {
|
|
|
|
|
$oP->p("USAGE:\n");
|
|
|
|
|
$oP->p("php cron.php --auth_user=<login> --auth_pwd=<password> [--param_file=<file>] [--verbose=1] [--debug=1] [--status_only=1]\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$oP->p("php cron.php --auth_user=<login> --auth_pwd=<password> [--param_file=<file>] [--verbose=0] [--status_only=1]\n");
|
|
|
|
|
} else {
|
|
|
|
|
$oP->p("Optional parameters: verbose, param_file, status_only\n");
|
|
|
|
|
}
|
|
|
|
|
$oP->output();
|
|
|
|
|
@@ -96,92 +94,64 @@ function RunTask(BackgroundTask $oTask, $iTimeLimit)
|
|
|
|
|
$oProcess = new $TaskClass;
|
|
|
|
|
$oRefClass = new ReflectionClass(get_class($oProcess));
|
|
|
|
|
$oDateStarted = new DateTime();
|
|
|
|
|
$oDatePlanned = new DateTime($oTask->Get('next_run_date'));
|
|
|
|
|
$fStart = microtime(true);
|
|
|
|
|
$oCtx = new ContextTag('CRON:Task:'.$TaskClass);
|
|
|
|
|
|
|
|
|
|
$sMessage = '';
|
|
|
|
|
$oExceptionToThrow = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
// Record (when starting) that this task was started, just in case it crashes during the execution
|
|
|
|
|
if ($oTask->Get('total_exec_count') == 0) {
|
|
|
|
|
// First execution
|
|
|
|
|
$oTask->Set('first_run_date', $oDateStarted->format('Y-m-d H:i:s'));
|
|
|
|
|
}
|
|
|
|
|
$oTask->Set('latest_run_date', $oDateStarted->format('Y-m-d H:i:s'));
|
|
|
|
|
// Record the current user running the cron
|
|
|
|
|
$oTask->Set('system_user', utils::GetCurrentUserName());
|
|
|
|
|
$oTask->Set('running', 1);
|
|
|
|
|
$oTask->DBUpdate();
|
|
|
|
|
// Time in seconds allowed to the task
|
|
|
|
|
$iCurrTimeLimit = $iTimeLimit;
|
|
|
|
|
// Compute allowed time
|
|
|
|
|
if ($oRefClass->implementsInterface('iScheduledProcess') === false)
|
|
|
|
|
{
|
|
|
|
|
// Periodic task, allow only X times ($iMaxTaskExecutionTime) its periodicity (GetPeriodicity())
|
|
|
|
|
$iMaxTaskExecutionTime = MetaModel::GetConfig()->Get('cron_task_max_execution_time');
|
|
|
|
|
$iTaskLimit = time() + $oProcess->GetPeriodicity() * $iMaxTaskExecutionTime;
|
|
|
|
|
// If our proposed time limit is less than cron limit, and cron_task_max_execution_time is > 0
|
|
|
|
|
if ($iTaskLimit < $iTimeLimit && $iMaxTaskExecutionTime > 0)
|
|
|
|
|
{
|
|
|
|
|
$iCurrTimeLimit = $iTaskLimit;
|
|
|
|
|
// Compute the next run date
|
|
|
|
|
if ($oRefClass->implementsInterface('iScheduledProcess')) {
|
|
|
|
|
// Schedules process do repeat at specific moments
|
|
|
|
|
$oPlannedStart = $oProcess->GetNextOccurrence();
|
|
|
|
|
} else {
|
|
|
|
|
// Background processes do repeat periodically
|
|
|
|
|
$oDatePlanned = new DateTime($oTask->Get('next_run_date'));
|
|
|
|
|
$oPlannedStart = clone $oDatePlanned;
|
|
|
|
|
// Let's schedule from the previous planned date of execution to avoid shift
|
|
|
|
|
$oPlannedStart->modify('+'.$oProcess->GetPeriodicity().' seconds');
|
|
|
|
|
$oNow = new DateTime();
|
|
|
|
|
while ($oPlannedStart->format('U') <= $oNow->format('U')) {
|
|
|
|
|
// Next planned start is already in the past, increase it again by a period
|
|
|
|
|
$oPlannedStart = $oPlannedStart->modify('+'.$oProcess->GetPeriodicity().' seconds');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$sMessage = $oProcess->Process($iCurrTimeLimit);
|
|
|
|
|
$oTask->Set('running', 0);
|
|
|
|
|
$oTask->Set('next_run_date', $oPlannedStart->format('Y-m-d H:i:s'));
|
|
|
|
|
$oTask->DBUpdate();
|
|
|
|
|
|
|
|
|
|
$sMessage = $oProcess->Process($iTimeLimit);
|
|
|
|
|
}
|
|
|
|
|
catch (MySQLHasGoneAwayException $e)
|
|
|
|
|
{
|
|
|
|
|
catch (MySQLHasGoneAwayException $e) {
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
catch (ProcessFatalException $e)
|
|
|
|
|
{
|
|
|
|
|
catch (ProcessFatalException $e) {
|
|
|
|
|
$oExceptionToThrow = $e;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception $e) // we shouldn't get so much exceptions... but we need to handle legacy code, and cron.php has to keep running
|
|
|
|
|
{
|
|
|
|
|
if ($oTask->IsDebug())
|
|
|
|
|
{
|
|
|
|
|
$sMessage = 'Processing failed with message: '. $e->getMessage() . '. ' . $e->getTraceAsString();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$sMessage = 'Processing failed with message: '. $e->getMessage();
|
|
|
|
|
if ($oTask->IsDebug()) {
|
|
|
|
|
$sMessage = 'Processing failed with message: '.$e->getMessage().'. '.$e->getTraceAsString();
|
|
|
|
|
} else {
|
|
|
|
|
$sMessage = 'Processing failed with message: '.$e->getMessage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$fDuration = microtime(true) - $fStart;
|
|
|
|
|
if ($oTask->Get('total_exec_count') == 0)
|
|
|
|
|
{
|
|
|
|
|
// First execution
|
|
|
|
|
$oTask->Set('first_run_date', $oDateStarted->format('Y-m-d H:i:s'));
|
|
|
|
|
}
|
|
|
|
|
$oTask->ComputeDurations($fDuration); // does increment the counter and compute statistics
|
|
|
|
|
|
|
|
|
|
// Update the timestamp since we want to be able to re-order the tasks based on the time they finished
|
|
|
|
|
$oDateEnded = new DateTime();
|
|
|
|
|
$oTask->Set('latest_run_date', $oDateEnded->format('Y-m-d H:i:s'));
|
|
|
|
|
|
|
|
|
|
if ($oRefClass->implementsInterface('iScheduledProcess'))
|
|
|
|
|
{
|
|
|
|
|
// Schedules process do repeat at specific moments
|
|
|
|
|
$oPlannedStart = $oProcess->GetNextOccurrence();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Background processes do repeat periodically
|
|
|
|
|
$oPlannedStart = clone $oDatePlanned;
|
|
|
|
|
// Let's schedule from the previous planned date of execution to avoid shift
|
|
|
|
|
$oPlannedStart->modify($oProcess->GetPeriodicity().' seconds');
|
|
|
|
|
$oEnd = new DateTime();
|
|
|
|
|
while ($oPlannedStart->format('U') < $oEnd->format('U'))
|
|
|
|
|
{
|
|
|
|
|
// Next planned start is already in the past, increase it again by a period
|
|
|
|
|
$oPlannedStart = $oPlannedStart->modify('+'.$oProcess->GetPeriodicity().' seconds');
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
$oTask->Set('running', 0);
|
|
|
|
|
$fDuration = microtime(true) - $fStart;
|
|
|
|
|
$oTask->ComputeDurations($fDuration); // does increment the counter and compute statistics
|
|
|
|
|
$oTask->DBUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$oTask->Set('next_run_date', $oPlannedStart->format('Y-m-d H:i:s'));
|
|
|
|
|
$oTask->DBUpdate();
|
|
|
|
|
|
|
|
|
|
if ($oExceptionToThrow)
|
|
|
|
|
{
|
|
|
|
|
if ($oExceptionToThrow) {
|
|
|
|
|
throw $oExceptionToThrow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -191,8 +161,6 @@ function RunTask(BackgroundTask $oTask, $iTimeLimit)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param CLIPage|WebPage $oP
|
|
|
|
|
* @param boolean $bVerbose
|
|
|
|
|
*
|
|
|
|
|
* @param bool $bDebug
|
|
|
|
|
*
|
|
|
|
|
@@ -207,24 +175,31 @@ function RunTask(BackgroundTask $oTask, $iTimeLimit)
|
|
|
|
|
* @throws \OQLException
|
|
|
|
|
* @throws \ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
function CronExec($oP, $bVerbose, $bDebug=false)
|
|
|
|
|
function CronExec($bDebug)
|
|
|
|
|
{
|
|
|
|
|
$iStarted = time();
|
|
|
|
|
$iMaxDuration = MetaModel::GetConfig()->Get('cron_max_execution_time');
|
|
|
|
|
$iTimeLimit = $iStarted + $iMaxDuration;
|
|
|
|
|
$iCronSleep = MetaModel::GetConfig()->Get('cron_sleep');
|
|
|
|
|
$iMaxCronProcess = max(MetaModel::GetConfig()->Get('cron.max_processes'), 1);
|
|
|
|
|
|
|
|
|
|
if ($bVerbose)
|
|
|
|
|
{
|
|
|
|
|
$oP->p("Planned duration = $iMaxDuration seconds");
|
|
|
|
|
$oP->p("Loop pause = $iCronSleep seconds");
|
|
|
|
|
}
|
|
|
|
|
// 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));
|
|
|
|
|
|
|
|
|
|
ReSyncProcesses($oP, $bVerbose, $bDebug);
|
|
|
|
|
CronLog::Trace("Planned duration = $iMaxDuration seconds");
|
|
|
|
|
CronLog::Trace("Planned duration per task = $iTimeSlot seconds");
|
|
|
|
|
CronLog::Trace("Loop pause = $iCronSleep seconds");
|
|
|
|
|
|
|
|
|
|
while (time() < $iTimeLimit)
|
|
|
|
|
{
|
|
|
|
|
CheckMaintenanceMode($oP);
|
|
|
|
|
ReSyncProcesses($bDebug);
|
|
|
|
|
|
|
|
|
|
while (time() < $iTimeLimit) {
|
|
|
|
|
CheckMaintenanceMode();
|
|
|
|
|
|
|
|
|
|
$oNow = new DateTime();
|
|
|
|
|
$sNow = $oNow->format('Y-m-d H:i:s');
|
|
|
|
|
@@ -232,120 +207,109 @@ function CronExec($oP, $bVerbose, $bDebug=false)
|
|
|
|
|
$oSearch->AddCondition('next_run_date', $sNow, '<=');
|
|
|
|
|
$oSearch->AddCondition('status', 'active');
|
|
|
|
|
$oTasks = new DBObjectSet($oSearch, ['next_run_date' => true]);
|
|
|
|
|
$bWorkDone = false;
|
|
|
|
|
|
|
|
|
|
if ($oTasks->CountExceeds(0))
|
|
|
|
|
{
|
|
|
|
|
$bWorkDone = true;
|
|
|
|
|
$aTasks = array();
|
|
|
|
|
if ($bVerbose)
|
|
|
|
|
{
|
|
|
|
|
$sCount = $oTasks->Count();
|
|
|
|
|
$oP->p("$sCount Tasks planned to run now ($sNow):");
|
|
|
|
|
$oP->p('+---------------------------+---------+---------------------+---------------------+');
|
|
|
|
|
$oP->p('| Task Class | Status | Last Run | Next Run |');
|
|
|
|
|
$oP->p('+---------------------------+---------+---------------------+---------------------+');
|
|
|
|
|
}
|
|
|
|
|
while ($oTask = $oTasks->Fetch())
|
|
|
|
|
{
|
|
|
|
|
$aTasks[$oTask->Get('class_name')] = $oTask;
|
|
|
|
|
if ($bVerbose)
|
|
|
|
|
{
|
|
|
|
|
$sTaskName = $oTask->Get('class_name');
|
|
|
|
|
$sStatus = $oTask->Get('status');
|
|
|
|
|
$sLastRunDate = $oTask->Get('latest_run_date');
|
|
|
|
|
$sNextRunDate = $oTask->Get('next_run_date');
|
|
|
|
|
$oP->p(sprintf('| %1$-25.25s | %2$-7s | %3$-19s | %4$-19s |', $sTaskName, $sStatus, $sLastRunDate, $sNextRunDate));
|
|
|
|
|
$aTasks = [];
|
|
|
|
|
if ($oTasks->CountExceeds(0)) {
|
|
|
|
|
$aDebugMessages = [];
|
|
|
|
|
while ($oTask = $oTasks->Fetch()) {
|
|
|
|
|
$sTaskName = $oTask->Get('class_name');
|
|
|
|
|
$oTaskMutex = new iTopMutex("cron_$sTaskName");
|
|
|
|
|
if ($oTaskMutex->IsLocked()) {
|
|
|
|
|
// Already running, ignore
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$aTasks[] = $oTask;
|
|
|
|
|
$sStatus = $oTask->Get('status');
|
|
|
|
|
$sLastRunDate = $oTask->Get('latest_run_date');
|
|
|
|
|
$sNextRunDate = $oTask->Get('next_run_date');
|
|
|
|
|
$aDebugMessages[] = sprintf('Task Class: %1$-25.25s Status: %2$-7s Last Run: %3$-19s Next Run: %4$-19s', $sTaskName, $sStatus, $sLastRunDate, $sNextRunDate);
|
|
|
|
|
}
|
|
|
|
|
if ($bVerbose)
|
|
|
|
|
{
|
|
|
|
|
$oP->p('+---------------------------+---------+---------------------+---------------------+');
|
|
|
|
|
$sCount = count($aDebugMessages);
|
|
|
|
|
CronLog::Trace("$sCount Tasks planned to run now ($sNow):");
|
|
|
|
|
foreach ($aDebugMessages as $sDebugMessage) {
|
|
|
|
|
CronLog::Trace($sDebugMessage);
|
|
|
|
|
}
|
|
|
|
|
$aRunTasks = [];
|
|
|
|
|
foreach ($aTasks as $oTask)
|
|
|
|
|
{
|
|
|
|
|
while (count($aTasks) > 0) {
|
|
|
|
|
$oTask = array_shift($aTasks);
|
|
|
|
|
|
|
|
|
|
$sTaskClass = $oTask->Get('class_name');
|
|
|
|
|
|
|
|
|
|
// Check if the current task is running
|
|
|
|
|
$oTaskMutex = new iTopMutex("cron_$sTaskClass");
|
|
|
|
|
if (!$oTaskMutex->TryLock()) {
|
|
|
|
|
// Task is already running, try next one
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$aRunTasks[] = $sTaskClass;
|
|
|
|
|
|
|
|
|
|
// 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)");
|
|
|
|
|
|
|
|
|
|
// Run the task and record its next run time
|
|
|
|
|
if ($bVerbose)
|
|
|
|
|
{
|
|
|
|
|
$oNow = new DateTime();
|
|
|
|
|
$oP->p(">> === ".$oNow->format('Y-m-d H:i:s').sprintf(" Starting:%-'=49s", ' '.$sTaskClass.' '));
|
|
|
|
|
$sDebugTaskClass = CronLog::GetDebugClassName($sTaskClass);
|
|
|
|
|
$oNow = new DateTime();
|
|
|
|
|
CronLog::Debug(sprintf("> Starting >>> %-'>49s", $sDebugTaskClass.' '));
|
|
|
|
|
try {
|
|
|
|
|
// 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));
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
$sMessage = RunTask($aTasks[$sTaskClass], $iTimeLimit);
|
|
|
|
|
} catch (MySQLHasGoneAwayException $e)
|
|
|
|
|
{
|
|
|
|
|
$oP->p("ERROR : 'MySQL has gone away' thrown when processing $sTaskClass (error_code=".$e->getCode().")");
|
|
|
|
|
catch (MySQLHasGoneAwayException $e) {
|
|
|
|
|
CronLog::Error("ERROR : 'MySQL has gone away' thrown when processing $sDebugTaskClass (error_code=".$e->getCode().")", CronLog::CHANNEL_DEFAULT, ['stack' => $e->getTraceAsString()]);
|
|
|
|
|
exit(EXIT_CODE_FATAL);
|
|
|
|
|
} catch (ProcessFatalException $e)
|
|
|
|
|
{
|
|
|
|
|
$oP->p("ERROR : an exception was thrown when processing '$sTaskClass' (".$e->getInfoLog().")");
|
|
|
|
|
IssueLog::Error("Cron.php error : an exception was thrown when processing '$sTaskClass' (".$e->getInfoLog().')');
|
|
|
|
|
}
|
|
|
|
|
if ($bVerbose)
|
|
|
|
|
{
|
|
|
|
|
if (!empty($sMessage))
|
|
|
|
|
{
|
|
|
|
|
$oP->p("$sTaskClass: $sMessage");
|
|
|
|
|
}
|
|
|
|
|
$oEnd = new DateTime();
|
|
|
|
|
$sNextRunDate = $oTask->Get('next_run_date');
|
|
|
|
|
$oP->p("<< === ".$oEnd->format('Y-m-d H:i:s').sprintf(" End of: %-'=42s", ' '.$sTaskClass.' ')." Next: $sNextRunDate");
|
|
|
|
|
catch (ProcessFatalException $e) {
|
|
|
|
|
CronLog::Error("ERROR : an exception was thrown when processing '$sDebugTaskClass' (".$e->getInfoLog().")", CronLog::CHANNEL_DEFAULT, ['stack' => $e->getTraceAsString()]);
|
|
|
|
|
}
|
|
|
|
|
if (time() > $iTimeLimit)
|
|
|
|
|
{
|
|
|
|
|
finally {
|
|
|
|
|
$oTaskMutex->Unlock();
|
|
|
|
|
}
|
|
|
|
|
if (!empty($sMessage)) {
|
|
|
|
|
CronLog::Debug("$sDebugTaskClass: $sMessage");
|
|
|
|
|
}
|
|
|
|
|
$oEnd = new DateTime();
|
|
|
|
|
$sNextRunDate = $oTask->Get('next_run_date');
|
|
|
|
|
CronLog::Debug(sprintf("< Ending <<<<< %-'<49s", $sDebugTaskClass.' ')." Next: $sNextRunDate");
|
|
|
|
|
if (time() > $iTimeLimit) {
|
|
|
|
|
break 2;
|
|
|
|
|
}
|
|
|
|
|
CheckMaintenanceMode($oP);
|
|
|
|
|
CheckMaintenanceMode();
|
|
|
|
|
if ($iMaxCronProcess > 1) {
|
|
|
|
|
// Reindex tasks every time
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tasks to run later
|
|
|
|
|
if ($bVerbose)
|
|
|
|
|
{
|
|
|
|
|
$oP->p('--');
|
|
|
|
|
if (count($aTasks) == 0) {
|
|
|
|
|
$oSearch = new DBObjectSearch('BackgroundTask');
|
|
|
|
|
$oSearch->AddCondition('next_run_date', $sNow, '>');
|
|
|
|
|
$oSearch->AddCondition('status', 'active');
|
|
|
|
|
$oTasks = new DBObjectSet($oSearch, ['next_run_date' => true]);
|
|
|
|
|
while ($oTask = $oTasks->Fetch())
|
|
|
|
|
{
|
|
|
|
|
if (!in_array($oTask->Get('class_name'), $aRunTasks))
|
|
|
|
|
{
|
|
|
|
|
$oP->p(sprintf("-- Skipping task: %-'-40s", $oTask->Get('class_name').' ')." until: ".$oTask->Get('next_run_date'));
|
|
|
|
|
while ($oTask = $oTasks->Fetch()) {
|
|
|
|
|
if (!in_array($oTask->Get('class_name'), $aRunTasks)) {
|
|
|
|
|
$sDebugTaskClass = CronLog::GetDebugClassName($oTask->Get('class_name'));
|
|
|
|
|
CronLog::Trace(sprintf("-- Skipping task: %-'-40s", $sDebugTaskClass.' ')." until: ".$oTask->Get('next_run_date'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($bVerbose && $bWorkDone)
|
|
|
|
|
{
|
|
|
|
|
$oP->p("Sleeping...\n");
|
|
|
|
|
if (count($aTasks) == 0) {
|
|
|
|
|
CronLog::Trace("sleeping...");
|
|
|
|
|
sleep($iCronSleep);
|
|
|
|
|
}
|
|
|
|
|
sleep($iCronSleep);
|
|
|
|
|
}
|
|
|
|
|
if ($bVerbose)
|
|
|
|
|
{
|
|
|
|
|
$oP->p('');
|
|
|
|
|
DisplayStatus($oP, ['next_run_date' => true]);
|
|
|
|
|
$oP->p("Reached normal execution time limit (exceeded by ".(time() - $iTimeLimit)."s)");
|
|
|
|
|
}
|
|
|
|
|
CronLog::Trace("Reached normal execution time limit (exceeded by ".(time() - $iTimeLimit)."s)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param WebPage $oP
|
|
|
|
|
*/
|
|
|
|
|
function CheckMaintenanceMode(Page $oP) {
|
|
|
|
|
function CheckMaintenanceMode()
|
|
|
|
|
{
|
|
|
|
|
// Verify files instead of reloading the full config each time
|
|
|
|
|
if (file_exists(MAINTENANCE_MODE_FILE) || file_exists(READONLY_MODE_FILE)) {
|
|
|
|
|
$oP->p("Maintenance detected, exiting");
|
|
|
|
|
CronLog::Info("Maintenance detected, exiting");
|
|
|
|
|
exit(EXIT_CODE_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -360,15 +324,14 @@ function CheckMaintenanceMode(Page $oP) {
|
|
|
|
|
* @throws \MySQLException
|
|
|
|
|
* @throws \OQLException
|
|
|
|
|
*/
|
|
|
|
|
function DisplayStatus($oP, $aTaskOrderBy = [])
|
|
|
|
|
function DisplayStatus($oP = null, $aTaskOrderBy = [])
|
|
|
|
|
{
|
|
|
|
|
$oSearch = new DBObjectSearch('BackgroundTask');
|
|
|
|
|
$oTasks = new DBObjectSet($oSearch, $aTaskOrderBy);
|
|
|
|
|
$oP->p('+---------------------------+---------+---------------------+---------------------+--------+-----------+');
|
|
|
|
|
$oP->p('| Task Class | Status | Last Run | Next Run | Nb Run | Avg. Dur. |');
|
|
|
|
|
$oP->p('+---------------------------+---------+---------------------+---------------------+--------+-----------+');
|
|
|
|
|
while ($oTask = $oTasks->Fetch())
|
|
|
|
|
{
|
|
|
|
|
while ($oTask = $oTasks->Fetch()) {
|
|
|
|
|
$sTaskName = $oTask->Get('class_name');
|
|
|
|
|
$sStatus = $oTask->Get('status');
|
|
|
|
|
$sLastRunDate = $oTask->Get('latest_run_date');
|
|
|
|
|
@@ -382,8 +345,6 @@ function DisplayStatus($oP, $aTaskOrderBy = [])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $oP
|
|
|
|
|
* @param $bVerbose
|
|
|
|
|
* @param $bDebug
|
|
|
|
|
*
|
|
|
|
|
* @throws \ArchivedObjectException
|
|
|
|
|
@@ -395,28 +356,25 @@ function DisplayStatus($oP, $aTaskOrderBy = [])
|
|
|
|
|
* @throws \OQLException
|
|
|
|
|
* @throws \ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
function ReSyncProcesses($oP, $bVerbose, $bDebug)
|
|
|
|
|
function ReSyncProcesses($bDebug)
|
|
|
|
|
{
|
|
|
|
|
// Enumerate classes implementing BackgroundProcess
|
|
|
|
|
//
|
|
|
|
|
$oSearch = new DBObjectSearch('BackgroundTask');
|
|
|
|
|
$oTasks = new DBObjectSet($oSearch);
|
|
|
|
|
$aTasks = array();
|
|
|
|
|
while ($oTask = $oTasks->Fetch())
|
|
|
|
|
{
|
|
|
|
|
$aTasks = [];
|
|
|
|
|
while ($oTask = $oTasks->Fetch()) {
|
|
|
|
|
$aTasks[$oTask->Get('class_name')] = $oTask;
|
|
|
|
|
}
|
|
|
|
|
$oNow = new DateTime();
|
|
|
|
|
|
|
|
|
|
$aProcesses = array();
|
|
|
|
|
foreach (InterfaceDiscovery::GetInstance()->FindItopClasses(iProcess::class) as $sTaskClass)
|
|
|
|
|
{
|
|
|
|
|
$aProcesses = [];
|
|
|
|
|
foreach (InterfaceDiscovery::GetInstance()->FindItopClasses(iProcess::class) as $sTaskClass) {
|
|
|
|
|
$oProcess = new $sTaskClass;
|
|
|
|
|
$aProcesses[$sTaskClass] = $oProcess;
|
|
|
|
|
|
|
|
|
|
// Create missing entry if needed
|
|
|
|
|
if (!array_key_exists($sTaskClass, $aTasks))
|
|
|
|
|
{
|
|
|
|
|
if (!array_key_exists($sTaskClass, $aTasks)) {
|
|
|
|
|
// New entry, let's create a new BackgroundTask record, and plan the first execution
|
|
|
|
|
$oTask = new BackgroundTask();
|
|
|
|
|
$oTask->SetDebug($bDebug);
|
|
|
|
|
@@ -426,41 +384,31 @@ function ReSyncProcesses($oP, $bVerbose, $bDebug)
|
|
|
|
|
$oTask->Set('max_run_duration', 0);
|
|
|
|
|
$oTask->Set('average_run_duration', 0);
|
|
|
|
|
$oRefClass = new ReflectionClass($sTaskClass);
|
|
|
|
|
if ($oRefClass->implementsInterface('iScheduledProcess'))
|
|
|
|
|
{
|
|
|
|
|
if ($oRefClass->implementsInterface('iScheduledProcess')) {
|
|
|
|
|
$oNextOcc = $oProcess->GetNextOccurrence();
|
|
|
|
|
$oTask->Set('next_run_date', $oNextOcc->format('Y-m-d H:i:s'));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
} else {
|
|
|
|
|
// Background processes do start asap, i.e. "now"
|
|
|
|
|
$oTask->Set('next_run_date', $oNow->format('Y-m-d H:i:s'));
|
|
|
|
|
}
|
|
|
|
|
if ($bVerbose)
|
|
|
|
|
{
|
|
|
|
|
$oP->p('Creating record for: '.$sTaskClass);
|
|
|
|
|
$oP->p('First execution planned at: '.$oTask->Get('next_run_date'));
|
|
|
|
|
}
|
|
|
|
|
$sDebugTaskClass = CronLog::GetDebugClassName($sTaskClass);
|
|
|
|
|
CronLog::Trace('Creating record for: '.$sDebugTaskClass);
|
|
|
|
|
CronLog::Trace('First execution planned at: '.$oTask->Get('next_run_date'));
|
|
|
|
|
$oTask->DBInsert();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
} else {
|
|
|
|
|
/** @var \BackgroundTask $oTask */
|
|
|
|
|
$oTask = $aTasks[$sTaskClass];
|
|
|
|
|
if ($oTask->Get('next_run_date') == '3000-01-01 00:00:00')
|
|
|
|
|
{
|
|
|
|
|
if ($oTask->Get('next_run_date') == '3000-01-01 00:00:00') {
|
|
|
|
|
// check for rescheduled tasks
|
|
|
|
|
$oRefClass = new ReflectionClass($sTaskClass);
|
|
|
|
|
if ($oRefClass->implementsInterface('iScheduledProcess'))
|
|
|
|
|
{
|
|
|
|
|
if ($oRefClass->implementsInterface('iScheduledProcess')) {
|
|
|
|
|
$oNextOcc = $oProcess->GetNextOccurrence();
|
|
|
|
|
$oTask->Set('next_run_date', $oNextOcc->format('Y-m-d H:i:s'));
|
|
|
|
|
$oTask->DBUpdate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Reactivate task if necessary
|
|
|
|
|
if ($oTask->Get('status') == 'removed')
|
|
|
|
|
{
|
|
|
|
|
if ($oTask->Get('status') == 'removed') {
|
|
|
|
|
$oTask->Set('status', 'active');
|
|
|
|
|
$oTask->DBUpdate();
|
|
|
|
|
}
|
|
|
|
|
@@ -470,26 +418,20 @@ function ReSyncProcesses($oP, $bVerbose, $bDebug)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove all the tasks not having a valid class
|
|
|
|
|
foreach ($aTasks as $oTask)
|
|
|
|
|
{
|
|
|
|
|
foreach ($aTasks as $oTask) {
|
|
|
|
|
$sTaskClass = $oTask->Get('class_name');
|
|
|
|
|
if (!class_exists($sTaskClass))
|
|
|
|
|
{
|
|
|
|
|
if (!class_exists($sTaskClass)) {
|
|
|
|
|
$oTask->Set('status', 'removed');
|
|
|
|
|
$oTask->DBUpdate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($bVerbose)
|
|
|
|
|
{
|
|
|
|
|
$aDisplayProcesses = array();
|
|
|
|
|
foreach ($aProcesses as $oExecInstance)
|
|
|
|
|
{
|
|
|
|
|
$aDisplayProcesses[] = get_class($oExecInstance);
|
|
|
|
|
}
|
|
|
|
|
$sDisplayProcesses = implode(', ', $aDisplayProcesses);
|
|
|
|
|
$oP->p("Background processes: ".$sDisplayProcesses);
|
|
|
|
|
$aDisplayProcesses = [];
|
|
|
|
|
foreach ($aProcesses as $oExecInstance) {
|
|
|
|
|
$aDisplayProcesses[] = get_class($oExecInstance);
|
|
|
|
|
}
|
|
|
|
|
$sDisplayProcesses = implode(', ', $aDisplayProcesses);
|
|
|
|
|
CronLog::Trace("Background processes: ".$sDisplayProcesses);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
@@ -500,118 +442,91 @@ function ReSyncProcesses($oP, $bVerbose, $bDebug)
|
|
|
|
|
set_time_limit(0); // Some background actions may really take long to finish (like backup)
|
|
|
|
|
|
|
|
|
|
$bIsModeCLI = utils::IsModeCLI();
|
|
|
|
|
if ($bIsModeCLI)
|
|
|
|
|
{
|
|
|
|
|
if ($bIsModeCLI) {
|
|
|
|
|
$oP = new CLIPage("iTop - cron");
|
|
|
|
|
|
|
|
|
|
SetupUtils::CheckPhpAndExtensionsForCli($oP, EXIT_CODE_FATAL);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
} else {
|
|
|
|
|
$oP = new WebPage("iTop - cron");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
utils::UseParamFile();
|
|
|
|
|
|
|
|
|
|
$bVerbose = utils::ReadParam('verbose', false, true /* Allow CLI */);
|
|
|
|
|
$bDebug = utils::ReadParam('debug', false, true /* Allow CLI */);
|
|
|
|
|
// Allow verbosity on output from 0 => none, 1 => debug, 2 => trace
|
|
|
|
|
// (writing debug messages to the cron.log file is configured with log_level_min config parameter)
|
|
|
|
|
$iVerbose = utils::ReadParam('verbose', 0, true /* Allow CLI */);
|
|
|
|
|
CronLog::SetDebug($oP, $iVerbose);
|
|
|
|
|
|
|
|
|
|
if ($bIsModeCLI)
|
|
|
|
|
{
|
|
|
|
|
if ($bIsModeCLI) {
|
|
|
|
|
// Next steps:
|
|
|
|
|
// specific arguments: 'csv file'
|
|
|
|
|
//
|
|
|
|
|
$sAuthUser = ReadMandatoryParam($oP, 'auth_user', 'raw_data');
|
|
|
|
|
$sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd', 'raw_data');
|
|
|
|
|
if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
|
|
|
|
|
{
|
|
|
|
|
if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd)) {
|
|
|
|
|
UserRights::Login($sAuthUser); // Login & set the user's language
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
} else {
|
|
|
|
|
$oP->p("Access wrong credentials ('$sAuthUser')");
|
|
|
|
|
$oP->output();
|
|
|
|
|
exit(EXIT_CODE_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
} else {
|
|
|
|
|
require_once(APPROOT.'/application/loginwebpage.class.inc.php');
|
|
|
|
|
LoginWebPage::DoLogin(); // Check user rights and prompt if needed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!UserRights::IsAdministrator())
|
|
|
|
|
{
|
|
|
|
|
if (!UserRights::IsAdministrator()) {
|
|
|
|
|
$oP->p("Access restricted to administrators");
|
|
|
|
|
$oP->Output();
|
|
|
|
|
exit(EXIT_CODE_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (utils::ReadParam('status_only', false, true /* Allow CLI */))
|
|
|
|
|
{
|
|
|
|
|
if (utils::ReadParam('status_only', false, true /* Allow CLI */)) {
|
|
|
|
|
// Display status and exit
|
|
|
|
|
DisplayStatus($oP);
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
require_once(APPROOT.'core/mutex.class.inc.php');
|
|
|
|
|
$oP->p("Starting: ".time().' ('.date('Y-m-d H:i:s').')');
|
|
|
|
|
}
|
|
|
|
|
catch (Exception $e)
|
|
|
|
|
{
|
|
|
|
|
catch (Exception $e) {
|
|
|
|
|
$oP->p("Error: ".$e->GetMessage());
|
|
|
|
|
$oP->output();
|
|
|
|
|
exit(EXIT_CODE_FATAL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
$oMutex = new iTopMutex('cron');
|
|
|
|
|
if (!MetaModel::DBHasAccess(ACCESS_ADMIN_WRITE))
|
|
|
|
|
{
|
|
|
|
|
$oP->p("A maintenance is ongoing");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if ($oMutex->TryLock())
|
|
|
|
|
{
|
|
|
|
|
CronExec($oP, $bVerbose, $bDebug);
|
|
|
|
|
CronLog::Enable(APPROOT.'/log/error.log');
|
|
|
|
|
try {
|
|
|
|
|
if (!MetaModel::DBHasAccess(ACCESS_ADMIN_WRITE)) {
|
|
|
|
|
CronLog::Debug("A maintenance is ongoing");
|
|
|
|
|
} else {
|
|
|
|
|
// Limit the number of cron process to run in parallel
|
|
|
|
|
$iMaxCronProcess = max(MetaModel::GetConfig()->Get('cron.max_processes'), 1);
|
|
|
|
|
$bCanRun = false;
|
|
|
|
|
$iProcessNumber = 0;
|
|
|
|
|
for ($i = 0; $i < $iMaxCronProcess; $i++) {
|
|
|
|
|
$oMutex = new iTopMutex("cron#$i");
|
|
|
|
|
if ($oMutex->TryLock()) {
|
|
|
|
|
$iProcessNumber = $i + 1;
|
|
|
|
|
$bCanRun = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Exit silently
|
|
|
|
|
$oP->p("Already running...");
|
|
|
|
|
if ($bCanRun) {
|
|
|
|
|
CronLog::$iProcessNumber = $iProcessNumber;
|
|
|
|
|
CronLog::Debug('Starting: '.time().' ('.date('Y-m-d H:i:s').')');
|
|
|
|
|
CronExec($iVerbose > 0);
|
|
|
|
|
} else {
|
|
|
|
|
CronLog::$iProcessNumber = $iMaxCronProcess + 1;
|
|
|
|
|
CronLog::Trace("The limit of $iMaxCronProcess cron process running in parallel is already reached");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception $e)
|
|
|
|
|
{
|
|
|
|
|
$oP->p("ERROR: '".$e->getMessage()."'");
|
|
|
|
|
if ($bDebug)
|
|
|
|
|
{
|
|
|
|
|
// Might contain verb parameters such a password...
|
|
|
|
|
$oP->p($e->getTraceAsString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
$oMutex->Unlock();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception $e)
|
|
|
|
|
{
|
|
|
|
|
$oP->p("ERROR: '".$e->getMessage()."'");
|
|
|
|
|
if ($bDebug)
|
|
|
|
|
{
|
|
|
|
|
// Might contain verb parameters such a password...
|
|
|
|
|
$oP->p($e->getTraceAsString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception $e) {
|
|
|
|
|
CronLog::Error("ERROR: '".$e->getMessage()."'", CronLog::CHANNEL_DEFAULT, ['stack' => $e->getTraceAsString()]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$oP->p("Exiting: ".time().' ('.date('Y-m-d H:i:s').')');
|
|
|
|
|
CronLog::Debug("Exiting: ".time().' ('.date('Y-m-d H:i:s').')');
|
|
|
|
|
$oP->Output();
|
|
|
|
|
|