lockable middleware (#5)
This commit is contained in:
parent
1e99455b65
commit
d9dca7b78b
@ -96,4 +96,18 @@ abstract class CommandMessage
|
|||||||
|
|
||||||
return $options;
|
return $options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For lockable message
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function __serialize(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'command' => $this->getCommandName(),
|
||||||
|
'arguments' => $this->getArguments(),
|
||||||
|
'options' => $this->getOptions()
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
13
Messenger/Middleware/LockableMessage.php
Normal file
13
Messenger/Middleware/LockableMessage.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace RetailCrm\ServiceBundle\Messenger\Middleware;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface LockableMessage
|
||||||
|
*
|
||||||
|
* @package RetailCrm\ServiceBundle\Messenger\Middleware
|
||||||
|
*/
|
||||||
|
interface LockableMessage
|
||||||
|
{
|
||||||
|
public function __serialize(): array;
|
||||||
|
}
|
68
Messenger/Middleware/LockableMessageMiddleware.php
Normal file
68
Messenger/Middleware/LockableMessageMiddleware.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace RetailCrm\ServiceBundle\Messenger\Middleware;
|
||||||
|
|
||||||
|
use Symfony\Component\Lock\LockFactory;
|
||||||
|
use Symfony\Component\Messenger\Envelope;
|
||||||
|
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
|
||||||
|
use Symfony\Component\Messenger\Middleware\StackInterface;
|
||||||
|
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class LockableMessageMiddleware
|
||||||
|
*
|
||||||
|
* @package RetailCrm\ServiceBundle\Messenger\Middleware
|
||||||
|
*/
|
||||||
|
class LockableMessageMiddleware implements MiddlewareInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var LockFactory
|
||||||
|
*/
|
||||||
|
private $lockFactory;
|
||||||
|
|
||||||
|
public function __construct(LockFactory $lockFactory)
|
||||||
|
{
|
||||||
|
$this->lockFactory = $lockFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Envelope $envelope
|
||||||
|
* @param StackInterface $stack
|
||||||
|
*
|
||||||
|
* @return Envelope
|
||||||
|
*
|
||||||
|
* @throws Throwable
|
||||||
|
*/
|
||||||
|
public function handle(Envelope $envelope, StackInterface $stack): Envelope
|
||||||
|
{
|
||||||
|
$message = $envelope->getMessage();
|
||||||
|
|
||||||
|
if ($envelope->all(ReceivedStamp::class) && $message instanceof LockableMessage) {
|
||||||
|
$lock = $this->lockFactory->createLock($this->objectHash($message), null);
|
||||||
|
if (!$lock->acquire()) {
|
||||||
|
return $envelope;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return $stack->next()->handle($envelope, $stack);
|
||||||
|
} catch (Throwable $exception) {
|
||||||
|
throw $exception;
|
||||||
|
} finally {
|
||||||
|
$lock->release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $stack->next()->handle($envelope, $stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param LockableMessage $message
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function objectHash(LockableMessage $message): string
|
||||||
|
{
|
||||||
|
return hash('crc32', serialize($message));
|
||||||
|
}
|
||||||
|
}
|
@ -3,8 +3,9 @@
|
|||||||
namespace RetailCrm\ServiceBundle\Tests\DataFixtures;
|
namespace RetailCrm\ServiceBundle\Tests\DataFixtures;
|
||||||
|
|
||||||
use RetailCrm\ServiceBundle\Messenger\CommandMessage;
|
use RetailCrm\ServiceBundle\Messenger\CommandMessage;
|
||||||
|
use RetailCrm\ServiceBundle\Messenger\Middleware\LockableMessage;
|
||||||
|
|
||||||
class TestMessage extends CommandMessage
|
class TestMessage extends CommandMessage implements LockableMessage
|
||||||
{
|
{
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
|
113
Tests/Messenger/Middleware/LockableMessageMiddlewareTest.php
Normal file
113
Tests/Messenger/Middleware/LockableMessageMiddlewareTest.php
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace RetailCrm\ServiceBundle\Tests\Messenger\Middleware;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use RetailCrm\ServiceBundle\Messenger\Middleware\LockableMessageMiddleware;
|
||||||
|
use RetailCrm\ServiceBundle\Tests\DataFixtures\TestMessage;
|
||||||
|
use Symfony\Component\Lock\Exception\LockConflictedException;
|
||||||
|
use Symfony\Component\Lock\Key;
|
||||||
|
use Symfony\Component\Lock\Lock;
|
||||||
|
use Symfony\Component\Lock\LockFactory;
|
||||||
|
use Symfony\Component\Lock\PersistingStoreInterface;
|
||||||
|
use Symfony\Component\Messenger\Envelope;
|
||||||
|
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
|
||||||
|
use Symfony\Component\Messenger\Middleware\StackInterface;
|
||||||
|
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class LockableMessageMiddlewareTest
|
||||||
|
*
|
||||||
|
* @package RetailCrm\ServiceBundle\Tests\Messenger\Middleware
|
||||||
|
*/
|
||||||
|
class LockableMessageMiddlewareTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var LockFactory
|
||||||
|
*/
|
||||||
|
private $lockFactory;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->lockFactory = $this->createMock(LockFactory::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHandle(): void
|
||||||
|
{
|
||||||
|
$store = $this->createMock(PersistingStoreInterface::class);
|
||||||
|
$key = new Key(uniqid());
|
||||||
|
$lock = new Lock($key, $store);
|
||||||
|
$this->lockFactory->expects(static::once())->method('createLock')->willReturn($lock);
|
||||||
|
$envelope = new Envelope(new TestMessage(), [new ReceivedStamp('test')]);
|
||||||
|
|
||||||
|
$next = $this->createMock(MiddlewareInterface::class);
|
||||||
|
$next->method('handle')->willReturn($envelope);
|
||||||
|
$stack = $this->createMock(StackInterface::class);
|
||||||
|
$stack->method('next')->willReturn($next);
|
||||||
|
|
||||||
|
$middleware = new LockableMessageMiddleware($this->lockFactory);
|
||||||
|
$result = $middleware->handle($envelope, $stack);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Envelope::class, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLockHandle(): void
|
||||||
|
{
|
||||||
|
$store = $this->createMock(PersistingStoreInterface::class);
|
||||||
|
$store->method('save')->willThrowException(new LockConflictedException);
|
||||||
|
$key = new Key(uniqid());
|
||||||
|
$lock = new Lock($key, $store);
|
||||||
|
$this->lockFactory->expects(static::once())->method('createLock')->willReturn($lock);
|
||||||
|
$envelope = new Envelope(new TestMessage(), [new ReceivedStamp('test')]);
|
||||||
|
|
||||||
|
$next = $this->createMock(MiddlewareInterface::class);
|
||||||
|
$next->method('handle')->willReturn($envelope);
|
||||||
|
$stack = $this->createMock(StackInterface::class);
|
||||||
|
$stack->method('next')->willReturn($next);
|
||||||
|
|
||||||
|
$middleware = new LockableMessageMiddleware($this->lockFactory);
|
||||||
|
$result = $middleware->handle($envelope, $stack);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Envelope::class, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNonLockableHandle(): void
|
||||||
|
{
|
||||||
|
$store = $this->createMock(PersistingStoreInterface::class);
|
||||||
|
$store->method('save')->willThrowException(new LockConflictedException);
|
||||||
|
$key = new Key(uniqid());
|
||||||
|
$lock = new Lock($key, $store);
|
||||||
|
$this->lockFactory->expects(static::never())->method('createLock')->willReturn($lock);
|
||||||
|
$envelope = new Envelope(new \stdClass(), [new ReceivedStamp('test')]);
|
||||||
|
|
||||||
|
$next = $this->createMock(MiddlewareInterface::class);
|
||||||
|
$next->method('handle')->willReturn($envelope);
|
||||||
|
$stack = $this->createMock(StackInterface::class);
|
||||||
|
$stack->method('next')->willReturn($next);
|
||||||
|
|
||||||
|
$middleware = new LockableMessageMiddleware($this->lockFactory);
|
||||||
|
$result = $middleware->handle($envelope, $stack);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Envelope::class, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNonReceivedHandle(): void
|
||||||
|
{
|
||||||
|
$store = $this->createMock(PersistingStoreInterface::class);
|
||||||
|
$store->method('save')->willThrowException(new LockConflictedException);
|
||||||
|
$key = new Key(uniqid());
|
||||||
|
$lock = new Lock($key, $store);
|
||||||
|
$this->lockFactory->expects(static::never())->method('createLock')->willReturn($lock);
|
||||||
|
$envelope = new Envelope(new TestMessage());
|
||||||
|
|
||||||
|
$next = $this->createMock(MiddlewareInterface::class);
|
||||||
|
$next->method('handle')->willReturn($envelope);
|
||||||
|
$stack = $this->createMock(StackInterface::class);
|
||||||
|
$stack->method('next')->willReturn($next);
|
||||||
|
|
||||||
|
$middleware = new LockableMessageMiddleware($this->lockFactory);
|
||||||
|
$result = $middleware->handle($envelope, $stack);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Envelope::class, $result);
|
||||||
|
}
|
||||||
|
}
|
@ -20,7 +20,8 @@
|
|||||||
"symfony/console": "^5.2",
|
"symfony/console": "^5.2",
|
||||||
"symfony/messenger": "^5.2",
|
"symfony/messenger": "^5.2",
|
||||||
"symfony/process": "^5.2",
|
"symfony/process": "^5.2",
|
||||||
"symfony/event-dispatcher": "^5.2"
|
"symfony/event-dispatcher": "^5.2",
|
||||||
|
"symfony/lock": "^5.2"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
Loading…
Reference in New Issue
Block a user