Symfony:通过电子邮件发送失败的消息

我有一个简单的信使配置:


framework:

    messenger:

        failure_transport: failed

        transports:

            async:

                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'

                retry_strategy:

                    max_retries: 2

                    delay: 10000

            failed: 'doctrine://default?queue_name=failed'


        routing:

            someInterface: async

我想知道路由到失败的传输 fe 系统的每条消息都应该在消息重定向时向我发送电子邮件通知。我唯一的想法是定期检查messenger_messages包含存储在失败传输中的消息的表,但我认为这不是 symfony 方式的解决方案。你知道这个功能更聪明的解决方案吗?


我正在使用版本 5.* 的 symfony/messenger


繁星淼淼
浏览 139回答 2
2回答

慕田峪4524236

最终,我应用了以下解决方案:<?php/**&nbsp;* Author: writ3it&nbsp;*/namespace App\Maintenance\Messenger;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;use Symfony\Component\Messenger\MessageBusInterface;class FailedMessageListener implements EventSubscriberInterface{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @var MessageBusInterface&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private MessageBusInterface $bus;&nbsp; &nbsp; public function __construct(MessageBusInterface $bus)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->bus = $bus;&nbsp; &nbsp; }&nbsp; &nbsp; public function onMessageFailed(WorkerMessageFailedEvent $event)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($event->willRetry()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $envelope = $event->getEnvelope();&nbsp; &nbsp; &nbsp; &nbsp; $message = $envelope->getMessage();&nbsp; &nbsp; &nbsp; &nbsp; $subject = sprintf("Message added to failed transport. Message: %s", \get_class($message));&nbsp; &nbsp; &nbsp; &nbsp; $content = serialize($message);&nbsp; &nbsp; &nbsp; &nbsp; $notification = new FailedMessageNotificationEvent();&nbsp; &nbsp; &nbsp; &nbsp; $notification->setSubject($subject)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setContent($content);&nbsp; &nbsp; &nbsp; &nbsp; $this->bus->dispatch($notification);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @return array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static function getSubscribedEvents()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WorkerMessageFailedEvent::class => ['onMessageFailed', -256],&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; }}FailedMessageNotificationEvent是一条将被我的邮件处理程序捕获的消息。该类FailedMessageListener应标记为kernel.event_subscriber或自动配置。我没有使用中间件,因为 Messenger 直接将消息发送到失败的传输,没有中间件。

守着一只汪

我认为不可能通过配置来完成,但您可以创建自定义中间件并自行发送电子邮件。
打开App,查看更多内容
随时随地看视频慕课网APP