pardahlman / RawRabbit

A modern .NET framework for communication over RabbitMq
MIT License
747 stars 144 forks source link

Question - Subscription error detection #329

Closed afmuller closed 6 years ago

afmuller commented 6 years ago

I have a basic consumer:

            // Subscribe to changes to entities 
            var entitySubscriptionTask = busClient.SubscribeAsync<EntityConfigCrudEvent>(crudEvent =>
            {
                // do something
                return Task.CompletedTask;
            },
                context =>
                    context
                        .UseSubscribeConfiguration(p => p
                            .FromDeclaredQueue(q => q
                                .WithNameSuffix($"{Guid.NewGuid().ToString()}")
                                .WithAutoDelete(true)
                            )
                        )
            , cancellationToken);

How can a detect failure of the consumer? E.g. rabbitmq server goes down, network is down etc. How do I know if the consumer is still running or not? I thought I could monitor the task (i.e in this case entitySubscriptionTask) but to completion immediately.

Is there an event I can subscribe to that can notify me if there is a problem or an error with the subscription?

Andrew

pardahlman commented 6 years ago

Hi @afmuller - RawRabbit handles connection failure and reconnection etc out of the box. The internal logger will create entires as the events happens. If this isn't enough, you can get hold of the underlying consumer; the task return from SubscribeAsync is in fact a Task<IPipeContext>, the execution context from the operation. There is no easy way to get hold of the context, but this should work

var t = subscriber.SubscribeAsync<Message>(async msg => {...});
if (t is Task<IPipeContext> contextTask)
{
    await contextTask;
    var consumer = contextTask.Result.GetConsumer();
    consumer.ConsumerCancelled += (sender, args) => { };
    consumer.Model.ModelShutdown += (sender, args) => { };
}

Note that the context exposes internal state that if changed might have undocumented affect on RawRabbit.

HTH!