From 48d740da2533a2f7fd6764fcfd3afa6d9f4a0784 Mon Sep 17 00:00:00 2001 From: Denis Flaven Date: Thu, 16 May 2013 14:05:38 +0000 Subject: [PATCH] #727: prevent a crash in cron.php SVN:trunk[2743] --- core/dbobject.class.php | 5 +++++ webservices/cron.php | 40 +++++++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/core/dbobject.class.php b/core/dbobject.class.php index eec82b1a0..37967d6c0 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -1785,6 +1785,11 @@ abstract class DBObject MyHelpers::CheckKeyInArray('object lifecycle stimulus', $sStimulusCode, MetaModel::EnumStimuli(get_class($this))); $aStateTransitions = $this->EnumTransitions(); + if (!array_key_exists($sStimulusCode, $aStateTransitions)) + { + // This simulus has no effect in the current state... do nothing + return; + } $aTransitionDef = $aStateTransitions[$sStimulusCode]; // Change the state before proceeding to the actions, this is necessary because an action might diff --git a/webservices/cron.php b/webservices/cron.php index 902cea76a..688920470 100644 --- a/webservices/cron.php +++ b/webservices/cron.php @@ -63,25 +63,31 @@ function UsageAndExit($oP) function RunTask($oBackgroundProcess, BackgroundTask $oTask, $oStartDate, $iTimeLimit) { - $oNow = new DateTime(); - $fStart = microtime(true); - $sMessage = $oBackgroundProcess->Process($iTimeLimit); - $fDuration = microtime(true) - $fStart; - $oTask->ComputeDurations($fDuration); - $oTask->Set('latest_run_date', $oNow->format('Y-m-d H:i:s')); - $oPlannedStart = new DateTime($oTask->Get('latest_run_date')); - // Let's assume that the task was started exactly when planned so that the schedule does no shift each time - // this allows to schedule a task everyday "around" 11:30PM for example - $oPlannedStart->modify('+'.$oBackgroundProcess->GetPeriodicity().' seconds'); - $oEnd = new DateTime(); - if ($oPlannedStart->format('U') < $oEnd->format('U')) + try { - // Huh, next planned start is already in the past, shift it of the periodicity ! - $oPlannedStart = $oEnd->modify('+'.$oBackgroundProcess->GetPeriodicity().' seconds'); + $oNow = new DateTime(); + $fStart = microtime(true); + $sMessage = $oBackgroundProcess->Process($iTimeLimit); + $fDuration = microtime(true) - $fStart; + $oTask->ComputeDurations($fDuration); + $oTask->Set('latest_run_date', $oNow->format('Y-m-d H:i:s')); + $oPlannedStart = new DateTime($oTask->Get('latest_run_date')); + // Let's assume that the task was started exactly when planned so that the schedule does no shift each time + // this allows to schedule a task everyday "around" 11:30PM for example + $oPlannedStart->modify('+'.$oBackgroundProcess->GetPeriodicity().' seconds'); + $oEnd = new DateTime(); + if ($oPlannedStart->format('U') < $oEnd->format('U')) + { + // Huh, next planned start is already in the past, shift it of the periodicity ! + $oPlannedStart = $oEnd->modify('+'.$oBackgroundProcess->GetPeriodicity().' seconds'); + } + $oTask->Set('next_run_date', $oPlannedStart->format('Y-m-d H:i:s')); + $oTask->DBUpdate(); + } + catch(Exception $e) + { + $sMessage = 'Processing failed, the following exception occured: '.$e->getMessage(); } - $oTask->Set('next_run_date', $oPlannedStart->format('Y-m-d H:i:s')); - $oTask->DBUpdate(); - return $sMessage; }