]> git.mxchange.org Git - friendica.git/commitdiff
Moved Lock.php back to Utils
authorPhilipp Holzer <admin@philipp.info>
Tue, 26 Jun 2018 21:28:07 +0000 (23:28 +0200)
committerPhilipp Holzer <admin@philipp.info>
Tue, 26 Jun 2018 21:28:07 +0000 (23:28 +0200)
12 files changed:
src/Core/Lock.php [deleted file]
src/Core/Lock/DatabaseLockDriver.php [deleted file]
src/Core/Lock/ILockDriver.php [deleted file]
src/Core/Lock/MemcacheLockDriver.php [deleted file]
src/Core/Lock/SemaphoreLockDriver.php [deleted file]
src/Core/Worker.php
src/Protocol/OStatus.php
src/Util/Lock.php [new file with mode: 0644]
src/Util/Lock/DatabaseLockDriver.php [new file with mode: 0644]
src/Util/Lock/ILockDriver.php [new file with mode: 0644]
src/Util/Lock/MemcacheLockDriver.php [new file with mode: 0644]
src/Util/Lock/SemaphoreLockDriver.php [new file with mode: 0644]

diff --git a/src/Core/Lock.php b/src/Core/Lock.php
deleted file mode 100644 (file)
index 9e02d14..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-<?php
-/**
- * @file src/Core/Lock.php
- */
-namespace Friendica\Util;
-
-/**
- * @file src/Core/Lock.php
- * @brief Functions for preventing parallel execution of functions
- */
-
-use Friendica\Core\Config;
-use Friendica\Core\Lock;
-
-require_once 'include/dba.php';
-
-/**
- * @brief This class contain Functions for preventing parallel execution of functions
- */
-class Lock
-{
-       /**
-        * @var Lock\ILockDriver;
-        */
-       static $driver = null;
-
-       public static function init()
-       {
-               switch(Config::get('system', 'lock_driver', 'default')) {
-                       case 'memcache':
-                               self::$driver = new Lock\MemcacheLockDriver();
-                               break;
-                       case 'database':
-                               self::$driver = new Lock\DatabaseLockDriver();
-                               break;
-                       case 'semaphore':
-                               self::$driver = new Lock\SemaphoreLockDriver();
-                               break;
-                       default:
-                               // Determine what's the best/fastest locking driver (default behavior in Friendica)
-                               if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
-                                       self::$driver = new Lock\SemaphoreLockDriver();
-                               } elseif (Config::get('system', 'cache_driver', 'database') == 'memcache') {
-                                       self::$driver = new Lock\MemcacheLockDriver();
-                               } else {
-                                       self::$driver = new Lock\DatabaseLockDriver();
-                               }
-               }
-       }
-
-       /**
-        * Returns the current cache driver
-        *
-        * @return Lock\ILockDriver;
-        */
-       private static function getDriver()
-       {
-               if (self::$driver === null) {
-                       self::init();
-               }
-
-               return self::$driver;
-       }
-
-       /**
-        * @brief Acquires a lock for a given name
-        *
-        * @param string  $key Name of the lock
-        * @param integer $timeout Seconds until we give up
-        *
-        * @return boolean Was the lock successful?
-        */
-       public static function acquireLock($key, $timeout = 120)
-       {
-               return self::getDriver()->acquireLock($key, $timeout);
-       }
-
-       /**
-        * @brief Releases a lock if it was set by us
-        *
-        * @param string $key Name of the lock
-        * @return mixed
-        */
-       public static function releaseLock($key)
-       {
-               return self::getDriver()->releaseLock($key);
-       }
-
-       /**
-        * @brief Releases all lock that were set by us
-        * @return void
-        */
-       public static function releaseAll()
-       {
-               self::getDriver()->releaseAll();
-       }
-}
diff --git a/src/Core/Lock/DatabaseLockDriver.php b/src/Core/Lock/DatabaseLockDriver.php
deleted file mode 100644 (file)
index 8761a14..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-<?php
-
-namespace Friendica\Core\Lock;
-
-use dba;
-use Friendica\Database\DBM;
-
-/**
- * Locking driver that stores the locks in the database
- */
-class DatabaseLockDriver implements ILockDriver
-{
-       /**
-        * @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?
-        */
-       public function acquireLock($key, $timeout = 120)
-       {
-               $got_lock = false;
-               $start = time();
-
-               do {
-                       dba::lock('locks');
-                       $lock = dba::selectFirst('locks', ['locked', 'pid'], ['name' => $key]);
-
-                       if (DBM::is_result($lock)) {
-                               if ($lock['locked']) {
-                                       // When the process id isn't used anymore, we can safely claim the lock for us.
-                                       if (!posix_kill($lock['pid'], 0)) {
-                                               $lock['locked'] = false;
-                                       }
-                                       // We want to lock something that was already locked by us? So we got the lock.
-                                       if ($lock['pid'] == getmypid()) {
-                                               $got_lock = true;
-                                       }
-                               }
-                               if (!$lock['locked']) {
-                                       dba::update('locks', ['locked' => true, 'pid' => getmypid()], ['name' => $key]);
-                                       $got_lock = true;
-                               }
-                       } elseif (!DBM::is_result($lock)) {
-                               dba::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid()]);
-                               $got_lock = true;
-                       }
-
-                       dba::unlock();
-
-                       if (!$got_lock && ($timeout > 0)) {
-                               usleep(rand(100000, 2000000));
-                       }
-               } while (!$got_lock && ((time() - $start) < $timeout));
-
-               return $got_lock;
-       }
-
-       /**
-        * @brief Removes a lock if it was set by us
-        *
-        * @param string $key Name of the lock
-        *
-        * @return mixed
-        */
-       public function releaseLock($key)
-       {
-               dba::update('locks', ['locked' => false, 'pid' => 0], ['name' => $key, 'pid' => getmypid()]);
-
-               return;
-       }
-
-       /**
-        * @brief Removes all lock that were set by us
-        *
-        * @return void
-        */
-       public function releaseAll()
-       {
-               dba::update('locks', ['locked' => false, 'pid' => 0], ['pid' => getmypid()]);
-       }
-}
diff --git a/src/Core/Lock/ILockDriver.php b/src/Core/Lock/ILockDriver.php
deleted file mode 100644 (file)
index b066361..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-<?php
-
-namespace Friendica\Core\Lock;
-
-/**
- * Lock Driver Interface
- *
- * @author Philipp Holzer <admin@philipp.info>
- */
-interface ILockDriver
-{
-       /**
-        *
-        * @brief Acquires 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?
-        */
-       public function acquireLock($key, $timeout = 120);
-
-       /**
-        * @brief Releases a lock if it was set by us
-        *
-        * @param string $key Name of the lock
-        *
-        * @return mixed
-        */
-       public function releaseLock($key);
-
-       /**
-        * @brief Releases all lock that were set by us
-        *
-        * @return void
-        */
-       public function releaseAll();
-}
\ No newline at end of file
diff --git a/src/Core/Lock/MemcacheLockDriver.php b/src/Core/Lock/MemcacheLockDriver.php
deleted file mode 100644 (file)
index 1796d81..0000000
+++ /dev/null
@@ -1,86 +0,0 @@
-<?php
-
-namespace Friendica\Core\Lock;
-
-use Friendica\Core\Cache;
-use dba;
-
-class MemcacheLockDriver implements ILockDriver
-{
-       /**
-        *
-        * @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?
-        */
-       public function acquireLock($key, $timeout = 120)
-       {
-               $got_lock = false;
-               $start = time();
-
-               $cachekey = get_app()->get_hostname() . ";lock:" . $key;
-
-               do {
-                       // We only lock to be sure that nothing happens at exactly the same time
-                       dba::lock('locks');
-                       $lock = Cache::get($cachekey);
-
-                       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)) {
-                               Cache::set($cachekey, getmypid(), 300);
-                               $got_lock = true;
-                       }
-
-                       dba::unlock();
-
-                       if (!$got_lock && ($timeout > 0)) {
-                               usleep(rand(10000, 200000));
-                       }
-               } while (!$got_lock && ((time() - $start) < $timeout));
-
-               return $got_lock;
-       }
-
-       /**
-        * @brief Removes a lock if it was set by us
-        *
-        * @param string $key Name of the lock
-        *
-        * @return mixed
-        */
-       public function releaseLock($key)
-       {
-               $cachekey = get_app()->get_hostname() . ";lock:" . $key;
-               $lock = Cache::get($cachekey);
-
-               if (!is_bool($lock)) {
-                       if ((int)$lock == getmypid()) {
-                               Cache::delete($cachekey);
-                       }
-               }
-
-               return;
-       }
-
-       /**
-        * @brief Removes all lock that were set by us
-        *
-        * @return void
-        */
-       public function releaseAll()
-       {
-               // We cannot delete all cache entries, but this doesn't matter with memcache
-               return;
-       }
-}
\ No newline at end of file
diff --git a/src/Core/Lock/SemaphoreLockDriver.php b/src/Core/Lock/SemaphoreLockDriver.php
deleted file mode 100644 (file)
index 93911a3..0000000
+++ /dev/null
@@ -1,82 +0,0 @@
-<?php
-
-namespace Friendica\Core\Lock;
-
-class SemaphoreLockDriver implements ILockDriver
-{
-       /**
-        * @var array stored semaphores
-        */
-       private static $semaphore = [];
-
-       /**
-        * @brief Creates a semaphore key
-        *
-        * @param string $key Name of the lock
-        *
-        * @return integer the semaphore key
-        */
-       private static function semaphoreKey($key)
-       {
-               $temp = get_temppath();
-
-               $file = $temp.'/'.$key.'.sem';
-
-               if (!file_exists($file)) {
-                       file_put_contents($file, $key);
-               }
-
-               return ftok($file, 'f');
-       }
-
-       /**
-        *
-        * @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?
-        */
-       public function acquireLock($key, $timeout = 120)
-       {
-               // The second parameter for "sem_acquire" doesn't exist before 5.6.1
-               if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
-                       self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
-                       if (self::$semaphore[$key]) {
-                               return sem_acquire(self::$semaphore[$key], ($timeout == 0));
-                       }
-               }
-       }
-
-       /**
-        * @brief Removes a lock if it was set by us
-        *
-        * @param string $key Name of the lock
-        *
-        * @return mixed
-        */
-       public function releaseLock($key)
-       {
-               if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
-                       if (empty(self::$semaphore[$key])) {
-                               return false;
-                       } else {
-                               $success = @sem_release(self::$semaphore[$key]);
-                               unset(self::$semaphore[$key]);
-                               return $success;
-                       }
-               }
-       }
-
-       /**
-        * @brief Removes all lock that were set by us
-        *
-        * @return void
-        */
-       public function releaseAll()
-       {
-               // not needed/supported
-               return;
-       }
-}
\ No newline at end of file
index b8a9021d0fa1825f9be1bdc0393529422b2fd633..0e8835a1564972a62ed40968789a498a89f418ab 100644 (file)
@@ -11,6 +11,7 @@ use Friendica\Database\DBM;
 use Friendica\Model\Process;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
