pubsub icon indicating copy to clipboard operation
pubsub copied to clipboard

More of a question than issue: Subscriber handles the same message multiple times

Open digitaldias opened this issue 5 years ago • 1 comments

I have a piece of code that takes an "event" and stores that event to CosmosDb. I've verified that there is only a Singleton instance of my EventManagementService (constructor only runs once), and also that the publisher of the event only transmits the event once. Here is the subscriber piece of code:

public void Initialize()
        {            
            _hub.Subscribe<NoobEvent>(SaveMessageToEventStore);            
        }

        private async void SaveMessageToEventStore(NbtEvent updateEvent)
        {
            _hub.UnSubscribe<NoobEvent>();

            _logger.Information($"Transmitting event to eventstore: {updateEvent.EventName} {updateEvent.EventType}");
            
            //TODO: (Pedro) Validate the updateEvent

            await _exceptionHandler.RunAsync(() => _eventStore.Write(updateEvent));

            _hub.Subscribe<NoobEvent>(SaveMessageToEventStore);
        }

If I do not unsubscribe while executing the "SaveMessageToEventstore()", then I see that it executes twice. Questions to this:

  • Why does the subscriber invoke twice? I only have a single publisher, and a single handler
  • By Unsubscribing at the beginning of the handler, does this run the risk of loosing any potentially queued messages, or will it resume where left off?
  • Is there a way to mark a published message as "completed" that I am missing?

In advance, thanks.

digitaldias avatar Apr 17 '20 05:04 digitaldias

This definitely smells like a bug, I'll have to dig into it a bit and see what I can find

upta avatar Apr 18 '20 18:04 upta