]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Lock/DatabaseLockDriver.php
some minor fixings
[friendica.git] / src / Core / Lock / DatabaseLockDriver.php
index a8788d1f45bf1400b9e96a125c10c28bc9891394..a8269bc92ea6ad16004c9dcdad2447e569d0f8a5 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,13 +68,19 @@ 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];
+               }
+
+               $return = DBA::delete('locks', $where);
 
                $this->markRelease($key);
 
-               return;
+               return $return;
        }
 
        /**
@@ -67,9 +88,11 @@ class DatabaseLockDriver extends AbstractLockDriver
         */
        public function releaseAll()
        {
-               DBA::delete('locks', ['pid' => getmypid()]);
+               $return = DBA::delete('locks', ['pid' => $this->pid]);
 
                $this->acquiredLocks = [];
+
+               return $return;
        }
 
        /**