]> git.mxchange.org Git - friendica.git/commitdiff
Replace Lock::acquire() with DI::lock()->acquire()
authornupplaPhil <admin@philipp.info>
Mon, 6 Jan 2020 23:20:31 +0000 (00:20 +0100)
committernupplaPhil <admin@philipp.info>
Mon, 6 Jan 2020 23:20:31 +0000 (00:20 +0100)
13 files changed:
src/Console/Lock.php
src/Core/Lock.php
src/Core/Lock/CacheLock.php
src/Core/Lock/DatabaseLock.php
src/Core/Lock/ILock.php
src/Core/Lock/SemaphoreLock.php
src/Core/Update.php
src/Core/Worker.php
src/Model/Item.php
src/Protocol/OStatus.php
tests/Util/DbaLockMockTrait.php
tests/src/Core/Lock/LockTest.php
tests/src/Core/Lock/SemaphoreLockTest.php

index 46826cc8c92a384981daef2c055b5e11ccc0af0a..71de727028f87a1e72248f58eae71b8affdeb26b 100644 (file)
@@ -156,11 +156,11 @@ HELP;
                        }
 
                        if (!empty($ttl) && !empty($timeout)) {
-                               $result = $this->lock->acquireLock($lock, $timeout, $ttl);
+                               $result = $this->lock->acquire($lock, $timeout, $ttl);
                        } elseif (!empty($timeout)) {
-                               $result = $this->lock->acquireLock($lock, $timeout);
+                               $result = $this->lock->acquire($lock, $timeout);
                        } else {
-                               $result = $this->lock->acquireLock($lock);
+                               $result = $this->lock->acquire($lock);
                        }
 
                        if ($result) {
index e25feac2ecda5e22890d6ea27f81d13de1877d09..a01ad888219cf123f4190890ecdd7bd4b0b6acb6 100644 (file)
@@ -15,21 +15,6 @@ use Friendica\DI;
  */
 class Lock
 {
-       /**
-        * @brief Acquires a lock for a given name
-        *
-        * @param string  $key     Name of the lock
-        * @param integer $timeout Seconds until we give up
-        * @param integer $ttl     The Lock lifespan, must be one of the Cache constants
-        *
-        * @return boolean Was the lock successful?
-        * @throws \Exception
-        */
-       public static function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
-       {
-               return DI::lock()->acquireLock($key, $timeout, $ttl);
-       }
-
        /**
         * @brief Releases a lock if it was set by us
         *
index 238beb705ce93e986773adef7ec898e41a3ab9b6..a4355cd085790d3ec27dbcd94cb3c123a507e2ba 100644 (file)
@@ -30,7 +30,7 @@ class CacheLock extends Lock
        /**
         * (@inheritdoc)
         */
-       public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
+       public function acquire($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
        {
                $got_lock = false;
                $start    = time();
index cecdc3966e41d9a6a1a0abfe889d6acdc8c439ee..4d1b3080bcd0f6ddce8ff73623f4618657d26de3 100644 (file)
@@ -35,7 +35,7 @@ class DatabaseLock extends Lock
        /**
         * (@inheritdoc)
         */
-       public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
+       public function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
        {
                $got_lock = false;
                $start    = time();
index d103d991912b42210c0ae4cd3a9766d788eae222..082abbd70db3002e1e492ddf5b037ebad18e4824 100644 (file)
@@ -30,7 +30,7 @@ interface ILock
         *
         * @return boolean Was the lock successful?
         */
-       public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES);
+       public function acquire($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES);
 
        /**
         * Releases a lock if it was set by us
index adfab5fc0e7d4a85eebe565f663567bae2093f8b..64a105ec03fa0d5efcbfed4c547da85c44e1fafc 100644 (file)
@@ -36,7 +36,7 @@ class SemaphoreLock extends Lock
        /**
         * (@inheritdoc)
         */
-       public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
+       public function acquire($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
        {
                self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
                if (!empty(self::$semaphore[$key])) {
index 0ead704c101671faa77b9c17fd95122e0df88d2e..5873f798e44f73c7172d1447db649445ca554ec6 100644 (file)
@@ -5,6 +5,7 @@ namespace Friendica\Core;
 use Friendica\App;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
+use Friendica\DI;
 use Friendica\Util\Strings;
 
 class Update
@@ -95,7 +96,7 @@ class Update
 
                                // Compare the current structure with the defined structure
                                // If the Lock is acquired, never release it automatically to avoid double updates
-                               if (Lock::acquire('dbupdate', 120, Cache::INFINITE)) {
+                               if (DI::lock()->acquire('dbupdate', 120, Cache::INFINITE)) {
 
                                        // Checks if the build changed during Lock acquiring (so no double update occurs)
                                        $retryBuild = Config::get('system', 'build', null, true);
@@ -181,7 +182,7 @@ class Update
                        // If the update fails or times-out completely you may need to
                        // delete the config entry to try again.
 
-                       if (Lock::acquire('dbupdate_function', 120,Cache::INFINITE)) {
+                       if (DI::lock()->acquire('dbupdate_function', 120,Cache::INFINITE)) {
 
                                // call the specific update
                                $retval = $funcname();
index 1eea922396a717bd8938bef911e474e1ef9f7711..ed0f3a2e782c84e6fb5c8bfece784087807db087 100644 (file)
@@ -115,7 +115,7 @@ class Worker
                                }
 
                                // Trying to fetch new processes - but only once when successful
-                               if (!$refetched && Lock::acquire('worker_process', 0)) {
+                               if (!$refetched && DI::lock()->acquire('worker_process', 0)) {
                                        self::findWorkerProcesses();
                                        Lock::release('worker_process');
                                        self::$state = self::STATE_REFETCH;
@@ -129,7 +129,7 @@ class Worker
                        if (!self::getWaitingJobForPID()) {
                                self::$state = self::STATE_LONG_LOOP;
 
-                               if (Lock::acquire('worker', 0)) {
+                               if (DI::lock()->acquire('worker', 0)) {
                                // Count active workers and compare them with a maximum value that depends on the load
                                        if (self::tooMuchWorkers()) {
                                                Logger::log('Active worker limit reached, quitting.', Logger::DEBUG);
@@ -933,7 +933,7 @@ class Worker
                }
 
                $stamp = (float)microtime(true);
-               if (!Lock::acquire('worker_process')) {
+               if (!DI::lock()->acquire('worker_process')) {
                        return false;
                }
                self::$lock_duration += (microtime(true) - $stamp);
@@ -1172,7 +1172,7 @@ class Worker
                }
 
                // If there is a lock then we don't have to check for too much worker
-               if (!Lock::acquire('worker', 0)) {
+               if (!DI::lock()->acquire('worker', 0)) {
                        return $added;
                }
 
index 4363a9a789ffd63893c7b2989c122cd641b978c3..411608d677fdacfa5ec4a267223eaa0c702172dc 100644 (file)
@@ -2060,7 +2060,7 @@ class Item
                }
 
                // To avoid timing problems, we are using locks.
-               $locked = Lock::acquire('item_insert_activity');
+               $locked = DI::lock()->acquire('item_insert_activity');
                if (!$locked) {
                        Logger::log("Couldn't acquire lock for URI " . $item['uri'] . " - proceeding anyway.");
                }
@@ -2103,7 +2103,7 @@ class Item
                }
 
                // To avoid timing problems, we are using locks.
-               $locked = Lock::acquire('item_insert_content');
+               $locked = DI::lock()->acquire('item_insert_content');
                if (!$locked) {
                        Logger::log("Couldn't acquire lock for URI " . $item['uri'] . " - proceeding anyway.");
                }
index 21659a6036ae79f696a1b6dbe13358ecaa438895..99702f9517c74bd906dcdfbea01efcf20e0f9888 100644 (file)
@@ -539,7 +539,7 @@ class OStatus
                                                        Logger::log("Item with uri ".$item["uri"]." is from a blocked contact.", Logger::DEBUG);
                                                } else {
                                                        // We are having duplicated entries. Hopefully this solves it.
-                                                       if (Lock::acquire('ostatus_process_item_insert')) {
+                                                       if (DI::lock()->acquire('ostatus_process_item_insert')) {
                                                                $ret = Item::insert($item);
                                                                Lock::release('ostatus_process_item_insert');
                                                                Logger::log("Item with uri ".$item["uri"]." for user ".$importer["uid"].' stored. Return value: '.$ret);
index 5b405dcfc275b9175267f79acb0f24ff576c8970..035a859365982579c1077a09e3d706f3a6e32bec 100644 (file)
@@ -22,7 +22,7 @@ trait DbaLockMockTrait
         * @param null     $time      The current timestamp
         * @param null|int $times     How often the method will get used
         *
-        *@see DatabaseLock::acquireLock()
+        *@see DatabaseLock::acquire()
         *
         */
        public function mockAcquireLock($key, $ttl = Cache::FIVE_MINUTES, $locked = false, $pid = null, $rowExists = true, $time = null, $times = null)
index 24efeaab869823d7f6fa2c5b7cf179798d936502..269d48abd9c99dea437285f18ad79a28d7a162f4 100644 (file)
@@ -38,7 +38,7 @@ abstract class LockTest extends MockedTest
        public function testLock()
        {
                $this->assertFalse($this->instance->isLocked('foo'));
-               $this->assertTrue($this->instance->acquireLock('foo', 1));
+               $this->assertTrue($this->instance->acquire('foo', 1));
                $this->assertTrue($this->instance->isLocked('foo'));
                $this->assertFalse($this->instance->isLocked('bar'));
        }
@@ -49,10 +49,10 @@ abstract class LockTest extends MockedTest
        public function testDoubleLock()
        {
                $this->assertFalse($this->instance->isLocked('foo'));
-               $this->assertTrue($this->instance->acquireLock('foo', 1));
+               $this->assertTrue($this->instance->acquire('foo', 1));
                $this->assertTrue($this->instance->isLocked('foo'));
                // We already locked it
-               $this->assertTrue($this->instance->acquireLock('foo', 1));
+               $this->assertTrue($this->instance->acquire('foo', 1));
        }
 
        /**
@@ -61,7 +61,7 @@ abstract class LockTest extends MockedTest
        public function testReleaseLock()
        {
                $this->assertFalse($this->instance->isLocked('foo'));
-               $this->assertTrue($this->instance->acquireLock('foo', 1));
+               $this->assertTrue($this->instance->acquire('foo', 1));
                $this->assertTrue($this->instance->isLocked('foo'));
                $this->instance->releaseLock('foo');
                $this->assertFalse($this->instance->isLocked('foo'));
@@ -72,9 +72,9 @@ abstract class LockTest extends MockedTest
         */
        public function testReleaseAll()
        {
-               $this->assertTrue($this->instance->acquireLock('foo', 1));
-               $this->assertTrue($this->instance->acquireLock('bar', 1));
-               $this->assertTrue($this->instance->acquireLock('nice', 1));
+               $this->assertTrue($this->instance->acquire('foo', 1));
+               $this->assertTrue($this->instance->acquire('bar', 1));
+               $this->assertTrue($this->instance->acquire('nice', 1));
 
                $this->assertTrue($this->instance->isLocked('foo'));
                $this->assertTrue($this->instance->isLocked('bar'));
@@ -95,9 +95,9 @@ abstract class LockTest extends MockedTest
                $this->assertFalse($this->instance->isLocked('foo'));
                $this->assertFalse($this->instance->isLocked('bar'));
                $this->assertFalse($this->instance->isLocked('nice'));
-               $this->assertTrue($this->instance->acquireLock('foo', 1));
-               $this->assertTrue($this->instance->acquireLock('bar', 1));
-               $this->assertTrue($this->instance->acquireLock('nice', 1));
+               $this->assertTrue($this->instance->acquire('foo', 1));
+               $this->assertTrue($this->instance->acquire('bar', 1));
+               $this->assertTrue($this->instance->acquire('nice', 1));
 
                $this->assertTrue($this->instance->releaseLock('foo'));
 
@@ -117,7 +117,7 @@ abstract class LockTest extends MockedTest
        public function testReleaseWitTTL()
        {
                $this->assertFalse($this->instance->isLocked('test'));
-               $this->assertTrue($this->instance->acquireLock('test', 1, 10));
+               $this->assertTrue($this->instance->acquire('test', 1, 10));
                $this->assertTrue($this->instance->isLocked('test'));
                $this->assertTrue($this->instance->releaseLock('test'));
                $this->assertFalse($this->instance->isLocked('test'));
@@ -128,9 +128,9 @@ abstract class LockTest extends MockedTest
         */
        public function testGetLocks()
        {
-               $this->assertTrue($this->instance->acquireLock('foo', 1));
-               $this->assertTrue($this->instance->acquireLock('bar', 1));
-               $this->assertTrue($this->instance->acquireLock('nice', 1));
+               $this->assertTrue($this->instance->acquire('foo', 1));
+               $this->assertTrue($this->instance->acquire('bar', 1));
+               $this->assertTrue($this->instance->acquire('nice', 1));
 
                $this->assertTrue($this->instance->isLocked('foo'));
                $this->assertTrue($this->instance->isLocked('bar'));
@@ -148,9 +148,9 @@ abstract class LockTest extends MockedTest
         */
        public function testGetLocksWithPrefix()
        {
-               $this->assertTrue($this->instance->acquireLock('foo', 1));
-               $this->assertTrue($this->instance->acquireLock('test1', 1));
-               $this->assertTrue($this->instance->acquireLock('test2', 1));
+               $this->assertTrue($this->instance->acquire('foo', 1));
+               $this->assertTrue($this->instance->acquire('test1', 1));
+               $this->assertTrue($this->instance->acquire('test2', 1));
 
                $this->assertTrue($this->instance->isLocked('foo'));
                $this->assertTrue($this->instance->isLocked('test1'));
@@ -174,8 +174,8 @@ abstract class LockTest extends MockedTest
                $this->assertFalse($this->instance->isLocked('bar'));
 
                // TODO [nupplaphil] - Because of the Datetime-Utils for the database, we have to wait a FULL second between the checks to invalidate the db-locks/cache
-               $this->assertTrue($this->instance->acquireLock('foo', 2, 1));
-               $this->assertTrue($this->instance->acquireLock('bar', 2, 3));
+               $this->assertTrue($this->instance->acquire('foo', 2, 1));
+               $this->assertTrue($this->instance->acquire('bar', 2, 3));
 
                $this->assertTrue($this->instance->isLocked('foo'));
                $this->assertTrue($this->instance->isLocked('bar'));
index 51d3866fe719d0b2e979f6ec7556709b7d9326d7..48c3b9f4360c53515f91c5eb725e9fc44106adee 100644 (file)
@@ -85,7 +85,7 @@ class SemaphoreLockTest extends LockTest
                touch($file);
 
                $this->assertTrue(file_exists($file));
-               $this->assertTrue($this->instance->acquireLock('test'));
+               $this->assertTrue($this->instance->acquire('test'));
                $this->assertTrue($this->instance->isLocked('test'));
                $this->assertTrue($this->instance->releaseLock('test'));
        }