-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EventDispatcherFactory with default Retry Strategy (#81)
* Add EventDispatcherFactory with default Retry Strategy * Add EventDispatcherFactory with default Retry Strategy * Code Styles * PHPCs fixes --------- Co-authored-by: Thomas Eimers <[email protected]>
- Loading branch information
Showing
3 changed files
with
84 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Xtreamwayz\PsrContainerMessenger\Event; | ||
|
||
use Laminas\ServiceManager\Factory\DelegatorFactoryInterface; | ||
use Laminas\ServiceManager\ServiceLocatorInterface; | ||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\Messenger\EventListener\SendFailedMessageForRetryListener; | ||
use Symfony\Component\Messenger\EventListener\SendFailedMessageToFailureTransportListener; | ||
use Symfony\Component\Messenger\Retry\MultiplierRetryStrategy; | ||
|
||
class EventDispatcherDelegatorFactory implements DelegatorFactoryInterface | ||
{ | ||
protected const MAX_RETRIES = 3; | ||
protected const WAIT_IN_SECONDS = 5 * 60; | ||
protected const WAIT_MULTIPLIER = 4; | ||
public const FAILED_QUEUE = 'failed'; | ||
|
||
/** | ||
* @param string $name | ||
*/ | ||
public function __invoke( | ||
ContainerInterface $container, | ||
$name, | ||
callable $callback, | ||
?array $options = null | ||
): EventDispatcherInterface { | ||
$config = $container->get('config'); | ||
$availableRoutes = []; | ||
foreach ($config['messenger']['buses']['messenger.command.bus']['routes'] as $routes) { | ||
foreach ($routes as $route) { | ||
if ($route === self::FAILED_QUEUE) { | ||
continue; | ||
} | ||
$availableRoutes[$route] = $route; | ||
} | ||
} | ||
|
||
/** @var EventDispatcherInterface $eventDispatcher */ | ||
$eventDispatcher = $callback(); | ||
|
||
$strategieContainer = new ContainerBuilder(); | ||
$failureContainer = new ContainerBuilder(); | ||
|
||
$logger = $container->get($config['messenger']['logger']); | ||
|
||
$eventDispatcher->addSubscriber( | ||
new SendFailedMessageForRetryListener($container, $strategieContainer, $logger) | ||
); | ||
|
||
foreach ($availableRoutes as $availableRoute) { | ||
$strategieContainer->set( | ||
$availableRoute, | ||
new MultiplierRetryStrategy(self::MAX_RETRIES, self::WAIT_IN_SECONDS * 1000, self::WAIT_MULTIPLIER) | ||
); | ||
$failureContainer->set($availableRoute, $container->get(self::FAILED_QUEUE)); | ||
} | ||
|
||
$eventDispatcher->addSubscriber( | ||
new SendFailedMessageToFailureTransportListener($failureContainer, $logger) | ||
); | ||
return $eventDispatcher; | ||
} | ||
|
||
public function createDelegatorWithName( | ||
ServiceLocatorInterface $serviceLocator, | ||
string $name, | ||
string $requestedName, | ||
callable $callback | ||
): EventDispatcherInterface { | ||
return $this($serviceLocator, $name, $callback); | ||
} | ||
} |