Skip to content

Commit 9d57856

Browse files
authored
Merge pull request #14 from ReputationVIP/use-new-lock-component
Use new lock component
2 parents 391d84c + 3624b5f commit 9d57856

8 files changed

Lines changed: 280 additions & 245 deletions

File tree

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
language: php
22
php:
3-
- 5.6
3+
- 7.0
4+
- 7.1
5+
- 7.2
46

57
script:
68
- composer install

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Queue Client Changelog
22

3+
## v2.0.0
4+
5+
- Use new Symfony/Lock component in Filesystem handler
6+
37
## v1.0.3
48

59
- Use Priority objects instead of string

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
}
99
],
1010
"require": {
11-
"symfony/filesystem": ">=2.7",
12-
"symfony/finder": ">=2.7",
11+
"symfony/filesystem": ">=3.4",
12+
"symfony/finder": ">=3.4",
13+
"symfony/lock": ">=3.4",
1314
"aws/aws-sdk-php": ">=2.7"
1415
},
1516
"require-dev": {
16-
"atoum/atoum": "~2",
17+
"atoum/atoum": "~3.1",
1718
"satooshi/php-coveralls": "dev-master"
1819
},
1920
"autoload": {

docker/test/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM php:7
1+
FROM php:7.1
22

33
RUN pecl install xdebug
44
RUN docker-php-ext-enable xdebug
@@ -9,4 +9,4 @@ RUN apt-get update && apt-get install -y \
99

1010
COPY ./ssh/ssh_config /etc/ssh/ssh_config
1111

12-
WORKDIR /data
12+
WORKDIR /data

src/Adapter/FileAdapter.php

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
use ReputationVIP\QueueClient\PriorityHandler\Priority\Priority;
88
use ReputationVIP\QueueClient\PriorityHandler\PriorityHandlerInterface;
99
use ReputationVIP\QueueClient\PriorityHandler\StandardPriorityHandler;
10-
use ReputationVIP\QueueClient\Utils\LockHandlerFactory;
11-
use ReputationVIP\QueueClient\Utils\LockHandlerFactoryInterface;
1210
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
1311
use Symfony\Component\Filesystem\Filesystem;
1412
use Symfony\Component\Finder\Finder;
1513
use Symfony\Component\Finder\SplFileInfo;
14+
use Symfony\Component\Lock\Factory;
15+
use Symfony\Component\Lock\Store\FlockStore;
1616

1717
class 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

src/Utils/LockHandlerFactory.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Utils/LockHandlerFactoryInterface.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)