X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FLock%2FDatabaseLockDriver.php;h=a137ef12edcac6e6766af4f704128e3ce97085d3;hb=b06fc578804fdc0073631e7c97eccd25543e769e;hp=a8788d1f45bf1400b9e96a125c10c28bc9891394;hpb=cfa68c52b9117616fa95a4639ad74e7d220d193d;p=friendica.git diff --git a/src/Core/Lock/DatabaseLockDriver.php b/src/Core/Lock/DatabaseLockDriver.php index a8788d1f45..a137ef12ed 100644 --- a/src/Core/Lock/DatabaseLockDriver.php +++ b/src/Core/Lock/DatabaseLockDriver.php @@ -11,6 +11,21 @@ use Friendica\Util\DateTimeFormat; */ class DatabaseLockDriver extends AbstractLockDriver { + /** + * The current ID of the process + * + * @var int + */ + private $pid; + + /** + * @param null|int $pid The Id of the current process (null means determine automatically) + */ + public function __construct($pid = null) + { + $this->pid = isset($pid) ? $pid : getmypid(); + } + /** * (@inheritdoc) */ @@ -26,16 +41,16 @@ class DatabaseLockDriver extends AbstractLockDriver if (DBA::isResult($lock)) { if ($lock['locked']) { // We want to lock something that was already locked by us? So we got the lock. - if ($lock['pid'] == getmypid()) { + if ($lock['pid'] == $this->pid) { $got_lock = true; } } if (!$lock['locked']) { - DBA::update('locks', ['locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]); + DBA::update('locks', ['locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]); $got_lock = true; } } else { - DBA::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]); + DBA::insert('locks', ['name' => $key, 'locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]); $got_lock = true; $this->markAcquire($key); } @@ -53,9 +68,15 @@ class DatabaseLockDriver extends AbstractLockDriver /** * (@inheritdoc) */ - public function releaseLock($key) + public function releaseLock($key, $override = false) { - DBA::delete('locks', ['name' => $key, 'pid' => getmypid()]); + if ($override) { + $where = ['name' => $key]; + } else { + $where = ['name' => $key, 'pid' => $this->pid]; + } + + DBA::delete('locks', $where); $this->markRelease($key); @@ -67,7 +88,7 @@ class DatabaseLockDriver extends AbstractLockDriver */ public function releaseAll() { - DBA::delete('locks', ['pid' => getmypid()]); + DBA::delete('locks', ['pid' => $this->pid]); $this->acquiredLocks = []; }