Semaphore based locking and hopefully the fix for the workerqueue

This commit is contained in:
Michael 2017-06-28 04:53:11 +00:00
commit 4515c36f69
7 changed files with 102 additions and 18 deletions

View file

@ -17,6 +17,8 @@ use dbm;
* @brief This class contain Functions for preventing parallel execution of functions
*/
class Lock {
private static $semaphore = array();
/**
* @brief Check for memcache and open a connection if configured
*
@ -43,6 +45,25 @@ class Lock {
return $memcache;
}
/**
* @brief Creates a semaphore key
*
* @param string $fn_name Name of the lock
*
* @return ressource the semaphore key
*/
private static function semaphore_key($fn_name) {
$temp = get_temppath();
$file = $temp.'/'.$fn_name.'.sem';
if (!file_exists($file)) {
file_put_contents($file, $function);
}
return ftok($file, 'f');
}
/**
* @brief Sets a lock for a given name
*
@ -55,6 +76,13 @@ class Lock {
$got_lock = false;
$start = time();
if (function_exists('sem_get')) {
self::$semaphore[$fn_name] = sem_get(self::semaphore_key($fn_name));
if (self::$semaphore[$fn_name]) {
return sem_acquire(self::$semaphore[$fn_name], ($timeout == 0));
}
}
$memcache = self::connectMemcache();
if (is_object($memcache)) {
$cachekey = get_app()->get_hostname().";lock:".$fn_name;
@ -128,6 +156,11 @@ class Lock {
* @param string $fn_name Name of the lock
*/
public static function remove($fn_name) {
if (function_exists('sem_get') && self::$semaphore[$fn_name]) {
sem_release(self::$semaphore[$fn_name]);
return;
}
$memcache = self::connectMemcache();
if (is_object($memcache)) {
$cachekey = get_app()->get_hostname().";lock:".$fn_name;