|
97 | 97 | use Patchlevel\EventSourcing\Subscription\Engine\ThrowOnErrorSubscriptionEngine; |
98 | 98 | use Patchlevel\EventSourcing\Subscription\Repository\RunSubscriptionEngineRepositoryManager; |
99 | 99 | use Patchlevel\EventSourcing\Subscription\RetryStrategy\ClockBasedRetryStrategy; |
| 100 | +use Patchlevel\EventSourcing\Subscription\RetryStrategy\NoRetryStrategy; |
100 | 101 | use Patchlevel\EventSourcing\Subscription\RetryStrategy\RetryStrategy; |
| 102 | +use Patchlevel\EventSourcing\Subscription\RetryStrategy\RetryStrategyRepository; |
101 | 103 | use Patchlevel\EventSourcing\Subscription\Store\DoctrineSubscriptionStore; |
102 | 104 | use Patchlevel\EventSourcing\Subscription\Store\InMemorySubscriptionStore; |
103 | 105 | use Patchlevel\EventSourcing\Subscription\Store\SubscriptionStore; |
@@ -335,16 +337,73 @@ static function (ChildDefinition $definition): void { |
335 | 337 | $container->register(AttributeSubscriberMetadataFactory::class); |
336 | 338 | $container->setAlias(SubscriberMetadataFactory::class, AttributeSubscriberMetadataFactory::class); |
337 | 339 |
|
338 | | - $container->register(ClockBasedRetryStrategy::class) |
| 340 | + $strategies = []; |
| 341 | + |
| 342 | + $retryStrategy = $config['subscription']['retry_strategy'] ?? null; |
| 343 | + |
| 344 | + if ($retryStrategy) { |
| 345 | + $container->register(ClockBasedRetryStrategy::class) |
| 346 | + ->setArguments([ |
| 347 | + new Reference('event_sourcing.clock'), |
| 348 | + $retryStrategy['base_delay'], |
| 349 | + $retryStrategy['delay_factor'], |
| 350 | + $retryStrategy['max_attempts'], |
| 351 | + ]); |
| 352 | + |
| 353 | + $container->register(NoRetryStrategy::class); |
| 354 | + |
| 355 | + $container |
| 356 | + ->setAlias(RetryStrategy::class, ClockBasedRetryStrategy::class) |
| 357 | + ->setDeprecated( |
| 358 | + 'patchlevel/event-sourcing-bundle', |
| 359 | + '3.10', |
| 360 | + 'The "%alias_id%" alias is deprecated, use "RetryStrategyRepository" instead.', |
| 361 | + ); |
| 362 | + |
| 363 | + $strategies['default'] = new Reference(RetryStrategy::class); |
| 364 | + $strategies['no_retry'] = new Reference(NoRetryStrategy::class); |
| 365 | + } else { |
| 366 | + foreach ($config['subscription']['retry_strategies'] as $name => $strategyConfig) { |
| 367 | + if ($strategyConfig['type'] === 'custom') { |
| 368 | + $strategies[$name] = new Reference($strategyConfig['service']); |
| 369 | + |
| 370 | + continue; |
| 371 | + } |
| 372 | + |
| 373 | + $id = 'event_sourcing.subscription.retry_strategy.' . $name; |
| 374 | + |
| 375 | + if ($strategyConfig['type'] === 'clock_based') { |
| 376 | + $container->register($id, ClockBasedRetryStrategy::class) |
| 377 | + ->setArguments([ |
| 378 | + new Reference('event_sourcing.clock'), |
| 379 | + $strategyConfig['options']['base_delay'] ?? 5, |
| 380 | + $strategyConfig['options']['delay_factor'] ?? 2, |
| 381 | + $strategyConfig['options']['max_attempts'] ?? 5, |
| 382 | + ]); |
| 383 | + |
| 384 | + $strategies[$name] = new Reference($id); |
| 385 | + |
| 386 | + continue; |
| 387 | + } |
| 388 | + |
| 389 | + if ($strategyConfig['type'] === 'no_retry') { |
| 390 | + $container->register($id, NoRetryStrategy::class); |
| 391 | + |
| 392 | + $strategies[$name] = new Reference($id); |
| 393 | + |
| 394 | + continue; |
| 395 | + } |
| 396 | + |
| 397 | + throw new InvalidArgumentException(sprintf('Unknown retry strategy type "%s"', $strategyConfig['type'])); |
| 398 | + } |
| 399 | + } |
| 400 | + |
| 401 | + $container->register(RetryStrategyRepository::class) |
339 | 402 | ->setArguments([ |
340 | | - new Reference('event_sourcing.clock'), |
341 | | - $config['subscription']['retry_strategy']['base_delay'], |
342 | | - $config['subscription']['retry_strategy']['delay_factor'], |
343 | | - $config['subscription']['retry_strategy']['max_attempts'], |
| 403 | + $strategies, |
| 404 | + $config['subscription']['default_retry_strategy'], |
344 | 405 | ]); |
345 | 406 |
|
346 | | - $container->setAlias(RetryStrategy::class, ClockBasedRetryStrategy::class); |
347 | | - |
348 | 407 | $container->register(SubscriberHelper::class) |
349 | 408 | ->setArguments([new Reference(SubscriberMetadataFactory::class)]); |
350 | 409 |
|
@@ -395,7 +454,7 @@ static function (ChildDefinition $definition): void { |
395 | 454 | new Reference(Store::class), |
396 | 455 | new Reference(SubscriptionStore::class), |
397 | 456 | new Reference(SubscriberAccessorRepository::class), |
398 | | - new Reference(RetryStrategy::class), |
| 457 | + new Reference(RetryStrategyRepository::class), |
399 | 458 | new Reference('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE), |
400 | 459 | ]) |
401 | 460 | ->addTag('monolog.logger', ['channel' => 'event_sourcing']); |
|
0 commit comments