We have a Dotnet BackgroundService(Microsoft.Extensions.Hosting) running from a WebApi project. There is a Kafka Consumer(fairly new to Kafka world) running from within the ExecuteAsync method. We would like our Kafka consumer to stop/start consuming based on a schedule(stop on non-business days, run only between 8am-6pm on business days etc.). As far as we see, there are couple of ways to get this done.
- Run the background service based on a schedule. We did some basic research and still not sure on how to achieve it.
StartAsync
andStopAsync
seems to be hooked with application lifecycle events and doesn't like something we will be able to use for our purpose. - Use
consumer.pause()
andresume()
to work based on a schedule. How can we achieve this programatically from Dotnet. We were not able to find any specific event handlers(custom schedule event) on the CosumerBuilder class. Any suggestions/recommendations on what is the best option here and some direction on how to achieve it would be greatly appreciated.