2
votes

I have a windows service that listens for Azure Service Bus Queue Messages in order to distribute processing from my WebApi application. I additionally need to handle recurring tasks (nightly/weekly), which I thought might be best handled using the same system.

For example, assume I have a queue for "CleanupDb" to delete stale DB nodes every day at midnight:

var client = QueueClient.Create("CleanupDb");
var options = /* ... */

client.OnMessage((message) => {
    client.Send(new BrokeredMessage() {
        ScheduledEnqueueTimeUtc = DateTime.Today.AddDays(1)
    });

    // do DB Cleanup
}, options)

In theory this should work, but I feel like I missing a more obvious way of handling this. Is there a better way?

1
It's fine. Another option, you can create a Azure WebJob, but it will produce the same result.Thiago Custodio

1 Answers

1
votes

Yes, this would work, but I would not recommend this approach for something like is firing once a day. The issues I see with this are:

  • Even if you supply a fairly healthy back-off policy this could still be spending transactions or keeping connections against a queue that is almost always empty. I see this as a waste of resources.
  • If whatever is hosting this service goes down then your operations will not be processed.
  • It is possible that an exception occurs that means the message for the processing is not put on the queue for the next day.

Thiago mentioned running an Azure WebJob as an option in a comment above, and I agree with that. It can be scheduled and not utilize any other resources until it is needed. Another option, especially if you aren't already hosting an Azure Website, is looking at Azure Automation, which for a schedule of daily should work fine (just understand that long running tasks might get suspended and thus may work better as an Azure WebJob).