How to tell Rabbit to that task fails in .Net Core

Viewed 63

Let's imagine that we have Q named "NotificationQ" and have a consumer who gets a task from that Q and sends emails to customers.

Emailing process sends an email by API from mailgun. That API request does not turn 200 every time(the reason is not important). In that time I need to tell RabbitMQ that tasks fail. I know there is a feature called autoAck but if a request fails how the RabbitMQ client pack understood that a fail.

Am I manually trigger ack to say that request failed?

I using https://www.nuget.org/packages/RabbitMQ.Client/ pack to handle RabbitMQ tasks.

            var channel = RabbitPrepareFactory.GetConnectionFactory();

            channel.BasicQos(0, 1, false);

            var notificationPack = channel.BasicGet("notification", true);


            var message = System.Text.Encoding.UTF8.GetString(notificationPack.Body.ToArray());


            var task = JsonConvert.DeserializeObject<ForgetPasswordEmailNotification>(message);


            var isEmailSendSuccessful = SomeFakeEmailSendFunctions(task.Email);


            if (!isEmailSendSuccessful)
            {
                //something for tell RabbitMQ that task fail and not delte that task in q
                .......
            }
1 Answers

I think this could be usefull. I would use something like a Dead Letter https://www.rabbitmq.com/dlx.html

So everytime a message is failing for whatever reason, you push the message to that queue. Once your messaged was recieved by your consumer and the scope of the operation finished, that message is acknowledged so that other consumers will not take a already processed message.

[Edit] I dont't think its a good ideea to process a message from a queue and afterwards to leave it there if something happens to your BackEnd. If you implement the dead letter queue you could try to reprocess those messages at some time ( Maybe a CronJob ) or if you really don't wanna have dead letter queues you could try to implement in your Client a Retry Mechanism. Polly could work very well in your case https://github.com/App-vNext/Polly

Related