-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add LockMiddleware * Fix phpstan and code coverage * Fix php-cs * Fix rector and php-cs-fixer * Remove readonly * Fix PHP 8.0 compatibility
- Loading branch information
1 parent
73da6f6
commit b4450d1
Showing
17 changed files
with
320 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Infrastructure/Symfony/Messenger/LockMiddleware/LockMiddleware.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sulu\Messenger\Infrastructure\Symfony\Messenger\LockMiddleware; | ||
|
||
use Symfony\Component\Lock\LockFactory; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; | ||
use Symfony\Component\Messenger\Middleware\StackInterface; | ||
|
||
class LockMiddleware implements MiddlewareInterface | ||
{ | ||
public function __construct( | ||
private LockFactory $lockFactory, | ||
) { | ||
} | ||
|
||
public function handle(Envelope $envelope, StackInterface $stack): Envelope | ||
{ | ||
$locks = []; | ||
|
||
try { | ||
/** @var LockStamp[] $lockStamps */ | ||
$lockStamps = $envelope->all(LockStamp::class); | ||
|
||
foreach ($lockStamps as $lockStamp) { | ||
$lock = $this->lockFactory->createLock( | ||
$lockStamp->getResource(), | ||
$lockStamp->getTtl(), | ||
$lockStamp->getAutoRelease(), | ||
); | ||
|
||
$lock->acquire(true); | ||
$locks[] = $lock; | ||
} | ||
|
||
$envelope = $stack->next()->handle($envelope, $stack); | ||
} finally { | ||
foreach ($locks as $lock) { | ||
$lock->release(); | ||
} | ||
} | ||
|
||
return $envelope; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Infrastructure/Symfony/Messenger/LockMiddleware/LockStamp.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sulu\Messenger\Infrastructure\Symfony\Messenger\LockMiddleware; | ||
|
||
use Symfony\Component\Messenger\Stamp\StampInterface; | ||
|
||
/** | ||
* Stamp to lock a process handling to avoid race conditions. | ||
*/ | ||
class LockStamp implements StampInterface | ||
{ | ||
public function __construct( | ||
private string $resource, | ||
private ?float $ttl = 300.0, | ||
private bool $autoRelease = true, | ||
) { | ||
} | ||
|
||
public function getResource(): string | ||
{ | ||
return $this->resource; | ||
} | ||
|
||
public function getTtl(): ?float | ||
{ | ||
return $this->ttl; | ||
} | ||
|
||
public function getAutoRelease(): bool | ||
{ | ||
return $this->autoRelease; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.