2018-06-26 16:31:04 -04:00
|
|
|
<?php
|
|
|
|
|
2018-06-28 16:57:17 -04:00
|
|
|
namespace Friendica\Core\Lock;
|
2018-06-26 16:31:04 -04:00
|
|
|
|
2018-06-28 16:57:17 -04:00
|
|
|
use Friendica\Core\Cache\ICacheDriver;
|
2018-06-26 16:31:04 -04:00
|
|
|
|
2018-06-28 16:57:17 -04:00
|
|
|
class CacheLockDriver extends AbstractLockDriver
|
2018-06-26 16:31:04 -04:00
|
|
|
{
|
2018-06-28 16:57:17 -04:00
|
|
|
/**
|
|
|
|
* @var \Friendica\Core\Cache\ICacheDriver;
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CacheLockDriver constructor.
|
|
|
|
*
|
|
|
|
* @param ICacheDriver $cache The CacheDriver for this type of lock
|
|
|
|
*/
|
|
|
|
public function __construct(ICacheDriver $cache)
|
|
|
|
{
|
|
|
|
$this->cache = $cache;
|
|
|
|
}
|
|
|
|
|
2018-06-26 16:31:04 -04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @brief Sets a lock for a given name
|
|
|
|
*
|
|
|
|
* @param string $key The Name of the lock
|
|
|
|
* @param integer $timeout Seconds until we give up
|
|
|
|
*
|
|
|
|
* @return boolean Was the lock successful?
|
|
|
|
*/
|
2018-06-28 17:06:14 -04:00
|
|
|
public function acquireLock($key, $timeout = 120)
|
2018-06-26 16:31:04 -04:00
|
|
|
{
|
|
|
|
$got_lock = false;
|
|
|
|
$start = time();
|
|
|
|
|
|
|
|
$cachekey = get_app()->get_hostname() . ";lock:" . $key;
|
|
|
|
|
|
|
|
do {
|
2018-06-28 16:57:17 -04:00
|
|
|
$lock = $this->cache->get($cachekey);
|
2018-06-26 16:31:04 -04:00
|
|
|
|
|
|
|
if (!is_bool($lock)) {
|
|
|
|
$pid = (int)$lock;
|
|
|
|
|
|
|
|
// When the process id isn't used anymore, we can safely claim the lock for us.
|
|
|
|
// Or we do want to lock something that was already locked by us.
|
|
|
|
if (!posix_kill($pid, 0) || ($pid == getmypid())) {
|
|
|
|
$lock = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_bool($lock)) {
|
2018-06-28 16:57:17 -04:00
|
|
|
$this->cache->set($cachekey, getmypid(), 300);
|
2018-06-26 16:31:04 -04:00
|
|
|
$got_lock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$got_lock && ($timeout > 0)) {
|
|
|
|
usleep(rand(10000, 200000));
|
|
|
|
}
|
|
|
|
} while (!$got_lock && ((time() - $start) < $timeout));
|
|
|
|
|
2018-06-28 16:57:17 -04:00
|
|
|
$this->markAcquire($key);
|
|
|
|
|
2018-06-26 16:31:04 -04:00
|
|
|
return $got_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Removes a lock if it was set by us
|
|
|
|
*
|
|
|
|
* @param string $key Name of the lock
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-06-28 17:06:14 -04:00
|
|
|
public function releaseLock($key)
|
2018-06-26 16:31:04 -04:00
|
|
|
{
|
|
|
|
$cachekey = get_app()->get_hostname() . ";lock:" . $key;
|
2018-06-28 16:57:17 -04:00
|
|
|
$lock = $this->cache->get($cachekey);
|
2018-06-26 16:31:04 -04:00
|
|
|
|
|
|
|
if (!is_bool($lock)) {
|
|
|
|
if ((int)$lock == getmypid()) {
|
2018-06-28 16:57:17 -04:00
|
|
|
$this->cache->delete($cachekey);
|
2018-06-26 16:31:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 16:57:17 -04:00
|
|
|
$this->markRelease($key);
|
2018-06-26 16:31:04 -04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-06-26 17:44:30 -04:00
|
|
|
}
|