]> git.mxchange.org Git - friendica.git/blob - src/Util/Lock.php
Lock now can use the memcache as well
[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         public static function memcache() {
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          * @param integer $wait_sec Time between to lock attempts
52          *
53          * @return boolean Was the lock successful?
54          */
55         public static function set($fn_name, $timeout = 30, $wait_sec = 2) {
56                 if ($wait_sec == 0) {
57                         $wait_sec = 2;
58                 }
59
60                 $got_lock = false;
61                 $start = time();
62
63                 $memcache = self::memcache();
64                 if (is_object($memcache)) {
65                         $cachekey = get_app()->get_hostname().";lock:".$fn_name;
66
67                         do {
68                                 $lock = $memcache->get($cachekey);
69
70                                 if (!is_bool($lock)) {
71                                         $pid = (int)$lock;
72
73                                         // When the process id isn't used anymore, we can safely claim the lock for us.
74                                         // Or we do want to lock something that was already locked by us.
75                                         if (!posix_kill($pid, 0) OR ($pid == getmypid())) {
76                                                 $lock = false;
77                                         }
78                                 }
79                                 if (is_bool($lock)) {
80                                         $memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
81                                         $got_lock = true;
82                                 }
83                                 if (!$got_lock) {
84                                         sleep($wait_sec);
85                                 }
86                         } while (!$got_lock AND ((time() - $start) < $timeout));
87
88                         return $got_lock;
89                 }
90                 do {
91                         dba::lock('locks');
92                         $lock = dba::select('locks', array('locked', 'pid'), array('name' => $fn_name), array('limit' => 1));
93
94                         if (dbm::is_result($lock)) {
95                                 if ($lock['locked']) {
96                                         // When the process id isn't used anymore, we can safely claim the lock for us.
97                                         if (!posix_kill($lock['pid'], 0)) {
98                                                 $lock['locked'] = false;
99                                         }
100                                         // We want to lock something that was already locked by us? So we got the lock.
101                                         if ($lock['pid'] == getmypid()) {
102                                                 $got_lock = true;
103                                         }
104                                 }
105                                 if (!$lock['locked']) {
106                                         dba::update('locks', array('locked' => true, 'pid' => getmypid()), array('name' => $fn_name));
107                                         $got_lock = true;
108                                 }
109                         } elseif (!dbm::is_result($lock)) {
110                                 dba::insert('locks', array('name' => $fn_name, 'locked' => true, 'pid' => getmypid()));
111                                 $got_lock = true;
112                         }
113
114                         dba::unlock();
115
116                         if (!$got_lock) {
117                                 sleep($wait_sec);
118                         }
119                 } while (!$got_lock AND ((time() - $start) < $timeout));
120
121                 return $got_lock;
122         }
123
124         /**
125          * @brief Removes a lock if it was set by us
126          *
127          * @param string $fn_name Name of the lock
128          */
129         public static function remove($fn_name) {
130                 $memcache = self::memcache();
131                 if (is_object($memcache)) {
132                         $cachekey = get_app()->get_hostname().";lock:".$fn_name;
133                         $lock = $memcache->get($cachekey);
134
135                         if (!is_bool($lock)) {
136                                 if ((int)$lock == getmypid()) {
137                                         $memcache->delete($cachekey);
138                                 }
139                         }
140                         return;
141                 }
142
143                 dba::update('locks', array('locked' => false, 'pid' => 0), array('name' => $fn_name, 'pid' => getmypid()));
144                 return;
145         }
146
147         /**
148          * @brief Removes all lock that were set by us
149          */
150         public static function removeAll() {
151                 $memcache = self::memcache();
152                 if (is_object($memcache)) {
153                         // We cannot delete all cache entries, but this doesn't matter with memcache
154                         return;
155                 }
156
157                 dba::update('locks', array('locked' => false, 'pid' => 0), array('pid' => getmypid()));
158                 return;
159         }
160 }