mutex.class.inc.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // Copyright (C) 2013 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Class iTopMutex
  20. * A class to serialize the execution of some code sections
  21. * Emulates the API of PECL Mutex class
  22. * Relies on MySQL locks because the API sem_get is not always present in the
  23. * installed PHP.
  24. *
  25. * @copyright Copyright (C) 2013 Combodo SARL
  26. * @license http://opensource.org/licenses/AGPL-3.0
  27. */
  28. class iTopMutex
  29. {
  30. protected $sName;
  31. protected $hDBLink;
  32. public function __construct($sName)
  33. {
  34. // Compute the name of a lock for mysql
  35. // Note: the name is server-wide!!!
  36. $this->sName = 'itop.'.$sName;
  37. // It is a MUST to create a dedicated session each time a lock is required, because
  38. // using GET_LOCK anytime on the same session will RELEASE the current and unique session lock (known issue)
  39. $oConfig = utils::GetConfig();
  40. $this->InitMySQLSession($oConfig->GetDBHost(), $oConfig->GetDBUser(), $oConfig->GetDBPwd());
  41. }
  42. public function __destruct()
  43. {
  44. $this->Unlock();
  45. mysqli_close($this->hDBLink);
  46. }
  47. /**
  48. * Acquire the mutex
  49. */
  50. public function Lock()
  51. {
  52. do
  53. {
  54. $res = $this->QueryToScalar("SELECT GET_LOCK('".$this->sName."', 3600)");
  55. if (is_null($res))
  56. {
  57. throw new Exception("Failed to acquire the lock '".$this->sName."'");
  58. }
  59. // $res === '1' means I hold the lock
  60. // $res === '0' means it timed out
  61. }
  62. while ($res !== '1');
  63. }
  64. /**
  65. * Attempt to acquire the mutex
  66. * @returns bool True if the mutex is acquired, false if already locked elsewhere
  67. */
  68. public function TryLock()
  69. {
  70. $res = $this->QueryToScalar("SELECT GET_LOCK('".$this->sName."', 0)");
  71. if (is_null($res))
  72. {
  73. throw new Exception("Failed to acquire the lock '".$this->sName."'");
  74. }
  75. // $res === '1' means I hold the lock
  76. // $res === '0' means it timed out
  77. return ($res === '1');
  78. }
  79. /**
  80. * Release the mutex
  81. */
  82. public function Unlock()
  83. {
  84. $res = $this->QueryToScalar("SELECT RELEASE_LOCK('".$this->sName."')");
  85. }
  86. public function InitMySQLSession($sHost, $sUser, $sPwd)
  87. {
  88. $aConnectInfo = explode(':', $sHost);
  89. if (count($aConnectInfo) > 1)
  90. {
  91. // Override the default port
  92. $sServer = $aConnectInfo[0];
  93. $iPort = $aConnectInfo[1];
  94. $this->hDBLink = @mysqli_connect($sServer, $sUser, $sPwd, '', $iPort);
  95. }
  96. else
  97. {
  98. $this->hDBLink = @mysqli_connect($sHost, $sUser, $sPwd);
  99. }
  100. if (!$this->hDBLink)
  101. {
  102. throw new Exception("Could not connect to the DB server (host=$sHost, user=$sUser): ".mysqli_connect_error().' (mysql errno: '.mysqli_connect_errno().')');
  103. }
  104. }
  105. protected function QueryToScalar($sSql)
  106. {
  107. $result = mysqli_query($this->hDBLink, $sSql);
  108. if (!$result)
  109. {
  110. throw new Exception("Failed to issue MySQL query '".$sSql."': ".mysqli_error($this->hDBLink).' (mysql errno: '.mysqli_errno($this->hDBLink).')');
  111. }
  112. if ($aRow = mysqli_fetch_array($result, MYSQLI_BOTH))
  113. {
  114. $res = $aRow[0];
  115. }
  116. else
  117. {
  118. mysqli_free_result($result);
  119. throw new Exception("No result for query '".$sSql."'");
  120. }
  121. mysqli_free_result($result);
  122. return $res;
  123. }
  124. }