mirror of
https://github.com/friendica/friendica
synced 2025-04-29 13:04:23 +02:00
Console Lock
WIP
This commit is contained in:
parent
425876316f
commit
41e2031e6b
9 changed files with 420 additions and 25 deletions
|
@ -7,6 +7,11 @@ use Friendica\Core\Cache\IMemoryCache;
|
|||
|
||||
class CacheLock extends Lock
|
||||
{
|
||||
/**
|
||||
* @var string The static prefix of all locks inside the cache
|
||||
*/
|
||||
const CACHE_PREFIX = 'lock:';
|
||||
|
||||
/**
|
||||
* @var \Friendica\Core\Cache\ICache;
|
||||
*/
|
||||
|
@ -25,7 +30,7 @@ class CacheLock extends Lock
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
||||
public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
|
||||
{
|
||||
$got_lock = false;
|
||||
$start = time();
|
||||
|
@ -85,6 +90,46 @@ class CacheLock extends Lock
|
|||
return isset($lock) && ($lock !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->cache->getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getLocks(string $prefix = '')
|
||||
{
|
||||
$locks = $this->cache->getAllKeys(self::CACHE_PREFIX . $prefix);
|
||||
|
||||
array_walk($locks, function (&$lock, $key) {
|
||||
$lock = substr($lock, strlen(self::CACHE_PREFIX));
|
||||
});
|
||||
|
||||
return $locks;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function releaseAll($override = false)
|
||||
{
|
||||
$success = parent::releaseAll($override);
|
||||
|
||||
$locks = $this->getLocks();
|
||||
|
||||
foreach ($locks as $lock) {
|
||||
if (!$this->releaseLock($lock, $override)) {
|
||||
$success = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key The original key
|
||||
*
|
||||
|
@ -92,6 +137,6 @@ class CacheLock extends Lock
|
|||
*/
|
||||
private static function getLockKey($key)
|
||||
{
|
||||
return "lock:" . $key;
|
||||
return self::CACHE_PREFIX . $key;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,9 +92,16 @@ class DatabaseLock extends Lock
|
|||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function releaseAll()
|
||||
public function releaseAll($override = false)
|
||||
{
|
||||
$return = $this->dba->delete('locks', ['pid' => $this->pid]);
|
||||
$success = parent::releaseAll($override);
|
||||
|
||||
if ($override) {
|
||||
$where = ['1 = 1'];
|
||||
} else {
|
||||
$where = ['pid' => $this->pid];
|
||||
}
|
||||
$return = $this->dba->delete('locks', $where);
|
||||
|
||||
$this->acquiredLocks = [];
|
||||
|
||||
|
@ -114,4 +121,34 @@ class DatabaseLock extends Lock
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_DATABASE;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getLocks(string $prefix = '')
|
||||
{
|
||||
if (empty($prefix)) {
|
||||
$where = ['`expires` >= ?', DateTimeFormat::utcNow()];
|
||||
} else {
|
||||
$where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
|
||||
}
|
||||
|
||||
$stmt = $this->dba->select('locks', ['name'], $where);
|
||||
|
||||
$keys = [];
|
||||
while ($key = $this->dba->fetch($stmt)) {
|
||||
array_push($keys, $key['name']);
|
||||
}
|
||||
$this->dba->close($stmt);
|
||||
|
||||
return $keys;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,25 @@ interface ILock
|
|||
/**
|
||||
* Releases all lock that were set by us
|
||||
*
|
||||
* @param bool $override Override to release all locks
|
||||
*
|
||||
* @return boolean Was the unlock of all locks successful?
|
||||
*/
|
||||
public function releaseAll();
|
||||
public function releaseAll($override = false);
|
||||
|
||||
/**
|
||||
* Returns the name of the current lock
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
|
||||
/**
|
||||
* Lists all locks
|
||||
*
|
||||
* @param string prefix optional a prefix to search
|
||||
*
|
||||
* @return array Empty if it isn't supported by the cache driver
|
||||
*/
|
||||
public function getLocks(string $prefix = '');
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Friendica\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache\Cache;
|
||||
|
||||
/**
|
||||
* Class AbstractLock
|
||||
*
|
||||
|
@ -11,6 +13,9 @@ namespace Friendica\Core\Lock;
|
|||
*/
|
||||
abstract class Lock implements ILock
|
||||
{
|
||||
const TYPE_DATABASE = Cache::TYPE_DATABASE;
|
||||
const TYPE_SEMAPHORE = 'semaphore';
|
||||
|
||||
/**
|
||||
* @var array The local acquired locks
|
||||
*/
|
||||
|
@ -49,16 +54,14 @@ abstract class Lock implements ILock
|
|||
}
|
||||
|
||||
/**
|
||||
* Releases all lock that were set by us
|
||||
*
|
||||
* @return boolean Was the unlock of all locks successful?
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function releaseAll()
|
||||
public function releaseAll($override = false)
|
||||
{
|
||||
$return = true;
|
||||
|
||||
foreach ($this->acquiredLocks as $acquiredLock => $hasLock) {
|
||||
if (!$this->releaseLock($acquiredLock)) {
|
||||
if (!$this->releaseLock($acquiredLock, $override)) {
|
||||
$return = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,9 +20,7 @@ class SemaphoreLock extends Lock
|
|||
*/
|
||||
private static function semaphoreKey($key)
|
||||
{
|
||||
$temp = get_temppath();
|
||||
|
||||
$file = $temp . '/' . $key . '.sem';
|
||||
$file = self::keyToFile($key);
|
||||
|
||||
if (!file_exists($file)) {
|
||||
file_put_contents($file, $key);
|
||||
|
@ -31,10 +29,24 @@ class SemaphoreLock extends Lock
|
|||
return ftok($file, 'f');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the full path to the semaphore file
|
||||
*
|
||||
* @param string $key The key of the semaphore
|
||||
*
|
||||
* @return string The full path
|
||||
*/
|
||||
private static function keyToFile($key)
|
||||
{
|
||||
$temp = get_temppath();
|
||||
|
||||
return $temp . '/' . $key . '.sem';
|
||||
}
|
||||
|
||||
/**
|
||||
* (@inheritdoc)
|
||||
*/
|
||||
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
||||
public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
|
||||
{
|
||||
self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
|
||||
if (self::$semaphore[$key]) {
|
||||
|
@ -52,14 +64,24 @@ class SemaphoreLock extends Lock
|
|||
*/
|
||||
public function releaseLock($key, $override = false)
|
||||
{
|
||||
if (empty(self::$semaphore[$key])) {
|
||||
return false;
|
||||
} else {
|
||||
$success = @sem_release(self::$semaphore[$key]);
|
||||
unset(self::$semaphore[$key]);
|
||||
$this->markRelease($key);
|
||||
return $success;
|
||||
$success = false;
|
||||
|
||||
if (!empty(self::$semaphore[$key])) {
|
||||
try {
|
||||
$success = @sem_release(self::$semaphore[$key]) &&
|
||||
unlink(self::keyToFile($key));
|
||||
unset(self::$semaphore[$key]);
|
||||
$this->markRelease($key);
|
||||
} catch (\Exception $exception) {
|
||||
$success = false;
|
||||
}
|
||||
} else if ($override) {
|
||||
if ($this->acquireLock($key)) {
|
||||
$success = $this->releaseLock($key, true);
|
||||
}
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -69,4 +91,47 @@ class SemaphoreLock extends Lock
|
|||
{
|
||||
return isset(self::$semaphore[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::TYPE_SEMAPHORE;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getLocks(string $prefix = '')
|
||||
{
|
||||
$temp = get_temppath();
|
||||
$locks = [];
|
||||
foreach (glob(sprintf('%s/%s*.sem', $temp, $prefix)) as $lock) {
|
||||
$lock = pathinfo($lock, PATHINFO_FILENAME);
|
||||
if(sem_get(self::semaphoreKey($lock))) {
|
||||
$locks[] = $lock;
|
||||
}
|
||||
}
|
||||
|
||||
return $locks;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function releaseAll($override = false)
|
||||
{
|
||||
$success = parent::releaseAll($override);
|
||||
|
||||
$temp = get_temppath();
|
||||
foreach (glob(sprintf('%s/*.sem', $temp)) as $lock) {
|
||||
$lock = pathinfo($lock, PATHINFO_FILENAME);
|
||||
if (!$this->releaseLock($lock, true)) {
|
||||
$success = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue