From 1aa489890c64307a48bcdf161d84adf0bf06204b Mon Sep 17 00:00:00 2001 From: Denis Flaven Date: Fri, 12 Jun 2015 17:00:41 +0000 Subject: [PATCH] Make sure that the SQL mutexes are specific to the current iTop instance, but still preserving the capability for the setup to detect an already running cron job with or without a valid config file. SVN:trunk[3591] --- core/mutex.class.inc.php | 14 ++++++++++++-- setup/wizardsteps.class.inc.php | 2 +- synchro/synchro_import.php | 2 +- synchro/synchrodatasource.class.inc.php | 2 +- webservices/cron.php | 2 +- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/core/mutex.class.inc.php b/core/mutex.class.inc.php index aa3c3bae3..8c7a7a918 100644 --- a/core/mutex.class.inc.php +++ b/core/mutex.class.inc.php @@ -37,8 +37,19 @@ class iTopMutex public function __construct($sName, $sDBHost = null, $sDBUser = null, $sDBPwd = null) { // Compute the name of a lock for mysql - // Note: the name is server-wide!!! + // Note: names are server-wide!!! So let's make the name specific to this iTop instance + $oConfig = utils::GetConfig(); // Will return an empty config when called during the setup + $sDBName = $oConfig->GetDBName(); + $sDBSubname = $oConfig->GetDBSubname(); $this->sName = 'itop.'.$sName; + if (substr($sName, -strlen($sDBName.$sDBSubname)) != $sDBName.$sDBSubname) + { + // If the name supplied already ends with the expected suffix + // don't add it twice, since the setup may try to detect an already + // running cron job by its mutex, without knowing if the config already exists or not + $this->sName .= $sDBName.$sDBSubname; + } + $this->bLocked = false; // Not yet locked if (!array_key_exists($this->sName, self::$aAcquiredLocks)) @@ -48,7 +59,6 @@ class iTopMutex // It is a MUST to create a dedicated session each time a lock is required, because // using GET_LOCK anytime on the same session will RELEASE the current and unique session lock (known issue) - $oConfig = utils::GetConfig(); $sDBHost = is_null($sDBHost) ? $oConfig->GetDBHost() : $sDBHost; $sDBUser = is_null($sDBUser) ? $oConfig->GetDBUser() : $sDBUser; $sDBPwd = is_null($sDBPwd) ? $oConfig->GetDBPwd() : $sDBPwd; diff --git a/setup/wizardsteps.class.inc.php b/setup/wizardsteps.class.inc.php index 3f5515beb..a9fcfba2d 100644 --- a/setup/wizardsteps.class.inc.php +++ b/setup/wizardsteps.class.inc.php @@ -618,7 +618,7 @@ EOF ); $oMutex = new iTopMutex( - 'cron.'.$this->oWizard->GetParameter('db_name', '').'_'.$this->oWizard->GetParameter('db_prefix', ''), + 'cron'.$this->oWizard->GetParameter('db_name', '').$this->oWizard->GetParameter('db_prefix', ''), $this->oWizard->GetParameter('db_server', ''), $this->oWizard->GetParameter('db_user', ''), $this->oWizard->GetParameter('db_pwd', '') diff --git a/synchro/synchro_import.php b/synchro/synchro_import.php index b238a8db7..88817aaba 100644 --- a/synchro/synchro_import.php +++ b/synchro/synchro_import.php @@ -468,7 +468,7 @@ try // Prepare insert columns $sInsertColumns = '`'.implode('`, `', $aInputColumns).'`'; - $oMutex = new iTopMutex('synchro_import_'.$oDataSource->GetKey().'_'.MetaModel::GetConfig()->GetDBName().'_'.MetaModel::GetConfig()->GetDBSubname()); + $oMutex = new iTopMutex('synchro_import_'.$oDataSource->GetKey()); $oMutex->Lock(); foreach($aData as $iRow => $aRow) { diff --git a/synchro/synchrodatasource.class.inc.php b/synchro/synchrodatasource.class.inc.php index 6f66f4bcb..a79e0808a 100644 --- a/synchro/synchrodatasource.class.inc.php +++ b/synchro/synchrodatasource.class.inc.php @@ -2415,7 +2415,7 @@ class SynchroExecution self::$m_oCurrentTask = $this->m_oDataSource; - $oMutex = new iTopMutex('synchro_process_'.$this->m_oDataSource->GetKey().'_'.MetaModel::GetConfig()->GetDBName().'_'.MetaModel::GetConfig()->GetDBSubname()); + $oMutex = new iTopMutex('synchro_process_'.$this->m_oDataSource->GetKey()); try { $oMutex->Lock(); diff --git a/webservices/cron.php b/webservices/cron.php index 43b5b01f5..588f687ba 100644 --- a/webservices/cron.php +++ b/webservices/cron.php @@ -354,7 +354,7 @@ $oP->p("Starting: ".time().' ('.date('Y-m-d H:i:s').')'); try { $oConfig = utils::GetConfig(); - $oMutex = new iTopMutex('cron.'.$oConfig->GetDBName().'_'.$oConfig->GetDBSubname()); + $oMutex = new iTopMutex('cron'); if ($oMutex->TryLock()) { // Note: testing this now in case some of the background processes forces the read-only mode for a while