My team and I support several background worker console applications that synchronously process messages from a queue.
Currently, we spin up new instances of the app using docker to handle multiple messages when the number of messages in the queue exceeds a certain threshold.
We were discussing alternative ways we could process these messages in parallel and one teammate proposed using a top level, async void
method in a while
loop to process the messages.
It looks like it will work, since it would be similar to a UI-based application using async void as an event handler.
However, I've personally never written a background worker to handle multiple messages in parallel this way, and wanted to know if anyone had experience doing so, and if so, if there are any "gotchas" that we aren't thinking of.
Here is a simplified version of the proposed solution:
static async void Main(string[] args)
{
while (true)
{
TopLevelHandler(await ReceiveMessage());
}
}
static async void TopLevelHandler(object message)
{
await DoAsyncWork(message);
}
static async Task<object> ReceiveMessage()
{
//fetch message from queue
return new object();
}
static async Task DoAsyncWork(object message)
{
//processing here
}