Michal S.
Michal S.

Reputation: 470

Using senders independent in Symfony messenger component

I'm using Symfony 4.2 and have one message to dispatch via messenger component which is a notification that should be sent via a few channels (for example SMS and email). I'm wondering how to make these senders independent (for example first channel fails and throw an exception) - how to make a try to send independent via the second sender? Currently, when one of the senders in the chain fails the rest can't make a try of delivering notification.

Catching exception on the sender level seems not to be a good solution, because returning envelop causes that it will be stamped as sent what is not true.

I've started to make message per channel to keep sentStamp convention, but It seems that should be one message and few channels listening for one message (even configuration indicates to that with senders keyword):

routing:
        'App\Messenger\Command\Notification\SendSomeInformation':
            senders:
                - App\Messenger\Sender\Notification\EmailSender
                - App\Messenger\Sender\Notification\SmsSender

There is some good approach for such problem?

Upvotes: 1

Views: 763

Answers (1)

msg
msg

Reputation: 8181

One possibility would be to configure two different transports, and assign each handler to different transports, so if one of them fails and dequeues the message, the other can still have a chance to run.

# config/packages/messenger.yaml
transports:
    async1:
        # dsn
    async2:
        # dsn
...
routing:
    'App\Messenger\Command\Notification\SendSomeInformation': [async1, async2]

Restricting handlers to transports can be done either in code or config, choose what works better for you.

In config:

# config/services.yaml
App\Messenger\Sender\Notification\SmsSender:
    tags: 
        - { name: 'messenger.message_handler', from_transport: 'async1'}

Upvotes: 1

Related Questions