]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Lock/DatabaseLockDriver.php
Merge pull request #6704 from tobiasd/20190220-credits
[friendica.git] / src / Core / Lock / DatabaseLockDriver.php
index a8788d1f45bf1400b9e96a125c10c28bc9891394..a137ef12edcac6e6766af4f704128e3ce97085d3 100644 (file)
@@ -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 = [];
        }