Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Host.RabbitMQ] Automatically ack messages #214

Closed
robert94p opened this issue Feb 22, 2024 · 8 comments
Closed

[Host.RabbitMQ] Automatically ack messages #214

robert94p opened this issue Feb 22, 2024 · 8 comments
Assignees
Milestone

Comments

@robert94p
Copy link

How to ack messages automatically? so that a message started for processing is not processed again when the service is restarted

@zarusz zarusz self-assigned this Feb 22, 2024
@zarusz
Copy link
Owner

zarusz commented Feb 22, 2024

So if I understand, you'd like to Ack the message right away upon arrival (before even processing starts in case the service process dies)? This will lead to at-most-once delivery guarantees.

There is no direct way to make it happen nowadays, there is something similar that lets the user decide if to Ack or Nack in case of an error (see here).

What I could propose is to 1) add a custom interceptor and 2) a way to Ack imperatively in that interceptor before message processing. The 2) will require a new feature which I could add here quite soon. The interceptor example is below:

public class AckConsumerInterceptor<TMessage> : IConsumerInterceptor<TMessage>
{
   public async Task<object> OnHandle(TMessage message, Func<Task<object>> next, IConsumerContext context) 
   {
      context.Ack(); // ack before message processing starts (at-most-once)
      return await next(); // process the message
   }
}

Let me know if that works.

@robert94p
Copy link
Author

Thank you, works

@robert94p
Copy link
Author

Сall context.Ack() in consumer handler throws an error
System.Collections.Generic.KeyNotFoundException: The given key 'RabbitMq_MessageConfirmAction' was not present in the dictionary.

@zarusz
Copy link
Owner

zarusz commented Feb 28, 2024

@robert94p as I wrote earlier the 2) will require a new feature which I could add here quite soon.. Once I add the feature it will be available for you to use. Are you in a hurry on this?

@zarusz
Copy link
Owner

zarusz commented Feb 29, 2024

@robert94p thinking about it some more:

  • it would be best to give a setting to have automated acks at the beginning (this is what you want) or at the end (currently works like that) of message processing.
  • in addition give the user the power to perform context.Ack() or contextNack() when there is more involved logic - as in my earlier example

That way we can avoid the interceptor altogether.

Let me know what you think.

@robert94p
Copy link
Author

it would be nice to have both ways: through settings automatically and through context.Ack

@zarusz
Copy link
Owner

zarusz commented Mar 1, 2024

Great, that's what I am working on now.

@zarusz
Copy link
Owner

zarusz commented Mar 3, 2024

hey @robert94p the feature is in preview as of version 2.3.0-rc1: https://www.nuget.org/packages/SlimMessageBus.Host.RabbitMQ

More about the feature:
https://github.com/zarusz/SlimMessageBus/blob/master/docs/provider_rabbitmq.md#acknowledgment-mode

You should be able to set the AckAutomaticByRabbit.

Let me know if any issues or suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants