diff --git a/core/mutex.class.inc.php b/core/mutex.class.inc.php index ea459d0bf..c0d8521f7 100644 --- a/core/mutex.class.inc.php +++ b/core/mutex.class.inc.php @@ -1,5 +1,5 @@ bLocked) + { + return true; // Already acquired + } + if (self::$aAcquiredLocks[$this->sName] > 0) + { + return true; + } + + $res = $this->QueryToScalar("SELECT IS_FREE_LOCK('".$this->sName."')"); // IS_FREE_LOCK detects some error cases that IS_USED_LOCK do not detect + if (is_null($res)) + { + $sMsg = "MySQL Error, IS_FREE_LOCK('".$this->sName."') returned null. Error (".mysqli_errno($this->hDBLink).") = '".mysqli_error($this->hDBLink)."'"; + IssueLog::Error($sMsg); + throw new Exception($sMsg); + } + else if ($res == '1') + { + // Lock is free + return false; + } + return true; + } /** * Release the mutex diff --git a/datamodels/2.x/itop-backup/ajax.backup.php b/datamodels/2.x/itop-backup/ajax.backup.php index 9e331c9ed..d6049a480 100644 --- a/datamodels/2.x/itop-backup/ajax.backup.php +++ b/datamodels/2.x/itop-backup/ajax.backup.php @@ -1,5 +1,5 @@ TryLock()) + if (!$oRestoreMutex->IsLocked()) { - $oRestoreMutex->Unlock(); $sFile = utils::ReadParam('file', '', false, 'raw_data'); $sToken = str_replace(' ', '', (string)microtime()); $sTokenFile = APPROOT.'/data/restore.'.$sToken.'.tok'; diff --git a/datamodels/2.x/itop-backup/main.itop-backup.php b/datamodels/2.x/itop-backup/main.itop-backup.php index d2cc37afa..e1803d5ba 100644 --- a/datamodels/2.x/itop-backup/main.itop-backup.php +++ b/datamodels/2.x/itop-backup/main.itop-backup.php @@ -1,5 +1,5 @@ TryLock()) - { - $oBackupMutex->Unlock(); - } - else - { - // Not needed: the DB dump is done in a single transaction - //MetaModel::GetConfig()->Set('access_mode', ACCESS_READONLY, 'itop-backup'); - //MetaModel::GetConfig()->Set('access_message', ' - '.dict::S('bkp-backup-running'), 'itop-backup'); - } - $oRestoreMutex = new iTopMutex('restore.'.utils::GetCurrentEnvironment()); - if ($oRestoreMutex->TryLock()) - { - $oRestoreMutex->Unlock(); - } - else + if ($oRestoreMutex->IsLocked()) { IssueLog::Info(__class__.'::'.__function__.' A user is trying to use iTop while a restore is running. The requested page is in read-only mode.'); MetaModel::GetConfig()->Set('access_mode', ACCESS_READONLY, 'itop-backup'); diff --git a/datamodels/2.x/itop-backup/status.php b/datamodels/2.x/itop-backup/status.php index bcb3e7883..5666aa169 100644 --- a/datamodels/2.x/itop-backup/status.php +++ b/datamodels/2.x/itop-backup/status.php @@ -1,5 +1,5 @@ TryLock()) + if ($oRestoreMutex->IsLocked()) { - $oRestoreMutex->Unlock(); - $sDisableRestore = ''; + $sDisableRestore = 'disabled="disabled"'; } else { - $sDisableRestore = 'disabled="disabled"'; + $sDisableRestore = ''; } // 1st table: list the backups made in the background @@ -271,20 +270,12 @@ try // Ongoing operation ? // $oBackupMutex = new iTopMutex('backup.'.utils::GetCurrentEnvironment()); - if ($oBackupMutex->TryLock()) - { - $oBackupMutex->Unlock(); - } - else + if ($oBackupMutex->IsLocked()) { $oP->p(Dict::S('bkp-backup-running')); } $oRestoreMutex = new iTopMutex('restore.'.utils::GetCurrentEnvironment()); - if ($oRestoreMutex->TryLock()) - { - $oRestoreMutex->Unlock(); - } - else + if ($oRestoreMutex->IsLocked()) { $oP->p(Dict::S('bkp-restore-running')); } diff --git a/setup/wizardsteps.class.inc.php b/setup/wizardsteps.class.inc.php index 268f5afb3..072f860b7 100644 --- a/setup/wizardsteps.class.inc.php +++ b/setup/wizardsteps.class.inc.php @@ -1,5 +1,5 @@ oWizard->GetParameter('db_user', ''), $this->oWizard->GetParameter('db_pwd', '') ); - if ($oMutex->TryLock()) - { - $oMutex->Unlock(); - } - else + if ($oMutex->IsLocked()) { $oPage->p(" An iTop CRON process is being executed on the target database. It is highly recommended to stop any iTop CRON process prior to running the setup program."); }