]> git.mxchange.org Git - friendica.git/blob - src/Core/Lock/DatabaseLock.php
- Move constants to the "Cache" class (more transparent than inside the interface)
[friendica.git] / src / Core / Lock / DatabaseLock.php
1 <?php
2
3 namespace Friendica\Core\Lock;
4
5 use Friendica\Core\Cache;
6 use Friendica\Database\Database;
7 use Friendica\Util\DateTimeFormat;
8
9 /**
10  * Locking driver that stores the locks in the database
11  */
12 class DatabaseLock extends Lock
13 {
14         /**
15          * The current ID of the process
16          *
17          * @var int
18          */
19         private $pid;
20
21         /**
22          * @var Database The database connection of Friendica
23          */
24         private $dba;
25
26         /**
27          * @param null|int $pid The Id of the current process (null means determine automatically)
28          */
29         public function __construct(Database $dba, $pid = null)
30         {
31                 $this->dba = $dba;
32                 $this->pid = isset($pid) ? $pid : getmypid();
33         }
34
35         /**
36          * (@inheritdoc)
37          */
38         public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
39         {
40                 $got_lock = false;
41                 $start    = time();
42
43                 do {
44                         $this->dba->lock('locks');
45                         $lock = $this->dba->selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
46
47                         if ($this->dba->isResult($lock)) {
48                                 if ($lock['locked']) {
49                                         // We want to lock something that was already locked by us? So we got the lock.
50                                         if ($lock['pid'] == $this->pid) {
51                                                 $got_lock = true;
52                                         }
53                                 }
54                                 if (!$lock['locked']) {
55                                         $this->dba->update('locks', ['locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
56                                         $got_lock = true;
57                                 }
58                         } else {
59                                 $this->dba->insert('locks', ['name' => $key, 'locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
60                                 $got_lock = true;
61                                 $this->markAcquire($key);
62                         }
63
64                         $this->dba->unlock();
65
66                         if (!$got_lock && ($timeout > 0)) {
67                                 usleep(rand(100000, 2000000));
68                         }
69                 } while (!$got_lock && ((time() - $start) < $timeout));
70
71                 return $got_lock;
72         }
73
74         /**
75          * (@inheritdoc)
76          */
77         public function releaseLock($key, $override = false)
78         {
79                 if ($override) {
80                         $where = ['name' => $key];
81                 } else {
82                         $where = ['name' => $key, 'pid' => $this->pid];
83                 }
84
85                 $return = $this->dba->delete('locks', $where);
86
87                 $this->markRelease($key);
88
89                 return $return;
90         }
91
92         /**
93          * (@inheritdoc)
94          */
95         public function releaseAll()
96         {
97                 $return = $this->dba->delete('locks', ['pid' => $this->pid]);
98
99                 $this->acquiredLocks = [];
100
101                 return $return;
102         }
103
104         /**
105          * (@inheritdoc)
106          */
107         public function isLocked($key)
108         {
109                 $lock = $this->dba->selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
110
111                 if ($this->dba->isResult($lock)) {
112                         return $lock['locked'] !== false;
113                 } else {
114                         return false;
115                 }
116         }
117 }