11
11
namespace hiqdev \assetpackagist \components ;
12
12
13
13
use hiqdev \assetpackagist \exceptions \AssetFileStorageException ;
14
- use hiqdev \assetpackagist \helpers \Locker ;
15
14
use hiqdev \assetpackagist \models \AssetPackage ;
16
15
use Yii ;
17
16
use yii \base \Component ;
@@ -30,26 +29,35 @@ public function init()
30
29
$ this ->_path = Yii::getAlias ('@storage ' , false );
31
30
}
32
31
33
- protected function getLocker ()
32
+ protected function acquireLock ()
34
33
{
35
- if ($ this ->_locker === null ) {
36
- $ this ->_locker = Locker::getInstance ($ this ->buildPath ('lock ' )); // TODO: get rid of singleton
34
+ /* @var $mutex \yii\mutex\Mutex */
35
+ $ mutex = Yii::$ app ->mutex ;
36
+
37
+ if (!$ mutex ->acquire ('lock ' , 5 )) {
38
+ throw new \Exception ('failed get lock ' );
37
39
}
40
+ }
41
+
42
+ protected function releaseLock ()
43
+ {
44
+ /* @var $mutex \yii\mutex\Mutex */
45
+ $ mutex = Yii::$ app ->mutex ;
38
46
39
- return $ this -> _locker ;
47
+ $ mutex -> release ( ' lock ' ) ;
40
48
}
41
49
42
50
/**
43
51
* {@inheritdoc}
44
52
*/
45
53
public function getNextId ()
46
54
{
47
- $ this ->getLocker ()-> lock ();
55
+ $ this ->acquireLock ();
48
56
{
49
57
$ nextID = $ this ->readLastId () + 1 ;
50
58
$ this ->writeLastId ($ nextID );
51
59
}
52
- $ this ->getLocker ()-> release ();
60
+ $ this ->releaseLock ();
53
61
54
62
return $ nextID ;
55
63
}
@@ -89,7 +97,7 @@ public function writePackage(AssetPackage $package)
89
97
$ path = $ this ->buildHashedPath ($ name , $ hash );
90
98
$ latestPath = $ this ->buildHashedPath ($ name );
91
99
if (!file_exists ($ path )) {
92
- $ this ->getLocker ()-> lock ();
100
+ $ this ->acquireLock ();
93
101
try {
94
102
if ($ this ->mkdir (dirname ($ path )) === false ) {
95
103
throw new AssetFileStorageException ('Failed to create a directory for asset-package ' , $ package );
@@ -102,7 +110,7 @@ public function writePackage(AssetPackage $package)
102
110
}
103
111
$ this ->writeProviderLatest ($ name , $ hash );
104
112
} finally {
105
- $ this ->getLocker ()-> release ();
113
+ $ this ->releaseLock ();
106
114
}
107
115
} else {
108
116
touch ($ latestPath );
@@ -162,7 +170,7 @@ protected function writeProviderLatest($name, $hash)
162
170
$ path = $ this ->buildHashedPath ('provider-latest ' , $ hash );
163
171
164
172
if (!file_exists ($ path )) {
165
- $ this ->getLocker ()-> lock ();
173
+ $ this ->acquireLock ();
166
174
167
175
try {
168
176
if ($ this ->mkdir (dirname ($ path )) === false ) {
@@ -176,7 +184,7 @@ protected function writeProviderLatest($name, $hash)
176
184
}
177
185
$ this ->writePackagesJson ($ hash );
178
186
} finally {
179
- $ this ->getLocker ()-> release ();
187
+ $ this ->releaseLock ();
180
188
}
181
189
} else {
182
190
touch ($ latestPath );
@@ -195,15 +203,15 @@ protected function writePackagesJson($hash)
195
203
],
196
204
],
197
205
];
198
- $ this ->getLocker ()-> lock ();
206
+ $ this ->acquireLock ();
199
207
$ filename = $ this ->buildPath ('packages.json ' );
200
208
try {
201
209
if (file_put_contents ($ filename , Json::encode ($ data )) === false ) {
202
210
throw new AssetFileStorageException ('Failed to write main packages.json ' );
203
211
}
204
212
touch ($ filename );
205
213
} finally {
206
- $ this ->getLocker ()-> release ();
214
+ $ this ->releaseLock ();
207
215
}
208
216
}
209
217
0 commit comments