}
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) {
*/
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
*
/**
* (@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();
/**
* (@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();
*
* @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
/**
* (@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])) {
use Friendica\App;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
+use Friendica\DI;
use Friendica\Util\Strings;
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);
// 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();
}
// 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;
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);
}
$stamp = (float)microtime(true);
- if (!Lock::acquire('worker_process')) {
+ if (!DI::lock()->acquire('worker_process')) {
return false;
}
self::$lock_duration += (microtime(true) - $stamp);
}
// 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;
}
}
// 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.");
}
}
// 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.");
}
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);
* @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)
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'));
}
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));
}
/**
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'));
*/
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'));
$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'));
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'));
*/
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'));
*/
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'));
$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'));
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'));
}