From d88249eabc2d45c3c85f255ae039c8d96718b061 Mon Sep 17 00:00:00 2001 From: Denis Flaven Date: Tue, 14 Jun 2016 16:11:28 +0000 Subject: [PATCH] Trying to protect the Synchro for timeouts, since the synchro may be launched from the web (as a "web service", especially by the "collectors"). To be validated further with large amounts of data. SVN:trunk[4210] --- synchro/synchro_import.php | 6 +++++- synchro/synchrodatasource.class.inc.php | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/synchro/synchro_import.php b/synchro/synchro_import.php index 5c2abe17d..11adf586b 100644 --- a/synchro/synchro_import.php +++ b/synchro/synchro_import.php @@ -491,11 +491,14 @@ try // Prepare insert columns $sInsertColumns = '`'.implode('`, `', $aInputColumns).'`'; + $iPreviousTimeLimit = ini_get('max_execution_time'); + $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop'); $oMutex = new iTopMutex('synchro_import_'.$oDataSource->GetKey()); $oMutex->Lock(); foreach($aData as $iRow => $aRow) { - $sReconciliationCondition = "`primary_key` = ".CMDBSource::Quote($aRow[$iPrimaryKeyCol]); + set_time_limit($iLoopTimeLimit); + $sReconciliationCondition = "`primary_key` = ".CMDBSource::Quote($aRow[$iPrimaryKeyCol]); $sSelect = "SELECT COUNT(*) FROM `$sTable` WHERE $sReconciliationCondition"; $aRes = CMDBSource::QueryToArray($sSelect); $iCount = $aRes[0]['COUNT(*)']; @@ -632,6 +635,7 @@ try } } $oMutex->Unlock(); + set_time_limit($iPreviousTimeLimit); if (($sOutput == "summary") || ($sOutput == 'details')) { diff --git a/synchro/synchrodatasource.class.inc.php b/synchro/synchrodatasource.class.inc.php index 38cf290c4..d80c355ab 100644 --- a/synchro/synchrodatasource.class.inc.php +++ b/synchro/synchrodatasource.class.inc.php @@ -2528,6 +2528,7 @@ class SynchroExecution $aArguments['step_count'] = $iStepCount; $iStepCount++; + set_time_limit(0); // On Linux the time spent outside of the script does not count, but on Windows it does, so let give us time ! list ($iRes, $aOut) = utils::ExecITopScript('synchro/priv_sync_chunk.php', $aArguments); // Reload the log that has been modified by the processes @@ -2618,6 +2619,7 @@ class SynchroExecution protected function DoJob1($iMaxReplica = null, $iCurrPos = -1) { $sLimitDate = $this->m_oLastFullLoadStartDate->Format('Y-m-d H:i:s'); + $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop'); // Get all the replicas that were not seen in the last import and mark them as obsolete $sDeletePolicy = $this->m_oDataSource->Get('delete_policy'); @@ -2646,6 +2648,7 @@ class SynchroExecution $iLastReplicaProcessed = -1; while($oReplica = $oSetToProcess->Fetch()) { + set_time_limit($iLoopTimeLimit); $iLastReplicaProcessed = $oReplica->GetKey(); switch ($sDeletePolicy) { @@ -2720,7 +2723,8 @@ class SynchroExecution protected function DoJob2($iMaxReplica = null, $iCurrPos = -1) { $sLimitDate = $this->m_oLastFullLoadStartDate->Format('Y-m-d H:i:s'); - + $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop'); + // Get all the replicas that are 'new' or modified or synchronized with a warning // $sSelectToSync = "SELECT SynchroReplica WHERE id > :curr_pos AND (status = 'new' OR status = 'modified' OR (status = 'synchronized' AND status_last_warning != '')) AND sync_source_id = :source_id AND status_last_seen >= :last_import"; @@ -2742,6 +2746,7 @@ class SynchroExecution $iLastReplicaProcessed = -1; while($oReplica = $oSetToProcess->Fetch()) { + set_time_limit($iLoopTimeLimit); $iLastReplicaProcessed = $oReplica->GetKey(); $oReplica->Synchro($this->m_oDataSource, $this->m_aReconciliationKeys, $this->m_aAttributes, $this->m_oChange, $this->m_oStatLog); $oReplica->DBUpdateTracked($this->m_oChange); @@ -2771,6 +2776,8 @@ class SynchroExecution */ protected function DoJob3($iMaxReplica = null, $iCurrPos = -1) { + $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop'); + $sDeletePolicy = $this->m_oDataSource->Get('delete_policy'); if ($sDeletePolicy != 'update_then_delete') { @@ -2815,6 +2822,7 @@ class SynchroExecution $iLastReplicaProcessed = -1; while($oReplica = $oSetToProcess->Fetch()) { + set_time_limit($iLoopTimeLimit); $iLastReplicaProcessed = $oReplica->GetKey(); $this->m_oStatLog->AddTrace("Destination object to be DELETED", $oReplica); $oReplica->DeleteDestObject($this->m_oChange, $this->m_oStatLog);