symfony/docker | save in db email sent with messenger

169 views Asked by At

in a symfony project with docker environment, I use the Messenger component, I configured it, the emails are sent via messenger, nice.

When emails are sent they disappear from the "messenger_message" table (the table linked to messenger).

To send emails I use the messenger:consume command, but when an email is sent it disappears from the table, so I created a subscriber to save the sent emails in another table, the subscriber works when I type the command messenger:consume. But in my case, this command is already executed automatically in my docker messenger container, and at this moment my subscriber no longer works... I looked at the logs, nothing alarming, everything looks good, but my emails are not saving at all.

Have you ever had this problem ?

my subscriber :

class MessengerMessageConsumedSubscriber implements EventSubscriberInterface
{
    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public static function getSubscribedEvents()
    {
        return [
            WorkerMessageHandledEvent::class => 'onEmailSent',
        ];
    }

    public function onEmailSent(WorkerMessageHandledEvent $event)
    {
        $message = $event->getEnvelope()->getMessage();

        $emailSent = new MessengerMessagesSent();
        $emailSent->setFromEmail($message->getMessage()->getHeaders()->get('from')->getBody()[0]->getAddress());
        $emailSent->setToEmail($message->getMessage()->getHeaders()->get('to')->getBody()[0]->getAddress());
        $emailSent->setSubject($message->getMessage()->getHeaders()->get('subject')->getValue());

        $this->entityManager->persist($emailSent);
        $this->entityManager->flush();
    }
}

my docker command executed in the messenger container:

'sh -c "php-fpm & php bin/console messenger:consume async -vv --time-limit=3600 --failure-limit=10"'

And my messenger.yaml:

        transports:
        # https://symfony.com/doc/current/messenger.html#transport-configuration
        async:
            dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
            options:
                use_notify: true
                check_delayed_interval: 60000
            retry_strategy:
                max_retries: 3
                multiplier: 2
                delay: 30000
        failed: 'doctrine://default?queue_name=failed'
        sent: 'doctrine://default?queue_name=sent'
1

There are 1 answers

0
Leroy On

Are you sure your code is updated in docker? When altering php code, the code is not automatically updated when running a php daemon like messenger:consume. You have to manually restart your consumer for new code to be applied, since it has been loaded in memory.

You also should consider running your consumer in another container instead. You are now starting php-fpm in the background and start the consumer. Your docker daemon can not recover from a php-fpm crash this way.

Another suggestion is to prevent a messenger chain without using the builtin Bus chain. It will not improve your code. In most cases you don't need to publish new async messages from other async messages.

I manage a piece of software at our company that uses this bad design, and it's a nightmare!