+use Friendica\Util\Lock;
 use dba;
 
 require_once 'include/dba.php';
index 69975c575377a83f1267dc7a938dd2c54154dfcd..a1223b7b681f3b5162503893a7fdab259b7c68a3 100644 (file)
@@ -19,6 +19,7 @@ use Friendica\Model\User;
 use Friendica\Network\Probe;
 use Friendica\Object\Image;
 use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Lock;
 use Friendica\Util\Network;
 use Friendica\Util\XML;
 use dba;
diff --git a/src/Util/Lock.php b/src/Util/Lock.php
new file mode 100644 (file)
index 0000000..95341b4
--- /dev/null
@@ -0,0 +1,102 @@
+<?php
+/**
+ * @file src/Core/Lock.php
+ */
+namespace Friendica\Util;
+
+/**
+ * @file src/Core/Lock.php
+ * @brief Functions for preventing parallel execution of functions
+ */
+
+use Friendica\Core\Config;
+use Friendica\Util\Lock;
+
+require_once 'include/dba.php';
+
+/**
+ * @brief This class contain Functions for preventing parallel execution of functions
+ */
+class Lock
+{
+       /**
+        * @var Lock\ILockDriver;
+        */
+       static $driver = null;
+
+       public static function init()
+       {
+               switch(Config::get('system', 'lock_driver', 'default')) {
+                       case 'memcache':
+                               self::$driver = new Lock\MemcacheLockDriver();
+                               break;
+                       case 'database':
+                               self::$driver = new Lock\DatabaseLockDriver();
+                               break;
+                       case 'semaphore':
+                               self::$driver = new Lock\SemaphoreLockDriver();
+                               break;
+                       default:
+                               // Determine what's the best/fastest locking driver (default behavior in Friendica)
+                               if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
+                                       self::$driver = new Lock\SemaphoreLockDriver();
+                               } elseif (Config::get('system', 'cache_driver', 'database') == 'memcache') {
+                                       self::$driver = new Lock\MemcacheLockDriver();
+                               } else {
+                                       self::$driver = new Lock\DatabaseLockDriver();
+                               }
+               }
+       }
+
+       /**
+        * Returns the current cache driver
+        *
+        * @return Lock\ILockDriver;
+        */
+       private static function getDriver()
+       {
+               if (self::$driver === null) {
+                       self::init();
+               }
+
+               return self::$driver;
+       }
+
+       /**
+        * @brief Acquires a lock for a given name
+        *
+        * @param string  $key Name of the lock
+        * @param integer $timeout Seconds until we give up
+        *
+        * @return boolean Was the lock successful?
+        */
+       public static function acquireLock($key, $timeout = 120)
+       {
+               return self::getDriver()->acquireLock($key, $timeout);
+       }
+
+       /**
+        * @brief Releases a lock if it was set by us
+        *
+        * @param string $key Name of the lock
+        * @return mixed
+        */
+       public static function releaseLock($key)
+       {
+               return self::getDriver()->releaseLock($key);
+       }
+
+       /**
+        * @brief Releases all lock that were set by us
+        * @return void
+        */
+       public static function releaseAll()
+       {
+               self::getDriver()->releaseAll();
+       }
+
+       public static function isLocked($key)
+       {
+
+       }
+}
diff --git a/src/Util/Lock/DatabaseLockDriver.php b/src/Util/Lock/DatabaseLockDriver.php
new file mode 100644 (file)
index 0000000..b2e8f50
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+
+namespace Friendica\Util\Lock;
+
+use dba;
+use Friendica\Database\DBM;
+
+/**
+ * Locking driver that stores the locks in the database
+ */
+class DatabaseLockDriver implements ILockDriver
+{
+       /**
+        * @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?
+        */
+       public function acquireLock($key, $timeout = 120)
+       {
+               $got_lock = false;
+               $start = time();
+
+               do {
+                       dba::lock('locks');
+                       $lock = dba::selectFirst('locks', ['locked', 'pid'], ['name' => $key]);
+
+                       if (DBM::is_result($lock)) {
+                               if ($lock['locked']) {
+                                       // When the process id isn't used anymore, we can safely claim the lock for us.
+                                       if (!posix_kill($lock['pid'], 0)) {
+                                               $lock['locked'] = false;
+                                       }
+                                       // We want to lock something that was already locked by us? So we got the lock.
+                                       if ($lock['pid'] == getmypid()) {
+                                               $got_lock = true;
+                                       }
+                               }
+                               if (!$lock['locked']) {
+                                       dba::update('locks', ['locked' => true, 'pid' => getmypid()], ['name' => $key]);
+                                       $got_lock = true;
+                               }
+                       } elseif (!DBM::is_result($lock)) {
+                               dba::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid()]);
+                               $got_lock = true;
+                       }
+
+                       dba::unlock();
+
+                       if (!$got_lock && ($timeout > 0)) {
+                               usleep(rand(100000, 2000000));
+                       }
+               } while (!$got_lock && ((time() - $start) < $timeout));
+
+               return $got_lock;
+       }
+
+       /**
+        * @brief Removes a lock if it was set by us
+        *
+        * @param string $key Name of the lock
+        *
+        * @return mixed
+        */
+       public function releaseLock($key)
+       {
+               dba::update('locks', ['locked' => false, 'pid' => 0], ['name' => $key, 'pid' => getmypid()]);
+
+               return;
+       }
+
+       /**
+        * @brief Removes all lock that were set by us
+        *
+        * @return void
+        */
+       public function releaseAll()
+       {
+               dba::update('locks', ['locked' => false, 'pid' => 0], ['pid' => getmypid()]);
+       }
+}
diff --git a/src/Util/Lock/ILockDriver.php b/src/Util/Lock/ILockDriver.php
new file mode 100644 (file)
index 0000000..6b8a08c
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+namespace Friendica\Util\Lock;
+
+/**
+ * Lock Driver Interface
+ *
+ * @author Philipp Holzer <admin@philipp.info>
+ */
+interface ILockDriver
+{
+       /**
+        *
+        * @brief Acquires 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?
+        */
+       public function acquireLock($key, $timeout = 120);
+
+       /**
+        * @brief Releases a lock if it was set by us
+        *
+        * @param string $key Name of the lock
+        *
+        * @return mixed
+        */
+       public function releaseLock($key);
+
+       /**
+        * @brief Releases all lock that were set by us
+        *
+        * @return void
+        */
+       public function releaseAll();
+
+
+       public function isLocked();
+}
\ No newline at end of file
diff --git a/src/Util/Lock/MemcacheLockDriver.php b/src/Util/Lock/MemcacheLockDriver.php
new file mode 100644 (file)
index 0000000..9a3ceb4
--- /dev/null
@@ -0,0 +1,86 @@
+<?php
+
+namespace Friendica\Util\Lock;
+
+use Friendica\Core\Cache;
+use dba;
+
+class MemcacheLockDriver implements ILockDriver
+{
+       /**
+        *
+        * @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?
+        */
+       public function acquireLock($key, $timeout = 120)
+       {
+               $got_lock = false;
+               $start = time();
+
+               $cachekey = get_app()->get_hostname() . ";lock:" . $key;
+
+               do {
+                       // We only lock to be sure that nothing happens at exactly the same time
+                       dba::lock('locks');
+                       $lock = Cache::get($cachekey);
+
+                       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)) {
+                               Cache::set($cachekey, getmypid(), 300);
+                               $got_lock = true;
+                       }
+
+                       dba::unlock();
+
+                       if (!$got_lock && ($timeout > 0)) {
+                               usleep(rand(10000, 200000));
+                       }
+               } while (!$got_lock && ((time() - $start) < $timeout));
+
+               return $got_lock;
+       }
+
+       /**
+        * @brief Removes a lock if it was set by us
+        *
+        * @param string $key Name of the lock
+        *
+        * @return mixed
+        */
+       public function releaseLock($key)
+       {
+               $cachekey = get_app()->get_hostname() . ";lock:" . $key;
+               $lock = Cache::get($cachekey);
+
+               if (!is_bool($lock)) {
+                       if ((int)$lock == getmypid()) {
+                               Cache::delete($cachekey);
+                       }
+               }
+
+               return;
+       }
+
+       /**
+        * @brief Removes all lock that were set by us
+        *
+        * @return void
+        */
+       public function releaseAll()
+       {
+               // We cannot delete all cache entries, but this doesn't matter with memcache
+               return;
+       }
+}
\ No newline at end of file
diff --git a/src/Util/Lock/SemaphoreLockDriver.php b/src/Util/Lock/SemaphoreLockDriver.php
new file mode 100644 (file)
index 0000000..3adf1ff
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+
+namespace Friendica\Util\Lock;
+
+class SemaphoreLockDriver implements ILockDriver
+{
+       /**
+        * @var array stored semaphores
+        */
+       private static $semaphore = [];
+
+       /**
+        * @brief Creates a semaphore key
+        *
+        * @param string $key Name of the lock
+        *
+        * @return integer the semaphore key
+        */
+       private static function semaphoreKey($key)
+       {
+               $temp = get_temppath();
+
+               $file = $temp.'/'.$key.'.sem';
+
+               if (!file_exists($file)) {
+                       file_put_contents($file, $key);
+               }
+
+               return ftok($file, 'f');
+       }
+
+       /**
+        *
+        * @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?
+        */
+       public function acquireLock($key, $timeout = 120)
+       {
+               // The second parameter for "sem_acquire" doesn't exist before 5.6.1
+               if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
+                       self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
+                       if (self::$semaphore[$key]) {
+                               return sem_acquire(self::$semaphore[$key], ($timeout == 0));
+                       }
+               }
+       }
+
+       /**
+        * @brief Removes a lock if it was set by us
+        *
+        * @param string $key Name of the lock
+        *
+        * @return mixed
+        */
+       public function releaseLock($key)
+       {
+               if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
+                       if (empty(self::$semaphore[$key])) {
+                               return false;
+                       } else {
+                               $success = @sem_release(self::$semaphore[$key]);
+                               unset(self::$semaphore[$key]);
+                               return $success;
+                       }
+               }
+       }
+
+       /**
+        * @brief Removes all lock that were set by us
+        *
+        * @return void
+        */
+       public function releaseAll()
+       {
+               // not needed/supported
+               return;
+       }
+}
\ No newline at end of file