vendor/symfony/messenger/EventListener/SendFailedMessageToFailureTransportListener.php line 39

Open in your IDE?
  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\Messenger\EventListener;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
  15. use Symfony\Component\Messenger\Exception\HandlerFailedException;
  16. use Symfony\Component\Messenger\Stamp\DelayStamp;
  17. use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
  18. use Symfony\Component\Messenger\Stamp\SentToFailureTransportStamp;
  19. use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
  20. /**
  21.  * Sends a rejected message to a "failure transport".
  22.  *
  23.  * @author Ryan Weaver <ryan@symfonycasts.com>
  24.  */
  25. class SendFailedMessageToFailureTransportListener implements EventSubscriberInterface
  26. {
  27.     private $failureSender;
  28.     private $logger;
  29.     public function __construct(SenderInterface $failureSenderLoggerInterface $logger null)
  30.     {
  31.         $this->failureSender $failureSender;
  32.         $this->logger $logger;
  33.     }
  34.     public function onMessageFailed(WorkerMessageFailedEvent $event)
  35.     {
  36.         if ($event->willRetry()) {
  37.             return;
  38.         }
  39.         $envelope $event->getEnvelope();
  40.         // avoid re-sending to the failed sender
  41.         if (null !== $envelope->last(SentToFailureTransportStamp::class)) {
  42.             return;
  43.         }
  44.         $throwable $event->getThrowable();
  45.         if ($throwable instanceof HandlerFailedException) {
  46.             $throwable $throwable->getNestedExceptions()[0];
  47.         }
  48.         $flattenedException class_exists(FlattenException::class) ? FlattenException::createFromThrowable($throwable) : null;
  49.         $envelope $envelope->with(
  50.             new SentToFailureTransportStamp($event->getReceiverName()),
  51.             new DelayStamp(0),
  52.             new RedeliveryStamp(0$throwable->getMessage(), $flattenedException)
  53.         );
  54.         if (null !== $this->logger) {
  55.             $this->logger->info('Rejected message {class} will be sent to the failure transport {transport}.', [
  56.                 'class' => \get_class($envelope->getMessage()),
  57.                 'transport' => \get_class($this->failureSender),
  58.             ]);
  59.         }
  60.         $this->failureSender->send($envelope);
  61.     }
  62.     public static function getSubscribedEvents()
  63.     {
  64.         return [
  65.             WorkerMessageFailedEvent::class => ['onMessageFailed', -100],
  66.         ];
  67.     }
  68. }