-
Notifications
You must be signed in to change notification settings - Fork 8
add instant retry command bus #751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,22 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Patchlevel\EventSourcing\Attribute; | ||
|
|
||
| use Attribute; | ||
| use Throwable; | ||
|
|
||
| #[Attribute(Attribute::TARGET_CLASS)] | ||
| final class InstantRetry | ||
| { | ||
| /** | ||
| * @param positive-int|null $maxRetries | ||
| * @param list<class-string<Throwable>>|null $exceptions | ||
| */ | ||
| public function __construct( | ||
| public readonly int|null $maxRetries = null, | ||
| public readonly array|null $exceptions = null, | ||
| ) { | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,65 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Patchlevel\EventSourcing\CommandBus; | ||
|
|
||
| use Patchlevel\EventSourcing\Attribute\InstantRetry; | ||
| use Patchlevel\EventSourcing\Repository\AggregateOutdated; | ||
| use ReflectionClass; | ||
| use Throwable; | ||
|
|
||
| use function in_array; | ||
|
|
||
| final class InstantRetryCommandBus implements CommandBus | ||
| { | ||
| /** | ||
| * @param positive-int $defaultMaxRetries | ||
| * @param list<class-string<Throwable>> $defaultExceptions | ||
| */ | ||
| public function __construct( | ||
| private readonly CommandBus $commandBus, | ||
| private readonly int $defaultMaxRetries = 3, | ||
DavidBadura marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private readonly array $defaultExceptions = [AggregateOutdated::class], | ||
| ) { | ||
| } | ||
|
|
||
| public function dispatch(object $command): void | ||
| { | ||
| $this->doDispatch($command, 0); | ||
| } | ||
|
|
||
| private function doDispatch(object $command, int $retry): void | ||
| { | ||
| try { | ||
| $this->commandBus->dispatch($command); | ||
| } catch (Throwable $exception) { | ||
| $configuration = $this->configuration($command); | ||
|
|
||
| if ($configuration === null) { | ||
| throw $exception; | ||
| } | ||
|
|
||
| $exceptions = $configuration->exceptions ?? $this->defaultExceptions; | ||
| $maxRetries = $configuration->maxRetries ?? $this->defaultMaxRetries; | ||
|
|
||
| if ($retry >= $maxRetries || !in_array($exception::class, $exceptions, true)) { | ||
DavidBadura marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw $exception; | ||
| } | ||
|
|
||
| $this->doDispatch($command, $retry + 1); | ||
| } | ||
| } | ||
|
|
||
| private function configuration(object $command): InstantRetry|null | ||
| { | ||
| $reflectionClass = new ReflectionClass($command); | ||
| $attributes = $reflectionClass->getAttributes(InstantRetry::class); | ||
|
|
||
| if ($attributes === []) { | ||
| return null; | ||
| } | ||
|
|
||
| return $attributes[0]->newInstance(); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,198 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Patchlevel\EventSourcing\Tests\Unit\CommandBus; | ||
|
|
||
| use Patchlevel\EventSourcing\Attribute\InstantRetry; | ||
| use Patchlevel\EventSourcing\CommandBus\CommandBus; | ||
| use Patchlevel\EventSourcing\CommandBus\InstantRetryCommandBus; | ||
| use Patchlevel\EventSourcing\Repository\AggregateOutdated; | ||
| use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId; | ||
| use PHPUnit\Framework\TestCase; | ||
| use RuntimeException; | ||
|
|
||
| final class InstantRetryCommandBusTest extends TestCase | ||
| { | ||
| public function testSuccess(): void | ||
| { | ||
| $command = new #[InstantRetry] | ||
| class { | ||
| public function __construct() | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $innerCommandBus = $this->createMock(CommandBus::class); | ||
| $innerCommandBus | ||
| ->expects($this->once()) | ||
| ->method('dispatch') | ||
| ->with($command); | ||
|
|
||
| $retryCommandBus = new InstantRetryCommandBus($innerCommandBus); | ||
|
|
||
| $retryCommandBus->dispatch($command); | ||
|
|
||
| $this->assertTrue(true); // If no exception is thrown, the test passes | ||
| } | ||
|
|
||
| public function testDispatchRetriesUntilSuccess(): void | ||
| { | ||
| $command = new #[InstantRetry] | ||
| class { | ||
| public function __construct() | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $innerCommandBus = $this->createMock(CommandBus::class); | ||
| $innerCommandBus | ||
| ->expects($this->exactly(3)) | ||
| ->method('dispatch') | ||
| ->with($command) | ||
| ->willReturnOnConsecutiveCalls( | ||
| $this->throwException(new AggregateOutdated('profile', ProfileId::fromString('profile'))), | ||
| $this->throwException(new AggregateOutdated('profile', ProfileId::fromString('profile'))), | ||
| null, // Success on the third attempt | ||
| ); | ||
|
|
||
| $retryCommandBus = new InstantRetryCommandBus($innerCommandBus); | ||
|
|
||
| $retryCommandBus->dispatch($command); | ||
|
|
||
| $this->assertTrue(true); // If no exception is thrown, the test passes | ||
| } | ||
|
|
||
| public function testDispatchThrowsAfterMaxRetries(): void | ||
| { | ||
| $command = new #[InstantRetry] | ||
| class { | ||
| public function __construct() | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $innerCommandBus = $this->createMock(CommandBus::class); | ||
| $innerCommandBus | ||
| ->expects($this->exactly(4)) | ||
| ->method('dispatch') | ||
| ->with($command) | ||
| ->willThrowException( | ||
| new AggregateOutdated( | ||
| 'profile', | ||
| ProfileId::fromString('profile'), | ||
| ), | ||
| ); | ||
|
|
||
| $retryCommandBus = new InstantRetryCommandBus($innerCommandBus); | ||
|
|
||
| $this->expectException(AggregateOutdated::class); | ||
|
|
||
| $retryCommandBus->dispatch($command); | ||
| } | ||
|
|
||
| public function testDispatchThrowsAfterMaxRetriesWithOverride(): void | ||
| { | ||
| $command = new #[InstantRetry(maxRetries: 2)] | ||
DavidBadura marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| class { | ||
| public function __construct() | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $innerCommandBus = $this->createMock(CommandBus::class); | ||
| $innerCommandBus | ||
| ->expects($this->exactly(3)) | ||
| ->method('dispatch') | ||
| ->with($command) | ||
| ->willThrowException( | ||
| new AggregateOutdated( | ||
| 'profile', | ||
| ProfileId::fromString('profile'), | ||
| ), | ||
| ); | ||
|
|
||
| $retryCommandBus = new InstantRetryCommandBus($innerCommandBus); | ||
|
|
||
| $this->expectException(AggregateOutdated::class); | ||
|
|
||
| $retryCommandBus->dispatch($command); | ||
| } | ||
|
|
||
| public function testDispatchNotRetry(): void | ||
| { | ||
| $command = new class { | ||
| public function __construct() | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $innerCommandBus = $this->createMock(CommandBus::class); | ||
| $innerCommandBus | ||
| ->expects($this->once()) | ||
| ->method('dispatch') | ||
| ->with($command) | ||
| ->willThrowException( | ||
| new AggregateOutdated( | ||
| 'profile', | ||
| ProfileId::fromString('profile'), | ||
| ), | ||
| ); | ||
|
|
||
| $retryCommandBus = new InstantRetryCommandBus($innerCommandBus); | ||
|
|
||
| $this->expectException(AggregateOutdated::class); | ||
|
|
||
| $retryCommandBus->dispatch($command); | ||
| } | ||
|
|
||
| public function testSkipOtherExceptions(): void | ||
| { | ||
| $command = new #[InstantRetry(maxRetries: 2)] | ||
| class { | ||
| public function __construct() | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $innerCommandBus = $this->createMock(CommandBus::class); | ||
| $innerCommandBus | ||
| ->expects($this->once()) | ||
| ->method('dispatch') | ||
| ->with($command) | ||
| ->willThrowException(new RuntimeException('Some other exception')); | ||
|
|
||
| $retryCommandBus = new InstantRetryCommandBus($innerCommandBus); | ||
|
|
||
| $this->expectException(RuntimeException::class); | ||
|
|
||
| $retryCommandBus->dispatch($command); | ||
| } | ||
|
|
||
| public function testOverrideException(): void | ||
| { | ||
| $command = new #[InstantRetry(exceptions: [RuntimeException::class])] | ||
| class { | ||
| public function __construct() | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $innerCommandBus = $this->createMock(CommandBus::class); | ||
| $innerCommandBus | ||
| ->expects($this->exactly(3)) | ||
| ->method('dispatch') | ||
| ->with($command) | ||
| ->willReturnOnConsecutiveCalls( | ||
| $this->throwException(new RuntimeException()), | ||
| $this->throwException(new RuntimeException()), | ||
| null, // Success on the third attempt | ||
| ); | ||
|
|
||
| $retryCommandBus = new InstantRetryCommandBus($innerCommandBus); | ||
|
|
||
| $retryCommandBus->dispatch($command); | ||
|
|
||
| $this->assertTrue(true); // If no exception is thrown, the test passes | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.