messenger.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
  11. use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory;
  12. use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpTransportFactory;
  13. use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdTransportFactory;
  14. use Symfony\Component\Messenger\Bridge\Redis\Transport\RedisTransportFactory;
  15. use Symfony\Component\Messenger\EventListener\AddErrorDetailsStampListener;
  16. use Symfony\Component\Messenger\EventListener\DispatchPcntlSignalListener;
  17. use Symfony\Component\Messenger\EventListener\SendFailedMessageForRetryListener;
  18. use Symfony\Component\Messenger\EventListener\SendFailedMessageToFailureTransportListener;
  19. use Symfony\Component\Messenger\EventListener\StopWorkerOnRestartSignalListener;
  20. use Symfony\Component\Messenger\EventListener\StopWorkerOnSigtermSignalListener;
  21. use Symfony\Component\Messenger\Middleware\AddBusNameStampMiddleware;
  22. use Symfony\Component\Messenger\Middleware\DispatchAfterCurrentBusMiddleware;
  23. use Symfony\Component\Messenger\Middleware\FailedMessageProcessingMiddleware;
  24. use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware;
  25. use Symfony\Component\Messenger\Middleware\RejectRedeliveredMessageMiddleware;
  26. use Symfony\Component\Messenger\Middleware\SendMessageMiddleware;
  27. use Symfony\Component\Messenger\Middleware\TraceableMiddleware;
  28. use Symfony\Component\Messenger\Middleware\ValidationMiddleware;
  29. use Symfony\Component\Messenger\Retry\MultiplierRetryStrategy;
  30. use Symfony\Component\Messenger\RoutableMessageBus;
  31. use Symfony\Component\Messenger\Transport\InMemoryTransportFactory;
  32. use Symfony\Component\Messenger\Transport\Sender\SendersLocator;
  33. use Symfony\Component\Messenger\Transport\Serialization\Normalizer\FlattenExceptionNormalizer;
  34. use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
  35. use Symfony\Component\Messenger\Transport\Serialization\Serializer;
  36. use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
  37. use Symfony\Component\Messenger\Transport\Sync\SyncTransportFactory;
  38. use Symfony\Component\Messenger\Transport\TransportFactory;
  39. return static function (ContainerConfigurator $container) {
  40. $container->services()
  41. ->alias(SerializerInterface::class, 'messenger.default_serializer')
  42. // Asynchronous
  43. ->set('messenger.senders_locator', SendersLocator::class)
  44. ->args([
  45. abstract_arg('per message senders map'),
  46. abstract_arg('senders service locator'),
  47. ])
  48. ->set('messenger.middleware.send_message', SendMessageMiddleware::class)
  49. ->args([
  50. service('messenger.senders_locator'),
  51. service('event_dispatcher'),
  52. ])
  53. ->call('setLogger', [service('logger')->ignoreOnInvalid()])
  54. ->tag('monolog.logger', ['channel' => 'messenger'])
  55. // Message encoding/decoding
  56. ->set('messenger.transport.symfony_serializer', Serializer::class)
  57. ->args([
  58. service('serializer'),
  59. abstract_arg('format'),
  60. abstract_arg('context'),
  61. ])
  62. ->set('serializer.normalizer.flatten_exception', FlattenExceptionNormalizer::class)
  63. ->tag('serializer.normalizer', ['priority' => -880])
  64. ->set('messenger.transport.native_php_serializer', PhpSerializer::class)
  65. // Middleware
  66. ->set('messenger.middleware.handle_message', HandleMessageMiddleware::class)
  67. ->abstract()
  68. ->args([
  69. abstract_arg('bus handler resolver'),
  70. ])
  71. ->tag('monolog.logger', ['channel' => 'messenger'])
  72. ->call('setLogger', [service('logger')->ignoreOnInvalid()])
  73. ->set('messenger.middleware.add_bus_name_stamp_middleware', AddBusNameStampMiddleware::class)
  74. ->abstract()
  75. ->set('messenger.middleware.dispatch_after_current_bus', DispatchAfterCurrentBusMiddleware::class)
  76. ->set('messenger.middleware.validation', ValidationMiddleware::class)
  77. ->args([
  78. service('validator'),
  79. ])
  80. ->set('messenger.middleware.reject_redelivered_message_middleware', RejectRedeliveredMessageMiddleware::class)
  81. ->set('messenger.middleware.failed_message_processing_middleware', FailedMessageProcessingMiddleware::class)
  82. ->set('messenger.middleware.traceable', TraceableMiddleware::class)
  83. ->abstract()
  84. ->args([
  85. service('debug.stopwatch'),
  86. ])
  87. // Discovery
  88. ->set('messenger.receiver_locator')
  89. ->args([
  90. [],
  91. ])
  92. ->tag('container.service_locator')
  93. // Transports
  94. ->set('messenger.transport_factory', TransportFactory::class)
  95. ->args([
  96. tagged_iterator('messenger.transport_factory'),
  97. ])
  98. ->set('messenger.transport.amqp.factory', AmqpTransportFactory::class)
  99. ->set('messenger.transport.redis.factory', RedisTransportFactory::class)
  100. ->set('messenger.transport.sync.factory', SyncTransportFactory::class)
  101. ->args([
  102. service('messenger.routable_message_bus'),
  103. ])
  104. ->tag('messenger.transport_factory')
  105. ->set('messenger.transport.in_memory.factory', InMemoryTransportFactory::class)
  106. ->tag('messenger.transport_factory')
  107. ->tag('kernel.reset', ['method' => 'reset'])
  108. ->set('messenger.transport.sqs.factory', AmazonSqsTransportFactory::class)
  109. ->set('messenger.transport.beanstalkd.factory', BeanstalkdTransportFactory::class)
  110. // retry
  111. ->set('messenger.retry_strategy_locator')
  112. ->args([
  113. [],
  114. ])
  115. ->tag('container.service_locator')
  116. ->set('messenger.retry.abstract_multiplier_retry_strategy', MultiplierRetryStrategy::class)
  117. ->abstract()
  118. ->args([
  119. abstract_arg('max retries'),
  120. abstract_arg('delay ms'),
  121. abstract_arg('multiplier'),
  122. abstract_arg('max delay ms'),
  123. ])
  124. // worker event listener
  125. ->set('messenger.retry.send_failed_message_for_retry_listener', SendFailedMessageForRetryListener::class)
  126. ->args([
  127. abstract_arg('senders service locator'),
  128. service('messenger.retry_strategy_locator'),
  129. service('logger')->ignoreOnInvalid(),
  130. service('event_dispatcher'),
  131. ])
  132. ->tag('kernel.event_subscriber')
  133. ->tag('monolog.logger', ['channel' => 'messenger'])
  134. ->set('messenger.failure.add_error_details_stamp_listener', AddErrorDetailsStampListener::class)
  135. ->tag('kernel.event_subscriber')
  136. ->set('messenger.failure.send_failed_message_to_failure_transport_listener', SendFailedMessageToFailureTransportListener::class)
  137. ->args([
  138. abstract_arg('failure transport'),
  139. service('logger')->ignoreOnInvalid(),
  140. ])
  141. ->tag('kernel.event_subscriber')
  142. ->tag('monolog.logger', ['channel' => 'messenger'])
  143. ->set('messenger.listener.dispatch_pcntl_signal_listener', DispatchPcntlSignalListener::class)
  144. ->tag('kernel.event_subscriber')
  145. ->set('messenger.listener.stop_worker_on_restart_signal_listener', StopWorkerOnRestartSignalListener::class)
  146. ->args([
  147. service('cache.messenger.restart_workers_signal'),
  148. service('logger')->ignoreOnInvalid(),
  149. ])
  150. ->tag('kernel.event_subscriber')
  151. ->tag('monolog.logger', ['channel' => 'messenger'])
  152. ->set('messenger.listener.stop_worker_on_sigterm_signal_listener', StopWorkerOnSigtermSignalListener::class)
  153. ->tag('kernel.event_subscriber')
  154. ->set('messenger.routable_message_bus', RoutableMessageBus::class)
  155. ->args([
  156. abstract_arg('message bus locator'),
  157. service('messenger.default_bus'),
  158. ])
  159. ;
  160. };