From 3c9ace5b530ea444d62a3f0430313be6755b57fd Mon Sep 17 00:00:00 2001 From: Denis Flaven Date: Mon, 15 Dec 2014 15:04:43 +0000 Subject: [PATCH] #1039: prevent concurrent executions of either synchro_import.php or synchro_exec.php for a given data source, since it would lead to unpredictable results. SVN:trunk[3465] --- synchro/synchro_import.php | 9 ++++++--- synchro/synchrodatasource.class.inc.php | 8 +++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/synchro/synchro_import.php b/synchro/synchro_import.php index 135c16b09..b4c0c7428 100644 --- a/synchro/synchro_import.php +++ b/synchro/synchro_import.php @@ -468,8 +468,10 @@ try // Prepare insert columns $sInsertColumns = '`'.implode('`, `', $aInputColumns).'`'; - foreach($aData as $iRow => $aRow) - { + $oMutex = new iTopMutex('synchro_import_'.$oDataSource->GetKey().'_'.$oConfig->GetDBName().'_'.$oConfig->GetDBSubname()); + $oMutex->Lock(); + foreach($aData as $iRow => $aRow) + { $sReconciliationCondition = "`primary_key` = ".CMDBSource::Quote($aRow[$iPrimaryKeyCol]); $sSelect = "SELECT COUNT(*) FROM `$sTable` WHERE $sReconciliationCondition"; $aRes = CMDBSource::QueryToArray($sSelect); @@ -597,7 +599,8 @@ try $oP->add("$iRow: Error - Failed to reconcile, found $iCount rows having '$sReconciliationCondition'\n"); } } - + $oMutex->Unlock(); + if (($sOutput == "summary") || ($sOutput == 'details')) { $oP->add_comment("Data Source: ".$iDataSourceId); diff --git a/synchro/synchrodatasource.class.inc.php b/synchro/synchrodatasource.class.inc.php index ff2508032..c2fabe628 100644 --- a/synchro/synchrodatasource.class.inc.php +++ b/synchro/synchrodatasource.class.inc.php @@ -2414,10 +2414,14 @@ class SynchroExecution $this->PrepareLogs(); self::$m_oCurrentTask = $this->m_oDataSource; + + $oMutex = new iTopMutex('synchro_process_'.$this->m_oDataSource->GetKey().'_'.$oConfig->GetDBName().'_'.$oConfig->GetDBSubname()); try { + $oMutex->Lock(); $this->DoSynchronize(); - + $oMutex->Unlock(); + $this->m_oStatLog->Set('end_date', time()); $this->m_oStatLog->Set('status', 'completed'); $this->m_oStatLog->DBUpdateTracked($this->m_oChange); @@ -2459,6 +2463,7 @@ class SynchroExecution } catch (SynchroExceptionNotStarted $e) { + $oMutex->Unlock(); // Set information for reporting... but delete the object in DB $this->m_oStatLog->Set('end_date', time()); $this->m_oStatLog->Set('status', 'error'); @@ -2468,6 +2473,7 @@ class SynchroExecution } catch (Exception $e) { + $oMutex->Unlock(); $this->m_oStatLog->Set('end_date', time()); $this->m_oStatLog->Set('status', 'error'); $this->m_oStatLog->Set('last_error', $e->getMessage());