From: nupplaPhil Date: Mon, 6 Jan 2020 23:20:31 +0000 (+0100) Subject: Replace Lock::acquire() with DI::lock()->acquire() X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=634e657d23a276bc2bc241297eff468d59e3ffa3;p=friendica.git Replace Lock::acquire() with DI::lock()->acquire() --- diff --git a/src/Console/Lock.php b/src/Console/Lock.php index 46826cc8c9..71de727028 100644 --- a/src/Console/Lock.php +++ b/src/Console/Lock.php @@ -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) { diff --git a/src/Core/Lock.php b/src/Core/Lock.php index e25feac2ec..a01ad88821 100644 --- a/src/Core/Lock.php +++ b/src/Core/Lock.php @@ -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 * diff --git a/src/Core/Lock/CacheLock.php b/src/Core/Lock/CacheLock.php index 238beb705c..a4355cd085 100644 --- a/src/Core/Lock/CacheLock.php +++ b/src/Core/Lock/CacheLock.php @@ -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(); diff --git a/src/Core/Lock/DatabaseLock.php b/src/Core/Lock/DatabaseLock.php index cecdc3966e..4d1b3080bc 100644 --- a/src/Core/Lock/DatabaseLock.php +++ b/src/Core/Lock/DatabaseLock.php @@ -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(); diff --git a/src/Core/Lock/ILock.php b/src/Core/Lock/ILock.php index d103d99191..082abbd70d 100644 --- a/src/Core/Lock/ILock.php +++ b/src/Core/Lock/ILock.php @@ -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 diff --git a/src/Core/Lock/SemaphoreLock.php b/src/Core/Lock/SemaphoreLock.php index adfab5fc0e..64a105ec03 100644 --- a/src/Core/Lock/SemaphoreLock.php +++ b/src/Core/Lock/SemaphoreLock.php @@ -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])) { diff --git a/src/Core/Update.php b/src/Core/Update.php index 0ead704c10..5873f798e4 100644 --- a/src/Core/Update.php +++ b/src/Core/Update.php @@ -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(); diff --git a/src/Core/Worker.php b/src/Core/Worker.php index 1eea922396..ed0f3a2e78 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -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; } diff --git a/src/Model/Item.php b/src/Model/Item.php index 4363a9a789..411608d677 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -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."); } diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index 21659a6036..99702f9517 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -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); diff --git a/tests/Util/DbaLockMockTrait.php b/tests/Util/DbaLockMockTrait.php index 5b405dcfc2..035a859365 100644 --- a/tests/Util/DbaLockMockTrait.php +++ b/tests/Util/DbaLockMockTrait.php @@ -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) diff --git a/tests/src/Core/Lock/LockTest.php b/tests/src/Core/Lock/LockTest.php index 24efeaab86..269d48abd9 100644 --- a/tests/src/Core/Lock/LockTest.php +++ b/tests/src/Core/Lock/LockTest.php @@ -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')); diff --git a/tests/src/Core/Lock/SemaphoreLockTest.php b/tests/src/Core/Lock/SemaphoreLockTest.php index 51d3866fe7..48c3b9f436 100644 --- a/tests/src/Core/Lock/SemaphoreLockTest.php +++ b/tests/src/Core/Lock/SemaphoreLockTest.php @@ -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')); }