2011-01-07 20:58:28 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package phpBB3
|
|
|
|
* @copyright (c) 2010 phpBB Group
|
2011-12-31 13:32:52 +00:00
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
2011-01-07 20:58:28 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-09-10 14:01:09 +02:00
|
|
|
namespace phpbb\lock;
|
|
|
|
|
2011-01-07 20:58:28 +01:00
|
|
|
/**
|
|
|
|
* Database locking class
|
|
|
|
* @package phpBB3
|
|
|
|
*/
|
2013-09-10 14:01:09 +02:00
|
|
|
class db
|
2011-01-07 20:58:28 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Name of the config variable this lock uses
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $config_name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unique identifier for this lock.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $unique_id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the state of this lock
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $locked;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The phpBB configuration
|
2013-09-10 14:01:09 +02:00
|
|
|
* @var \phpbb\config\config
|
2011-01-07 20:58:28 +01:00
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A database connection
|
2014-03-17 13:29:35 +01:00
|
|
|
* @var \phpbb\db\driver\driver_interface
|
2011-01-07 20:58:28 +01:00
|
|
|
*/
|
|
|
|
private $db;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a named released instance of the lock.
|
|
|
|
*
|
2011-01-13 02:17:24 +01:00
|
|
|
* You have to call acquire() to actually create the lock.
|
2011-01-07 20:58:28 +01:00
|
|
|
*
|
|
|
|
* @param string $config_name A config variable to be used for locking
|
|
|
|
* @param array $config The phpBB configuration
|
2014-03-17 13:29:35 +01:00
|
|
|
* @param \phpbb\db\driver\driver_interface $db A database connection
|
2011-01-07 20:58:28 +01:00
|
|
|
*/
|
2014-03-17 13:29:35 +01:00
|
|
|
public function __construct($config_name, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db)
|
2011-01-07 20:58:28 +01:00
|
|
|
{
|
|
|
|
$this->config_name = $config_name;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->db = $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-12 22:43:09 +01:00
|
|
|
* Tries to acquire the lock by updating
|
|
|
|
* the configuration variable in the database.
|
2011-01-07 20:58:28 +01:00
|
|
|
*
|
|
|
|
* As a lock may only be held by one process at a time, lock
|
|
|
|
* acquisition may fail if another process is holding the lock
|
|
|
|
* or if another process obtained the lock but never released it.
|
|
|
|
* Locks are forcibly released after a timeout of 1 hour.
|
|
|
|
*
|
|
|
|
* @return bool true if lock was acquired
|
|
|
|
* false otherwise
|
|
|
|
*/
|
2011-01-13 02:03:16 +01:00
|
|
|
public function acquire()
|
2011-01-07 20:58:28 +01:00
|
|
|
{
|
|
|
|
if ($this->locked)
|
|
|
|
{
|
2011-01-13 02:03:16 +01:00
|
|
|
return false;
|
2011-01-07 20:58:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($this->config[$this->config_name]))
|
|
|
|
{
|
2011-01-12 22:43:09 +01:00
|
|
|
$this->config->set($this->config_name, '0', false);
|
2011-01-07 20:58:28 +01:00
|
|
|
}
|
|
|
|
$lock_value = $this->config[$this->config_name];
|
|
|
|
|
2011-01-12 22:43:09 +01:00
|
|
|
// make sure lock cannot be acquired by multiple processes
|
2011-01-07 20:58:28 +01:00
|
|
|
if ($lock_value)
|
|
|
|
{
|
|
|
|
// if the other process is running more than an hour already we have to assume it
|
|
|
|
// aborted without cleaning the lock
|
|
|
|
$time = explode(' ', $lock_value);
|
|
|
|
$time = $time[0];
|
|
|
|
|
|
|
|
if ($time + 3600 >= time())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->unique_id = time() . ' ' . unique_id();
|
|
|
|
|
2011-01-12 22:43:09 +01:00
|
|
|
// try to update the config value, if it was already modified by another
|
|
|
|
// process we failed to acquire the lock.
|
|
|
|
$this->locked = $this->config->set_atomic($this->config_name, $lock_value, $this->unique_id, false);
|
2011-01-07 20:58:28 +01:00
|
|
|
|
|
|
|
return $this->locked;
|
|
|
|
}
|
|
|
|
|
2013-04-30 15:48:29 +02:00
|
|
|
/**
|
|
|
|
* Does this process own the lock?
|
|
|
|
*
|
|
|
|
* @return bool true if lock is owned
|
|
|
|
* false otherwise
|
|
|
|
*/
|
|
|
|
public function owns_lock()
|
|
|
|
{
|
|
|
|
return (bool) $this->locked;
|
|
|
|
}
|
|
|
|
|
2011-01-07 20:58:28 +01:00
|
|
|
/**
|
2011-01-12 22:43:09 +01:00
|
|
|
* Releases the lock.
|
2011-01-07 20:58:28 +01:00
|
|
|
*
|
2011-01-13 02:03:16 +01:00
|
|
|
* The lock must have been previously obtained, that is, acquire() call
|
2011-01-07 20:58:28 +01:00
|
|
|
* was issued and returned true.
|
|
|
|
*
|
2011-01-12 22:43:09 +01:00
|
|
|
* Note: Attempting to release a lock that is already released,
|
2011-01-13 02:03:16 +01:00
|
|
|
* that is, calling release() multiple times, is harmless.
|
2011-01-07 20:58:28 +01:00
|
|
|
*
|
2012-11-30 23:03:06 -05:00
|
|
|
* @return null
|
2011-01-07 20:58:28 +01:00
|
|
|
*/
|
2011-01-13 02:03:16 +01:00
|
|
|
public function release()
|
2011-01-07 20:58:28 +01:00
|
|
|
{
|
|
|
|
if ($this->locked)
|
|
|
|
{
|
2011-01-12 22:43:09 +01:00
|
|
|
$this->config->set_atomic($this->config_name, $this->unique_id, '0', false);
|
2011-01-07 20:58:28 +01:00
|
|
|
$this->locked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|