| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Core_Lock implements \Civi\Core\Lock\LockInterface { |
| 36 | |
| 37 | static $jobLog = FALSE; |
| 38 | |
| 39 | // lets have a 3 second timeout for now |
| 40 | const TIMEOUT = 3; |
| 41 | |
| 42 | protected $_hasLock = FALSE; |
| 43 | |
| 44 | protected $_name; |
| 45 | |
| 46 | /** |
| 47 | * Use MySQL's GET_LOCK(). Locks are shared across all Civi instances |
| 48 | * on the same MySQL server. |
| 49 | * |
| 50 | * @param string $name |
| 51 | * Symbolic name for the lock. Names generally look like |
| 52 | * "worker.mailing.EmailProcessor" ("{category}.{component}.{AdhocName}"). |
| 53 | * |
| 54 | * Categories: worker|data|cache|... |
| 55 | * Component: core|mailing|member|contribute|... |
| 56 | * @return \Civi\Core\Lock\LockInterface |
| 57 | */ |
| 58 | public static function createGlobalLock($name) { |
| 59 | return new static($name, NULL, TRUE); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Use MySQL's GET_LOCK(), but apply prefixes to the lock names. |
| 64 | * Locks are unique to each instance of Civi. |
| 65 | * |
| 66 | * @param string $name |
| 67 | * Symbolic name for the lock. Names generally look like |
| 68 | * "worker.mailing.EmailProcessor" ("{category}.{component}.{AdhocName}"). |
| 69 | * |
| 70 | * Categories: worker|data|cache|... |
| 71 | * Component: core|mailing|member|contribute|... |
| 72 | * @return \Civi\Core\Lock\LockInterface |
| 73 | */ |
| 74 | public static function createScopedLock($name) { |
| 75 | return new static($name); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Use MySQL's GET_LOCK(), but conditionally apply prefixes to the lock names |
| 80 | * (if civimail_server_wide_lock is disabled). |
| 81 | * |
| 82 | * @param string $name |
| 83 | * Symbolic name for the lock. Names generally look like |
| 84 | * "worker.mailing.EmailProcessor" ("{category}.{component}.{AdhocName}"). |
| 85 | * |
| 86 | * Categories: worker|data|cache|... |
| 87 | * Component: core|mailing|member|contribute|... |
| 88 | * @return \Civi\Core\Lock\LockInterface |
| 89 | * @deprecated |
| 90 | */ |
| 91 | public static function createCivimailLock($name) { |
| 92 | $serverWideLock = \CRM_Core_BAO_Setting::getItem( |
| 93 | \CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, |
| 94 | 'civimail_server_wide_lock' |
| 95 | ); |
| 96 | return new static($name, NULL, $serverWideLock); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Initialize the constants used during lock acquire / release |
| 101 | * |
| 102 | * @param string $name |
| 103 | * Symbolic name for the lock. Names generally look like |
| 104 | * "worker.mailing.EmailProcessor" ("{category}.{component}.{AdhocName}"). |
| 105 | * |
| 106 | * Categories: worker|data|cache|... |
| 107 | * Component: core|mailing|member|contribute|... |
| 108 | * @param int $timeout |
| 109 | * The number of seconds to wait to get the lock. 1 if not set. |
| 110 | * @param bool $serverWideLock |
| 111 | * Should this lock be applicable across your entire mysql server. |
| 112 | * this is useful if you have multiple sites running on the same |
| 113 | * mysql server and you want to limit the number of parallel cron |
| 114 | * jobs - CRM-91XX |
| 115 | */ |
| 116 | public function __construct($name, $timeout = NULL, $serverWideLock = FALSE) { |
| 117 | $config = CRM_Core_Config::singleton(); |
| 118 | $dsnArray = DB::parseDSN($config->dsn); |
| 119 | $database = $dsnArray['database']; |
| 120 | $domainID = CRM_Core_Config::domainID(); |
| 121 | if ($serverWideLock) { |
| 122 | $this->_name = $name; |
| 123 | } |
| 124 | else { |
| 125 | $this->_name = $database . '.' . $domainID . '.' . $name; |
| 126 | } |
| 127 | if (defined('CIVICRM_LOCK_DEBUG')) { |
| 128 | CRM_Core_Error::debug_log_message('trying to construct lock for ' . $this->_name); |
| 129 | } |
| 130 | $this->_timeout = $timeout !== NULL ? $timeout : self::TIMEOUT; |
| 131 | } |
| 132 | |
| 133 | public function __destruct() { |
| 134 | $this->release(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @return bool |
| 139 | */ |
| 140 | public function acquire($timeout = NULL) { |
| 141 | if (!$this->_hasLock) { |
| 142 | if (self::$jobLog && CRM_Core_DAO::singleValueQuery("SELECT IS_USED_LOCK( '" . self::$jobLog . "')")) { |
| 143 | return $this->hackyHandleBrokenCode(self::$jobLog); |
| 144 | } |
| 145 | |
| 146 | $query = "SELECT GET_LOCK( %1, %2 )"; |
| 147 | $params = array( |
| 148 | 1 => array($this->_name, 'String'), |
| 149 | 2 => array($timeout ? $timeout : $this->_timeout, 'Integer'), |
| 150 | ); |
| 151 | $res = CRM_Core_DAO::singleValueQuery($query, $params); |
| 152 | if ($res) { |
| 153 | if (defined('CIVICRM_LOCK_DEBUG')) { |
| 154 | CRM_Core_Error::debug_log_message('acquire lock for ' . $this->_name); |
| 155 | } |
| 156 | $this->_hasLock = TRUE; |
| 157 | if (stristr($this->_name, 'data.mailing.job.')) { |
| 158 | self::$jobLog = $this->_name; |
| 159 | } |
| 160 | } |
| 161 | else { |
| 162 | if (defined('CIVICRM_LOCK_DEBUG')) { |
| 163 | CRM_Core_Error::debug_log_message('failed to acquire lock for ' . $this->_name); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | return $this->_hasLock; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @return null|string |
| 172 | */ |
| 173 | public function release() { |
| 174 | if ($this->_hasLock) { |
| 175 | if (defined('CIVICRM_LOCK_DEBUG')) { |
| 176 | CRM_Core_Error::debug_log_message('release lock for ' . $this->_name); |
| 177 | } |
| 178 | $this->_hasLock = FALSE; |
| 179 | |
| 180 | if (self::$jobLog == $this->_name) { |
| 181 | self::$jobLog = FALSE; |
| 182 | } |
| 183 | |
| 184 | $query = "SELECT RELEASE_LOCK( %1 )"; |
| 185 | $params = array(1 => array($this->_name, 'String')); |
| 186 | return CRM_Core_DAO::singleValueQuery($query, $params); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @return null|string |
| 192 | */ |
| 193 | public function isFree() { |
| 194 | $query = "SELECT IS_FREE_LOCK( %1 )"; |
| 195 | $params = array(1 => array($this->_name, 'String')); |
| 196 | return CRM_Core_DAO::singleValueQuery($query, $params); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @return bool |
| 201 | */ |
| 202 | public function isAcquired() { |
| 203 | return $this->_hasLock; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * CRM-12856 locks were originally set up for jobs, but the concept was extended to caching & groups without |
| 208 | * understanding that would undermine the job locks (because grabbing a lock implicitly releases existing ones) |
| 209 | * this is all a big hack to mitigate the impact of that - but should not be seen as a fix. Not sure correct fix |
| 210 | * but maybe locks should be used more selectively? Or else we need to handle is some cool way that Tim is yet to write :-) |
| 211 | * if we are running in the context of the cron log then we would rather die (or at least let our process die) |
| 212 | * than release that lock - so if the attempt is being made by setCache or something relatively trivial |
| 213 | * we'll just return TRUE, but if it's another job then we will crash as that seems 'safer' |
| 214 | * |
| 215 | * @param string $jobLog |
| 216 | * @throws CRM_Core_Exception |
| 217 | * @return bool |
| 218 | */ |
| 219 | public function hackyHandleBrokenCode($jobLog) { |
| 220 | if (stristr($this->_name, 'job')) { |
| 221 | CRM_Core_Error::debug_log_message('lock acquisition for ' . $this->_name . ' attempted when ' . $jobLog . ' is not released'); |
| 222 | throw new CRM_Core_Exception('lock acquisition for ' . $this->_name . ' attempted when ' . $jobLog . ' is not released'); |
| 223 | } |
| 224 | if (defined('CIVICRM_LOCK_DEBUG')) { |
| 225 | CRM_Core_Error::debug_log_message('(CRM-12856) faking lock for ' . $this->_name); |
| 226 | } |
| 227 | $this->_hasLock = TRUE; |
| 228 | return TRUE; |
| 229 | } |
| 230 | |
| 231 | } |