b6183b8aa738a6a8352d6c9959377ab713589e1e
2 /***************************************************************
5 * (c) 2008-2011 Michael Stucki (michael@typo3.org)
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
28 * Class for providing locking features in TYPO3
30 * @author Michael Stucki <michael@typo3.org>
36 * This class provides an abstract layer to various locking features for TYPO3
38 * It is intended to blocks requests until some data has been generated.
39 * This is especially useful if two clients are requesting the same website short after each other. While the request of client 1 triggers building and caching of the website, client 2 will be waiting at this lock.
41 * @author Michael Stucki <michael@typo3.org>
44 * @see class.t3lib_tstemplate.php, class.tslib_fe.php
49 * @var string Locking method: One of 'simple', 'flock', 'semaphore' or 'disable'
54 * @var mixed Identifier used for this lock
59 * @var mixed Resource used for this lock (can be a file or a semaphore resource)
64 * @var resource File pointer if using flock method
66 protected $filepointer;
69 * @var boolean True if lock is acquired
71 protected $isAcquired = FALSE;
74 * @var integer Number of times a locked resource is tried to be acquired. Only used in manual locks method "simple".
76 protected $loops = 150;
79 * @var integer Milliseconds after lock acquire is retried. $loops * $step results in the maximum delay of a lock. Only used in manual lock method "simple".
81 protected $step = 200;
84 * @var string Logging facility
86 protected $syslogFacility = 'cms';
89 * @var boolean True if locking should be logged
91 protected $isLoggingEnabled = TRUE;
96 * initializes locking, check input parameters and set variables accordingly.
98 * @param string $id ID to identify this lock in the system
99 * @param string $method Define which locking method to use. Defaults to "simple".
100 * @param integer $loops Number of times a locked resource is tried to be acquired. Only used in manual locks method "simple".
101 * @param integer step Milliseconds after lock acquire is retried. $loops * $step results in the maximum delay of a lock. Only used in manual lock method "simple".
103 public function __construct($id, $method = 'simple', $loops = 0, $step = 0) {
104 // Force ID to be string
107 if (intval($loops)) {
108 $this->loops
= intval($loops);
111 $this->step
= intval($step);
114 $this->method
= $method;
116 switch ($this->method
) {
119 $path = PATH_site
. 'typo3temp/locks/';
120 if (!is_dir($path)) {
121 t3lib_div
::mkdir($path);
123 $this->id
= md5($id);
124 $this->resource = $path . $this->id
;
127 $this->id
= abs(crc32($id));
128 if (($this->resource = sem_get($this->id
, 1)) === FALSE) {
129 throw new RuntimeException(
130 'Unable to get semaphore',
138 throw new InvalidArgumentException(
139 'No such method "' . $method . '"',
147 * Releases lock automatically when instance is destroyed.
151 function __destruct() {
156 * Acquire a lock and return when successful. If the lock is already open, the client will be
158 * It is important to know that the lock will be acquired in any case, even if the request was blocked first. Therefore, the lock needs to be released in every situation.
160 * @return boolean Returns TRUE if lock could be acquired without waiting, FALSE otherwise.
162 public function acquire() {
163 $noWait = TRUE; // Default is TRUE, which means continue without caring for other clients. In the case of TYPO3s cache management, this has no negative effect except some resource overhead.
166 switch ($this->method
) {
168 if (is_file($this->resource)) {
169 $this->sysLog('Waiting for a different process to release the lock');
170 $maxExecutionTime = ini_get('max_execution_time');
171 $maxAge = time() - ($maxExecutionTime ?
$maxExecutionTime : 120);
172 if (@filectime
($this->resource) < $maxAge) {
173 @unlink
($this->resource);
174 $this->sysLog('Unlink stale lockfile');
179 for ($i = 0; $i < $this->loops
; $i++
) {
180 $filepointer = @fopen
($this->resource, 'x');
181 if ($filepointer !== FALSE) {
182 fclose($filepointer);
183 $this->sysLog('Lock aquired');
184 $noWait = ($i === 0);
188 usleep($this->step
* 1000);
192 throw new RuntimeException('Lock file could not be created', 1294586098);
195 t3lib_div
::fixPermissions($this->resource);
198 if (($this->filepointer
= fopen($this->resource, 'w+')) == FALSE) {
199 throw new RuntimeException('Lock file could not be opened', 1294586099);
202 if (flock($this->filepointer
, LOCK_EX | LOCK_NB
) == TRUE) { // Lock without blocking
204 } elseif (flock($this->filepointer
, LOCK_EX
) == TRUE) { // Lock with blocking (waiting for similar locks to become released)
207 throw new RuntimeException('Could not lock file "' . $this->resource . '"', 1294586100);
211 if (sem_acquire($this->resource)) {
212 // Unfortunately it seems not possible to find out if the request was blocked, so we return FALSE in any case to make sure the operation is tried again.
222 $this->isAcquired
= $isAcquired;
229 * @return boolean Returns TRUE on success or FALSE on failure
231 public function release() {
232 if (!$this->isAcquired
) {
237 switch ($this->method
) {
239 if (t3lib_div
::isAllowedAbsPath($this->resource) && t3lib_div
::isFirstPartOfStr($this->resource, PATH_site
. 'typo3temp/locks/')) {
240 if (@unlink
($this->resource) == FALSE) {
246 if (flock($this->filepointer
, LOCK_UN
) == FALSE) {
249 fclose($this->filepointer
);
250 if (t3lib_div
::isAllowedAbsPath($this->resource) && t3lib_div
::isFirstPartOfStr($this->resource, PATH_site
. 'typo3temp/locks/')) {
251 @unlink
($this->resource);
255 if (@sem_release
($this->resource)) {
256 sem_remove($this->resource);
266 $this->isAcquired
= FALSE;
271 * Return the locking method which is currently used
273 * @return string Locking method
275 public function getMethod() {
276 return $this->method
;
280 * Return the ID which is currently used
282 * @return string Locking ID
284 public function getId() {
289 * Return the resource which is currently used.
290 * Depending on the locking method this can be a filename or a semaphore resource.
292 * @return mixed Locking resource (filename as string or semaphore as resource)
294 public function getResource() {
295 return $this->resource;
299 * Return the status of a lock
301 * @return string Returns TRUE if lock is acquired, FALSE otherwise
303 public function getLockStatus() {
304 return $this->isAcquired
;
308 * Sets the facility (extension name) for the syslog entry.
310 * @param string $syslogFacility
312 public function setSyslogFacility($syslogFacility) {
313 $this->syslogFacility
= $syslogFacility;
317 * Enable/ disable logging
319 * @param boolean $isLoggingEnabled
321 public function setEnableLogging($isLoggingEnabled) {
322 $this->isLoggingEnabled
= $isLoggingEnabled;
326 * Adds a common log entry for this locking API using t3lib_div::sysLog().
327 * Example: 25-02-08 17:58 - cms: Locking [simple::0aeafd2a67a6bb8b9543fb9ea25ecbe2]: Acquired
329 * @param string $message: The message to be logged
330 * @param integer $severity: Severity - 0 is info (default), 1 is notice, 2 is warning, 3 is error, 4 is fatal error
333 public function sysLog($message, $severity = 0) {
334 if ($this->isLoggingEnabled
) {
335 t3lib_div
::sysLog('Locking [' . $this->method
. '::' . $this->id
. ']: ' . trim($message), $this->syslogFacility
, $severity);