CRM-18414: On changing payment processor selection on Contribution page, disable...
[civicrm-core.git] / CRM / Core / Lock.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 * $Id$
33 *
34 */
10760fa1 35class CRM_Core_Lock implements \Civi\Core\Lock\LockInterface {
6a488035 36
722ce4f9
TO
37 static $jobLog = FALSE;
38
6a488035 39 // lets have a 3 second timeout for now
7da04cde 40 const TIMEOUT = 3;
6a488035
TO
41
42 protected $_hasLock = FALSE;
43
44 protected $_name;
45
10760fa1
TO
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) {
aaffa79f 92 $serverWideLock = \Civi::settings()->get('civimail_server_wide_lock');
10760fa1
TO
93 return new static($name, NULL, $serverWideLock);
94 }
95
6a488035
TO
96 /**
97 * Initialize the constants used during lock acquire / release
98 *
6a0b768e 99 * @param string $name
10760fa1
TO
100 * Symbolic name for the lock. Names generally look like
101 * "worker.mailing.EmailProcessor" ("{category}.{component}.{AdhocName}").
102 *
103 * Categories: worker|data|cache|...
104 * Component: core|mailing|member|contribute|...
6a0b768e
TO
105 * @param int $timeout
106 * The number of seconds to wait to get the lock. 1 if not set.
107 * @param bool $serverWideLock
108 * Should this lock be applicable across your entire mysql server.
10760fa1
TO
109 * this is useful if you have multiple sites running on the same
110 * mysql server and you want to limit the number of parallel cron
111 * jobs - CRM-91XX
6a488035 112 */
00be9182 113 public function __construct($name, $timeout = NULL, $serverWideLock = FALSE) {
e32e3c19 114 $config = CRM_Core_Config::singleton();
6a488035
TO
115 $dsnArray = DB::parseDSN($config->dsn);
116 $database = $dsnArray['database'];
117 $domainID = CRM_Core_Config::domainID();
118 if ($serverWideLock) {
119 $this->_name = $name;
120 }
121 else {
122 $this->_name = $database . '.' . $domainID . '.' . $name;
123 }
f2da77e6
TO
124 if (defined('CIVICRM_LOCK_DEBUG')) {
125 CRM_Core_Error::debug_log_message('trying to construct lock for ' . $this->_name);
126 }
6a488035 127 $this->_timeout = $timeout !== NULL ? $timeout : self::TIMEOUT;
6a488035
TO
128 }
129
00be9182 130 public function __destruct() {
6a488035
TO
131 $this->release();
132 }
133
a0ee3941 134 /**
ad37ac8e 135 * Acquire lock.
136 *
137 * @param int $timeout
138 *
a0ee3941 139 * @return bool
ad37ac8e 140 * @throws \CRM_Core_Exception
a0ee3941 141 */
10760fa1 142 public function acquire($timeout = NULL) {
6a488035 143 if (!$this->_hasLock) {
c1374ea1
TO
144 if (self::$jobLog && CRM_Core_DAO::singleValueQuery("SELECT IS_USED_LOCK( '" . self::$jobLog . "')")) {
145 return $this->hackyHandleBrokenCode(self::$jobLog);
146 }
147
6a488035 148 $query = "SELECT GET_LOCK( %1, %2 )";
e32e3c19
TO
149 $params = array(
150 1 => array($this->_name, 'String'),
10760fa1 151 2 => array($timeout ? $timeout : $this->_timeout, 'Integer'),
6a488035
TO
152 );
153 $res = CRM_Core_DAO::singleValueQuery($query, $params);
154 if ($res) {
722ce4f9
TO
155 if (defined('CIVICRM_LOCK_DEBUG')) {
156 CRM_Core_Error::debug_log_message('acquire lock for ' . $this->_name);
157 }
6a488035 158 $this->_hasLock = TRUE;
c1374ea1
TO
159 if (stristr($this->_name, 'data.mailing.job.')) {
160 self::$jobLog = $this->_name;
161 }
6a488035 162 }
722ce4f9
TO
163 else {
164 if (defined('CIVICRM_LOCK_DEBUG')) {
165 CRM_Core_Error::debug_log_message('failed to acquire lock for ' . $this->_name);
166 }
167 }
6a488035
TO
168 }
169 return $this->_hasLock;
170 }
171
a0ee3941
EM
172 /**
173 * @return null|string
174 */
00be9182 175 public function release() {
6a488035 176 if ($this->_hasLock) {
10760fa1
TO
177 if (defined('CIVICRM_LOCK_DEBUG')) {
178 CRM_Core_Error::debug_log_message('release lock for ' . $this->_name);
179 }
6a488035
TO
180 $this->_hasLock = FALSE;
181
722ce4f9
TO
182 if (self::$jobLog == $this->_name) {
183 self::$jobLog = FALSE;
184 }
185
6a488035
TO
186 $query = "SELECT RELEASE_LOCK( %1 )";
187 $params = array(1 => array($this->_name, 'String'));
188 return CRM_Core_DAO::singleValueQuery($query, $params);
189 }
190 }
191
a0ee3941
EM
192 /**
193 * @return null|string
194 */
00be9182 195 public function isFree() {
6a488035
TO
196 $query = "SELECT IS_FREE_LOCK( %1 )";
197 $params = array(1 => array($this->_name, 'String'));
198 return CRM_Core_DAO::singleValueQuery($query, $params);
199 }
200
a0ee3941
EM
201 /**
202 * @return bool
203 */
00be9182 204 public function isAcquired() {
6a488035
TO
205 return $this->_hasLock;
206 }
f877d534
E
207
208 /**
209 * CRM-12856 locks were originally set up for jobs, but the concept was extended to caching & groups without
210 * understanding that would undermine the job locks (because grabbing a lock implicitly releases existing ones)
211 * this is all a big hack to mitigate the impact of that - but should not be seen as a fix. Not sure correct fix
212 * but maybe locks should be used more selectively? Or else we need to handle is some cool way that Tim is yet to write :-)
213 * if we are running in the context of the cron log then we would rather die (or at least let our process die)
214 * than release that lock - so if the attempt is being made by setCache or something relatively trivial
215 * we'll just return TRUE, but if it's another job then we will crash as that seems 'safer'
216 *
217 * @param string $jobLog
218 * @throws CRM_Core_Exception
28a04ea9 219 * @return bool
f877d534 220 */
00be9182 221 public function hackyHandleBrokenCode($jobLog) {
e32e3c19 222 if (stristr($this->_name, 'job')) {
722ce4f9
TO
223 CRM_Core_Error::debug_log_message('lock acquisition for ' . $this->_name . ' attempted when ' . $jobLog . ' is not released');
224 throw new CRM_Core_Exception('lock acquisition for ' . $this->_name . ' attempted when ' . $jobLog . ' is not released');
f877d534 225 }
f2da77e6
TO
226 if (defined('CIVICRM_LOCK_DEBUG')) {
227 CRM_Core_Error::debug_log_message('(CRM-12856) faking lock for ' . $this->_name);
228 }
f877d534
E
229 $this->_hasLock = TRUE;
230 return TRUE;
231 }
96025800 232
6a488035 233}