77use ReputationVIP \QueueClient \PriorityHandler \Priority \Priority ;
88use ReputationVIP \QueueClient \PriorityHandler \PriorityHandlerInterface ;
99use ReputationVIP \QueueClient \PriorityHandler \StandardPriorityHandler ;
10- use ReputationVIP \QueueClient \Utils \LockHandlerFactory ;
11- use ReputationVIP \QueueClient \Utils \LockHandlerFactoryInterface ;
1210use Symfony \Component \Filesystem \Exception \IOExceptionInterface ;
1311use Symfony \Component \Filesystem \Filesystem ;
1412use Symfony \Component \Finder \Finder ;
1513use Symfony \Component \Finder \SplFileInfo ;
14+ use Symfony \Component \Lock \Factory ;
15+ use Symfony \Component \Lock \Store \FlockStore ;
1616
1717class FileAdapter extends AbstractAdapter implements AdapterInterface
1818{
@@ -31,7 +31,7 @@ class FileAdapter extends AbstractAdapter implements AdapterInterface
3131 /** @var Filesystem $fs */
3232 private $ fs ;
3333
34- /** @var LockHandlerFactoryInterface $fs */
34+ /** @var Factory $lockHandlerFactory */
3535 private $ lockHandlerFactory ;
3636
3737 /** @var PriorityHandlerInterface $priorityHandler */
@@ -42,12 +42,12 @@ class FileAdapter extends AbstractAdapter implements AdapterInterface
4242 * @param PriorityHandlerInterface $priorityHandler
4343 * @param Filesystem $fs
4444 * @param Finder $finder
45- * @param LockHandlerFactoryInterface $lockHandlerFactory
45+ * @param Factory $lockHandlerFactory
4646 *
4747 * @throws \InvalidArgumentException
4848 * @throws QueueAccessException
4949 */
50- public function __construct ($ repository , PriorityHandlerInterface $ priorityHandler = null , Filesystem $ fs = null , Finder $ finder = null , LockHandlerFactoryInterface $ lockHandlerFactory = null )
50+ public function __construct ($ repository , PriorityHandlerInterface $ priorityHandler = null , Filesystem $ fs = null , Finder $ finder = null , Factory $ lockHandlerFactory = null )
5151 {
5252 if (empty ($ repository )) {
5353 throw new \InvalidArgumentException ('Argument repository empty or not defined. ' );
@@ -61,15 +61,12 @@ public function __construct($repository, PriorityHandlerInterface $priorityHandl
6161 $ finder = new Finder ();
6262 }
6363
64- if (null === $ lockHandlerFactory ) {
65- $ lockHandlerFactory = new LockHandlerFactory ();
66- }
67-
6864 if (null === $ priorityHandler ) {
6965 $ priorityHandler = new StandardPriorityHandler ();
7066 }
7167
7268 $ this ->fs = $ fs ;
69+
7370 if (!$ this ->fs ->exists ($ repository )) {
7471 try {
7572 $ this ->fs ->mkdir ($ repository );
@@ -78,6 +75,10 @@ public function __construct($repository, PriorityHandlerInterface $priorityHandl
7875 }
7976 }
8077
78+ if (null === $ lockHandlerFactory ) {
79+ $ lockHandlerFactory = new Factory (new FlockStore ($ repository ));
80+ }
81+
8182 $ this ->priorityHandler = $ priorityHandler ;
8283 $ this ->repository = $ repository ;
8384 $ this ->finder = $ finder ;
@@ -126,8 +127,8 @@ private function getQueuePath($queueName, Priority $priority)
126127 private function readQueueFromFile ($ queueName , Priority $ priority , $ nbTries = 0 )
127128 {
128129 $ queueFilePath = $ this ->getQueuePath ($ queueName , $ priority );
129- $ lockHandler = $ this ->lockHandlerFactory ->getLockHandler ($ queueFilePath );
130- if (!$ lockHandler -> lock ()) {
130+ $ lock = $ this ->lockHandlerFactory ->createLock ($ queueFilePath );
131+ if (!$ lock -> acquire ()) {
131132 if ($ nbTries >= static ::MAX_LOCK_TRIES ) {
132133 throw new QueueAccessException ('Reach max retry for locking queue file ' . $ queueFilePath );
133134 }
@@ -148,10 +149,10 @@ private function readQueueFromFile($queueName, Priority $priority, $nbTries = 0)
148149 }
149150 $ queue = json_decode ($ content , true );
150151 } catch (\Exception $ e ) {
151- $ lockHandler ->release ();
152+ $ lock ->release ();
152153 throw $ e ;
153154 }
154- $ lockHandler ->release ();
155+ $ lock ->release ();
155156
156157 return $ queue ;
157158 }
@@ -170,8 +171,8 @@ private function readQueueFromFile($queueName, Priority $priority, $nbTries = 0)
170171 private function writeQueueInFile ($ queueName , Priority $ priority , $ queue , $ nbTries = 0 )
171172 {
172173 $ queueFilePath = $ this ->getQueuePath ($ queueName , $ priority );
173- $ lockHandler = $ this ->lockHandlerFactory ->getLockHandler ($ queueFilePath );
174- if (!$ lockHandler -> lock ()) {
174+ $ lock = $ this ->lockHandlerFactory ->createLock ($ queueFilePath );
175+ if (!$ lock -> acquire ()) {
175176 if ($ nbTries >= static ::MAX_LOCK_TRIES ) {
176177 throw new QueueAccessException ('Reach max retry for locking queue file ' . $ queueFilePath );
177178 }
@@ -182,10 +183,10 @@ private function writeQueueInFile($queueName, Priority $priority, $queue, $nbTri
182183 $ queueJson = json_encode ($ queue );
183184 $ this ->fs ->dumpFile ($ queueFilePath , $ queueJson );
184185 } catch (\Exception $ e ) {
185- $ lockHandler ->release ();
186+ $ lock ->release ();
186187 throw $ e ;
187188 }
188- $ lockHandler ->release ();
189+ $ lock ->release ();
189190 return $ this ;
190191 }
191192
@@ -205,8 +206,8 @@ private function writeQueueInFile($queueName, Priority $priority, $queue, $nbTri
205206 private function addMessageLock ($ queueName , $ message , Priority $ priority , $ nbTries = 0 , $ delaySeconds = 0 )
206207 {
207208 $ queueFilePath = $ this ->getQueuePath ($ queueName , $ priority );
208- $ lockHandler = $ this ->lockHandlerFactory ->getLockHandler ($ queueFilePath );
209- if (!$ lockHandler -> lock ()) {
209+ $ lock = $ this ->lockHandlerFactory ->createLock ($ queueFilePath );
210+ if (!$ lock -> acquire ()) {
210211 if ($ nbTries >= static ::MAX_LOCK_TRIES ) {
211212 throw new QueueAccessException ('Reach max retry for locking queue file ' . $ queueFilePath );
212213 }
@@ -239,10 +240,10 @@ private function addMessageLock($queueName, $message, Priority $priority, $nbTri
239240 $ queueJson = json_encode ($ queue );
240241 $ this ->fs ->dumpFile ($ queueFilePath , $ queueJson );
241242 } catch (\Exception $ e ) {
242- $ lockHandler ->release ();
243+ $ lock ->release ();
243244 throw $ e ;
244245 }
245- $ lockHandler ->release ();
246+ $ lock ->release ();
246247 return $ this ;
247248 }
248249
@@ -291,8 +292,8 @@ public function addMessage($queueName, $message, Priority $priority = null, $del
291292 private function getMessagesLock ($ queueName , $ nbMsg , Priority $ priority , $ nbTries = 0 )
292293 {
293294 $ queueFilePath = $ this ->getQueuePath ($ queueName , $ priority );
294- $ lockHandler = $ this ->lockHandlerFactory ->getLockHandler ($ queueFilePath );
295- if (!$ lockHandler -> lock ()) {
295+ $ lock = $ this ->lockHandlerFactory ->createLock ($ queueFilePath );
296+ if (!$ lock -> acquire ()) {
296297 if ($ nbTries >= static ::MAX_LOCK_TRIES ) {
297298 throw new QueueAccessException ('Reach max retry for locking queue file ' . $ queueFilePath );
298299 }
@@ -335,10 +336,10 @@ private function getMessagesLock($queueName, $nbMsg, Priority $priority, $nbTrie
335336 $ queueJson = json_encode ($ queue );
336337 $ this ->fs ->dumpFile ($ queueFilePath , $ queueJson );
337338 } catch (\Exception $ e ) {
338- $ lockHandler ->release ();
339+ $ lock ->release ();
339340 throw $ e ;
340341 }
341- $ lockHandler ->release ();
342+ $ lock ->release ();
342343
343344 return $ messages ;
344345 }
@@ -399,8 +400,8 @@ public function getMessages($queueName, $nbMsg = 1, Priority $priority = null)
399400 private function deleteMessageLock ($ queueName , $ message , Priority $ priority , $ nbTries = 0 )
400401 {
401402 $ queueFilePath = $ this ->getQueuePath ($ queueName , $ priority );
402- $ lockHandler = $ this ->lockHandlerFactory ->getLockHandler ($ queueFilePath );
403- if (!$ lockHandler -> lock ()) {
403+ $ lock = $ this ->lockHandlerFactory ->createLock ($ queueFilePath );
404+ if (!$ lock -> acquire ()) {
404405 if ($ nbTries >= static ::MAX_LOCK_TRIES ) {
405406 throw new QueueAccessException ('Reach max retry for locking queue file ' . $ queueFilePath );
406407 }
@@ -431,10 +432,10 @@ private function deleteMessageLock($queueName, $message, Priority $priority, $nb
431432 $ queueJson = json_encode ($ queue );
432433 $ this ->fs ->dumpFile ($ queueFilePath , $ queueJson );
433434 } catch (\Exception $ e ) {
434- $ lockHandler ->release ();
435+ $ lock ->release ();
435436 throw $ e ;
436437 }
437- $ lockHandler ->release ();
438+ $ lock ->release ();
438439
439440 return $ this ;
440441 }
@@ -570,16 +571,16 @@ private function deleteQueueLock($queueName, Priority $priority, $nbTries = 0)
570571 }
571572
572573 $ queueFilePath = $ this ->getQueuePath ($ queueName , $ priority );
573- $ lockHandler = $ this ->lockHandlerFactory ->getLockHandler ($ queueFilePath );
574- if (!$ lockHandler -> lock ()) {
574+ $ lock = $ this ->lockHandlerFactory ->createLock ($ queueFilePath );
575+ if (!$ lock -> acquire ()) {
575576 if ($ nbTries >= static ::MAX_LOCK_TRIES ) {
576577 throw new QueueAccessException ('Reach max retry for locking queue file ' . $ queueFilePath );
577578 }
578579 usleep (10 );
579580 return $ this ->deleteQueueLock ($ queueName , $ priority , ($ nbTries + 1 ));
580581 }
581582 $ this ->fs ->remove ($ queueFilePath );
582- $ lockHandler ->release ();
583+ $ lock ->release ();
583584 return $ this ;
584585 }
585586
0 commit comments