]> git.mxchange.org Git - friendica.git/blob - src/Util/Lock.php
36f408cf324b4e1603c82e9c6d963579e0ab85cc
[friendica.git] / src / Util / Lock.php
1 <?php
2
3 namespace Friendica\Util;
4
5 /**
6  * @file src/Util/Lock.php
7  * @brief Functions for preventing parallel execution of functions
8  *
9  */
10
11 use Friendica\Core\Config;
12 use Memcache;
13 use dba;
14 use dbm;
15
16 /**
17  * @brief This class contain Functions for preventing parallel execution of functions
18  */
19 class Lock {
20        /**
21          * @brief Check for memcache and open a connection if configured
22          *
23          * @return object|boolean The memcache object - or "false" if not successful
24          */
25         private static function connectMemcache() {
26                 if (!function_exists('memcache_connect')) {
27                         return false;
28                 }
29
30                 if (!Config::get('system', 'memcache')) {
31                         return false;
32                 }
33
34                 $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
35                 $memcache_port = Config::get('system', 'memcache_port', 11211);
36
37                 $memcache = new Memcache;
38
39                 if (!$memcache->connect($memcache_host, $memcache_port)) {
40                         return false;
41                 }
42
43                 return $memcache;
44         }
45
46         /**
47          * @brief Sets a lock for a given name
48          *
49          * @param string $fn_name Name of the lock
50          * @param integer $timeout Seconds until we give up
51          *
52          * @return boolean Was the lock successful?
53          */
54         public static function set($fn_name, $timeout = 120) {
55                 $got_lock = false;
56                 $start = time();
57
58                 $memcache = self::connectMemcache();
59                 if (is_object($memcache)) {
60                         $cachekey = get_app()->get_hostname().";lock:".$fn_name;
61
62                         do {
63                                 // We only lock to be sure that nothing happens at exactly the same time
64                                 dba::lock('locks');
65                                 $lock = $memcache->get($cachekey);
66
67                                 if (!is_bool($lock)) {
68                                         $pid = (int)$lock;
69
70                                         // When the process id isn't used anymore, we can safely claim the lock for us.
71                                         // Or we do want to lock something that was already locked by us.
72                                         if (!posix_kill($pid, 0) || ($pid == getmypid())) {
73                                                 $lock = false;
74                                         }
75                                 }
76                                 if (is_bool($lock)) {
77                                         $memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
78                                         $got_lock = true;
79                                 }
80
81                                 dba::unlock();
82
83                                 if (!$got_lock && ($timeout > 0)) {
84                                         usleep(rand(10000, 200000));
85                                 }
86                         } while (!$got_lock && ((time() - $start) < $timeout));
87
88                         return $got_lock;
89                 }
90
91                 do {
92                         dba::lock('locks');
93                         $lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
94
95                         if (dbm::is_result($lock)) {
96                                 if ($lock['locked']) {
97                                         // When the process id isn't used anymore, we can safely claim the lock for us.
98                                         if (!posix_kill($lock['pid'], 0)) {
99                                                 $lock['locked'] = false;
100                                         }
101                                         // We want to lock something that was already locked by us? So we got the lock.
102                                         if ($lock['pid'] == getmypid()) {
103                                                 $got_lock = true;
104                                         }
105                                 }
106                                 if (!$lock['locked']) {
107                                         dba::update('locks', array('locked' => true, 'pid' => getmypid()), array('name' => $fn_name));
108                                         $got_lock = true;
109                                 }
110                         } elseif (!dbm::is_result($lock)) {
111                                 dba::insert('locks', array('name' => $fn_name, 'locked' => true, 'pid' => getmypid()));
112                                 $got_lock = true;
113                         }
114
115                         dba::unlock();
116
117                         if (!$got_lock && ($timeout > 0)) {
118                                 usleep(rand(100000, 2000000));
119                         }
120                 } while (!$got_lock && ((time() - $start) < $timeout));
121
122                 return $got_lock;
123         }
124
125         /**
126          * @brief Removes a lock if it was set by us
127          *
128          * @param string $fn_name Name of the lock
129          */
130         public static function remove($fn_name) {
131                 $memcache = self::connectMemcache();
132                 if (is_object($memcache)) {
133                         $cachekey = get_app()->get_hostname().";lock:".$fn_name;
134                         $lock = $memcache->get($cachekey);
135
136                         if (!is_bool($lock)) {
137                                 if ((int)$lock == getmypid()) {
138                                         $memcache->delete($cachekey);
139                                 }
140                         }
141                         return;
142                 }
143
144                 dba::update('locks', array('locked' => false, 'pid' => 0), array('name' => $fn_name, 'pid' => getmypid()));
145                 return;
146         }
147
148         /**
149          * @brief Removes all lock that were set by us
150          */
151         public static function removeAll() {
152                 $memcache = self::connectMemcache();
153                 if (is_object($memcache)) {
154                         // We cannot delete all cache entries, but this doesn't matter with memcache
155                         return;
156                 }
157
158                 dba::update('locks', array('locked' => false, 'pid' => 0), array('pid' => getmypid()));
159                 return;
160         }
161 }