2018-06-26 22:31:04 +02:00
|
|
|
<?php
|
|
|
|
|
2018-06-28 22:57:17 +02:00
|
|
|
namespace Friendica\Core\Lock;
|
2018-06-26 22:31:04 +02:00
|
|
|
|
|
|
|
use dba;
|
|
|
|
use Friendica\Database\DBM;
|
2018-07-04 23:37:22 +02:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2018-06-26 22:31:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Locking driver that stores the locks in the database
|
|
|
|
*/
|
2018-06-28 22:57:17 +02:00
|
|
|
class DatabaseLockDriver extends AbstractLockDriver
|
2018-06-26 22:31:04 +02:00
|
|
|
{
|
|
|
|
/**
|
2018-07-04 23:37:22 +02:00
|
|
|
* (@inheritdoc)
|
2018-06-26 22:31:04 +02:00
|
|
|
*/
|
2018-06-28 23:06:14 +02:00
|
|
|
public function acquireLock($key, $timeout = 120)
|
2018-06-26 22:31:04 +02:00
|
|
|
{
|
|
|
|
$got_lock = false;
|
|
|
|
$start = time();
|
|
|
|
|
|
|
|
do {
|
|
|
|
dba::lock('locks');
|
2018-07-04 23:37:22 +02:00
|
|
|
$lock = dba::selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
2018-06-26 22:31:04 +02:00
|
|
|
|
|
|
|
if (DBM::is_result($lock)) {
|
|
|
|
if ($lock['locked']) {
|
|
|
|
// We want to lock something that was already locked by us? So we got the lock.
|
|
|
|
if ($lock['pid'] == getmypid()) {
|
|
|
|
$got_lock = true;
|
2018-07-04 23:37:22 +02:00
|
|
|
$this->markAcquire($key);
|
2018-06-26 22:31:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$lock['locked']) {
|
2018-07-04 23:37:22 +02:00
|
|
|
dba::update('locks', ['locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + 300seconds')], ['name' => $key]);
|
2018-06-26 22:31:04 +02:00
|
|
|
$got_lock = true;
|
2018-07-04 23:37:22 +02:00
|
|
|
$this->markAcquire($key);
|
2018-06-26 22:31:04 +02:00
|
|
|
}
|
2018-06-28 22:57:17 +02:00
|
|
|
} else {
|
2018-07-04 23:37:22 +02:00
|
|
|
dba::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + 300seconds')]);
|
2018-06-26 22:31:04 +02:00
|
|
|
$got_lock = true;
|
2018-07-04 23:37:22 +02:00
|
|
|
$this->markAcquire($key);
|
2018-06-26 22:31:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dba::unlock();
|
|
|
|
|
|
|
|
if (!$got_lock && ($timeout > 0)) {
|
|
|
|
usleep(rand(100000, 2000000));
|
|
|
|
}
|
|
|
|
} while (!$got_lock && ((time() - $start) < $timeout));
|
|
|
|
|
|
|
|
return $got_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-04 23:37:22 +02:00
|
|
|
* (@inheritdoc)
|
2018-06-26 22:31:04 +02:00
|
|
|
*/
|
2018-06-28 23:06:14 +02:00
|
|
|
public function releaseLock($key)
|
2018-06-26 22:31:04 +02:00
|
|
|
{
|
2018-07-04 23:37:22 +02:00
|
|
|
dba::delete('locks', ['name' => $key, 'pid' => getmypid()]);
|
2018-06-28 22:57:17 +02:00
|
|
|
|
2018-07-04 23:37:22 +02:00
|
|
|
$this->markRelease($key);
|
2018-06-26 22:31:04 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-04 23:37:22 +02:00
|
|
|
* (@inheritdoc)
|
2018-06-26 22:31:04 +02:00
|
|
|
*/
|
|
|
|
public function releaseAll()
|
|
|
|
{
|
2018-07-04 23:37:22 +02:00
|
|
|
dba::delete('locks', ['pid' => getmypid()]);
|
2018-06-28 22:57:17 +02:00
|
|
|
|
|
|
|
$this->acquiredLocks = [];
|
2018-06-26 22:31:04 +02:00
|
|
|
}
|
2018-07-04 23:37:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
|
|
|
public function isLocked($key)
|
|
|
|
{
|
|
|
|
$lock = dba::selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
|
|
|
|
|
|
|
if (DBM::is_result($lock)) {
|
|
|
|
return $lock['locked'] !== false;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-06-26 22:31:04 +02:00
|
|
|
}
|