]> git.mxchange.org Git - friendica.git/blob - src/Core/Lock/DatabaseLock.php
wrapping up 2019.12
[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                 if ($this->dba->exists('locks', $where)) {
86                         $return = $this->dba->delete('locks', $where);
87                 } else {
88                         $return = false;
89                 }
90
91                 $this->markRelease($key);
92
93                 return $return;
94         }
95
96         /**
97          * (@inheritdoc)
98          */
99         public function releaseAll($override = false)
100         {
101                 $success = parent::releaseAll($override);
102
103                 if ($override) {
104                         $where = ['1 = 1'];
105                 } else {
106                         $where = ['pid' => $this->pid];
107                 }
108                 $return = $this->dba->delete('locks', $where);
109
110                 $this->acquiredLocks = [];
111
112                 return $return && $success;
113         }
114
115         /**
116          * (@inheritdoc)
117          */
118         public function isLocked($key)
119         {
120                 $lock = $this->dba->selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
121
122                 if ($this->dba->isResult($lock)) {
123                         return $lock['locked'] !== false;
124                 } else {
125                         return false;
126                 }
127         }
128
129         /**
130          * {@inheritDoc}
131          */
132         public function getName()
133         {
134                 return self::TYPE_DATABASE;
135         }
136
137         /**
138          * {@inheritDoc}
139          */
140         public function getLocks(string $prefix = '')
141         {
142                 if (empty($prefix)) {
143                         $where = ['`expires` >= ?', DateTimeFormat::utcNow()];
144                 } else {
145                         $where = ['`expires` >= ? AND `name` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
146                 }
147
148                 $stmt = $this->dba->select('locks', ['name'], $where);
149
150                 $keys = [];
151                 while ($key = $this->dba->fetch($stmt)) {
152                         array_push($keys, $key['name']);
153                 }
154                 $this->dba->close($stmt);
155
156                 return $keys;
157         }
158 }