Skip to content

[Messenger] [RFC] Correct way to handle random network failure when dispatching message #33794

@tristanbes

Description

@tristanbes

Symfony version(s) affected: 4.3

Description
When calling $this->messageBus->dispatch(); on a loop; if for some reason, there is a connectivity issue, preventing from publishing a message in the queue; then the code stops;

Aws\Sqs\Exception\SqsException
Error executing "SendMessage" on "https://sqs.eu-west-2.amazonaws.com/xyxyxyxyxyxy/my-aweome-queue-name"; AWS HTTP error: cURL error 6: Could not resolve host: sqs.eu-west-2.amazonaws.com (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
Screenshot of the Sentry trace

aws_error

I think it is bad DX, because we don't except dispatch to stop code execution until the code on the production tells you it can happen :).
And after it's difficult to know which messages has been correctly sent to the queue and the one that didn't;

So I wrote this instead:

try {
    $this->messageBus->dispatch(Message::create());
} catch (\Exception $exception) {
    try {
        sleep(1);
        $this->messageBus->dispatch(KeywordMessage::create());
    } catch (\Exception) {
       // do nothing so the code still continue to iterate
    }
}

The code is not particularly beautiful; but allow at least 1 retry in case of some failure;

  1. Should the message bus implement some logic to retry sending the message to the queue ?

  2. should the developper implement and should be encouraged to wrap all calls to dispatch in a try/catch block ? (something similar to what has been done to the Symfony Http Client)

  3. Or should the libraries/adapters underneath do that ?

I'm using:
sroze/messenger-enqueue-transport
enqueue/sqs
symfony/messenger (v4.3.4)

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureMessengerRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions