19
19
*/
20
20
class Database implements \Magento \Framework \Lock \LockManagerInterface
21
21
{
22
- /** @var ResourceConnection */
22
+ /**
23
+ * Max time for lock is 1 week
24
+ *
25
+ * MariaDB does not support negative timeout value to get infinite timeout,
26
+ * so we set 1 week for lock timeout
27
+ */
28
+ const MAX_LOCK_TIME = 604800 ;
29
+
30
+ /**
31
+ * @var ResourceConnection
32
+ */
23
33
private $ resource ;
24
34
25
- /** @var DeploymentConfig */
35
+ /**
36
+ * @var DeploymentConfig
37
+ */
26
38
private $ deploymentConfig ;
27
39
28
- /** @var string Lock prefix */
40
+ /**
41
+ * @var string Lock prefix
42
+ */
29
43
private $ prefix ;
30
44
31
- /** @var string|false Holds current lock name if set, otherwise false */
45
+ /**
46
+ * @var string|false Holds current lock name if set, otherwise false
47
+ */
32
48
private $ currentLock = false ;
33
49
34
50
/**
35
- * Database constructor.
36
- *
37
51
* @param ResourceConnection $resource
38
52
* @param DeploymentConfig $deploymentConfig
39
53
* @param string|null $prefix
@@ -81,7 +95,7 @@ public function lock(string $name, int $timeout = -1): bool
81
95
82
96
$ result = (bool )$ this ->resource ->getConnection ()->query (
83
97
"SELECT GET_LOCK(?, ?); " ,
84
- [( string ) $ name , ( int ) $ timeout ]
98
+ [$ name , $ timeout < 0 ? self :: MAX_LOCK_TIME : $ timeout ]
85
99
)->fetchColumn ();
86
100
87
101
if ($ result === true ) {
@@ -104,6 +118,7 @@ public function unlock(string $name): bool
104
118
if (!$ this ->deploymentConfig ->isDbAvailable ()) {
105
119
return true ;
106
120
};
121
+
107
122
$ name = $ this ->addPrefix ($ name );
108
123
109
124
$ result = (bool )$ this ->resource ->getConnection ()->query (
@@ -131,11 +146,12 @@ public function isLocked(string $name): bool
131
146
if (!$ this ->deploymentConfig ->isDbAvailable ()) {
132
147
return false ;
133
148
};
149
+
134
150
$ name = $ this ->addPrefix ($ name );
135
151
136
152
return (bool )$ this ->resource ->getConnection ()->query (
137
153
"SELECT IS_USED_LOCK(?); " ,
138
- [( string ) $ name ]
154
+ [$ name ]
139
155
)->fetchColumn ();
140
156
}
141
157
@@ -145,7 +161,7 @@ public function isLocked(string $name): bool
145
161
* Limited to 64 characters in MySQL.
146
162
*
147
163
* @param string $name
148
- * @return string $name
164
+ * @return string
149
165
* @throws InputException
150
166
*/
151
167
private function addPrefix (string $ name ): string
0 commit comments