8000 feature #18471 [Console] Add Lockable trait (geoffrey-brier) · symfony/symfony@43f9514 · GitHub
[go: up one dir, main page]

Skip to content

Commit 43f9514

Browse files
committed
feature #18471 [Console] Add Lockable trait (geoffrey-brier)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Console] Add Lockable trait | Q | A | ------------- | --- | Branch? | "master" | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | none for the moment :) Hi there, Since the 2.6 the `LockHandler` class was added to ease concurrency problems. There was a nice post about [using it in your commands](http://symfony.com/blog/new-in-symfony-2-6-lockhandler). From my humble experience, I find it a bit unpleasant/time consuming to always copy/paste the same code. So here is my proposal: Before: ```php class UpdateContentsCommand extends Command { protected function configure() { // ... } protected function execute(InputInterface $input, OutputInterface $output) { // create the lock $lock = new LockHandler('update:contents'); if (!$lock->lock()) { $output->writeln('The command is already running in another process.'); return 0; } // Your code $lock->release(); } } ``` After: ```php class MyCommand extends Command { use LockableTrait; protected function execute(InputInterface $input, OutputInterface $output) { if (!$this->lock()) { // Here you can handle locking errors } // Your code // The lock release is still optionnal $this->release(); } } ``` In addition, you can optionally pass two arguments: - a string argument to change the lock name - a boolean argument to indicate if you want to wait until the requested lock is released Commits ------- b57a83f [Console] Add Lockable trait
2 parents 6ede0b0 + b57a83f commit 43f9514

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* added `setInputs()` method to CommandTester for ease testing of commands expecting inputs
88
* added `setStream()` and `getStream()` methods to Input (implement StreamableInputInterface)
99
* added StreamableInputInterface
10+
* added LockableTrait
1011

1112
3.1.0
1213
-----
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Command;
13+
14+
use Symfony\Component\Console\Exception\LogicException;
15+
use Symfony\Component\Console\Exception\RuntimeException;
16+
use Symfony\Component\Filesystem\LockHandler;
17+
18+
/**
19+
* Basic lock feature for commands.
20+
*
21+
* @author Fabien Potencier <fabien@symfony.com>
22+
*/
23+
trait LockableTrait
24+
{
25+
protected $lockHandler;
26+
27+
/**
28+
* Locks a command.
29+
*
30+
* @return bool
31+
*/
32+
protected function lock($name = null, $blocking = false)
33+
{
34+
if (!class_exists(LockHandler::class)) {
35+
throw new RuntimeException('To enable the locking feature you must install the symfony/filesystem component.');
36+
}
37+
38+
if (null !== $this->lockHandler) {
39+
throw new LogicException('A lock is already in place.');
40+
}
41+
42+
$this->lockHandler = new LockHandler($name ?: $this->getName());
43+
44+
if (!$this->lockHandler->lock($blocking)) {
45+
$this->lockHandler = null;
46+
47+
return false;
48+
}
49+
50+
return true;
51+
}
52+
53+
/**
54+
* Releases the command lock if there is one.
55+
*/
56+
protected function release()
57+
{
58+
if ($this->lockHandler) {
59+
$this->lockHandler->release();
60+
$this->lockHandler = null;
61+
}
62+
}
63+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\Command;
13+
14+
use Symfony\Component\Console\Tester\CommandTester;
15+
use Symfony\Component\Filesystem\LockHandler;
16+
17+
class LockableTraitTest extends \PHPUnit_Framework_TestCase
18+
{
19+
protected static $fixturesPath;
20+
21+
public static function setUpBeforeClass()
22+
{
23+
self::$fixturesPath = __DIR__.'/../Fixtures/';
24+
require_once self::$fixturesPath.'/FooLockCommand.php';
25+
require_once self::$fixturesPath.'/FooLock2Command.php';
26+
}
27+
28+
public function testLockIsReleased()
29+
{
30+
$command = new \FooLockCommand();
31+
32+
$tester = new CommandTester($command);
33+
$this->assertSame(2, $tester->execute(array()));
34+
$this->assertSame(2, $tester->execute(array()));
35+
}
36+
37+
public function testLockReturnsFalseIfAlreadyLockedByAnotherCommand()
38+
{
39+
$command = new \FooLockCommand();
40+
41+
$lock = new LockHandler($command->getName());
42+
$lock->lock();
43+
44+
$tester = new CommandTester($command);
45+
$this->assertSame(1, $tester->execute(array()));
46+
47+
$lock->release();
48+
$this->assertSame(2, $tester->execute(array()));
49+
}
50+
51+
public function testMultipleLockCallsThrowLogicException()
52+
{
53+
$command = new \FooLock2Command();
54+
55+
$tester = new CommandTester($command);
56+
$this->assertSame(1, $tester->execute(array()));
57+
}
58+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Command\LockableTrait;
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
class FooLock2Command extends Command
9+
{
10+
use LockableTrait;
11+
12+
protected function configure()
13+
{
14+
$this->setName('foo:lock2');
15+
}
16+
17+
protected function execute(InputInterface $input, OutputInterface $output)
18+
{
19+
try {
20+
$this->lock();
21+
$this->lock();
22+
} catch (LogicException $e) {
23+
return 1;
24+
}
25+
26+
return 2;
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Command\LockableTrait;
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
class FooLockCommand extends Command
9+
{
10+
use LockableTrait;
11+
12+
protected function configure()
13+
{
14+
$this->setName('foo:lock');
15+
}
16+
17+
protected function execute(InputInterface $input, OutputInterface $output)
18+
{
19+
if (!$this->lock()) {
20+
return 1;
21+
}
22+
23+
$this->release();
24+
25+
return 2;
26+
}
27+
}

src/Symfony/Component/Console/composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
},
2222
"require-dev": {
2323
"symfony/event-dispatcher": "~2.8|~3.0",
24+
"symfony/filesystem": "~2.8|~3.0",
2425
"symfony/process": "~2.8|~3.0",
2526
"psr/log": "~1.0"
2627
},
2728
"suggest": {
2829
"symfony/event-dispatcher": "",
30+
"symfony/filesystem": "",
2931
"symfony/process": "",
3032
"psr/log": "For using the console logger"
3133
},

0 commit comments

Comments
 (0)
0