azure-service-bus-dotnet icon indicating copy to clipboard operation
azure-service-bus-dotnet copied to clipboard

No possibility to shut down pump in Message Receiver to implement graceful shutdown

Open ilya-git opened this issue 7 years ago • 1 comments

MessageReceiver class allows to start a message pump after calling RegisterMessageHandler that works quite well. The problem arises when I want to stop this pump as e.g. a part of Windows Service shutdown. I would like to implement graceful shutdown but the class does not allow to shutdown the pump separately and it means all the outstanding messages won't be able to call complete (I tried to open separate receiver to call complete, but it does not seem possible). Additionally there is no notion of active message handlers or at least a count that will allow implementation of the graceful shutdown.

Implementation suggestion:

  • Add a method to MessageReceiver reciever that will allow closing the pump separately so that it will be possible to call CompleteAsync after this method is called.
  • Add a method/property to get a list of all executing handlers as tasks (or at least an active message handlers counter) that will allow external code to do a graceful shutdown loop until it reaches zero or times out

Additional suggestions for a higher level graceful shutdown (can be implemented either instead or alongside first suggestion)

  • Implement a method to do a graceful shutdown of the whole receiver with pump that will close the pump immediately and then allow all the running handlers to be executed (within given timeout) before the receiver itself is closed. Ideally it should check at the end of each handler how many other handlers are remaining so that when it reaches 0 it closes immediately instead of simply waiting to maximum timeout before closing.

ilya-git avatar Jun 21 '18 18:06 ilya-git

This feature would be very helpful. From experience, it is hard to get transactional isolation of all kinds of message processing, or idempotent states of target systems - resulting in issues when double processing messages. Graceful shutdown could alleviate this concern at least to some degree. 👍

devbeard avatar Jun 22 '18 10:06 devbeard