2021-04-16 13:37:47 +03:00
|
|
|
<?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;
|
|
|
|
|
2023-10-18 14:15:32 +03:00
|
|
|
/**
|
|
|
|
* @var int|null
|
|
|
|
*/
|
|
|
|
private $ttl;
|
|
|
|
|
|
|
|
public function __construct(LockFactory $lockFactory, int $ttl = null)
|
2021-04-16 13:37:47 +03:00
|
|
|
{
|
|
|
|
$this->lockFactory = $lockFactory;
|
2023-10-18 14:15:32 +03:00
|
|
|
$this->ttl = $ttl;
|
2021-04-16 13:37:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) {
|
2023-10-18 14:15:32 +03:00
|
|
|
$lock = $this->lockFactory->createLock($this->objectHash($message), $this->ttl);
|
2021-04-16 13:37:47 +03:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|