azure queue storage - putting a message back in the queue azure queue storage - putting a message back in the queue azure azure

azure queue storage - putting a message back in the queue


When you add the message to the queue just set the initialVisibilityDelay so that the message isn't seen until the minimum process time.

CloudQueue queue = queueClient.GetQueueReference(queueName);var msg = new CloudQueueMessage("Hello World!");TimeSpan timeSpanDelay = GetEarliestProcessTime();await queue.AddMessageAsync(msg, null, timeSpanDelay, null, null);

CloudQueue.AddMessage


So there're a few things with Azure Storage Queues that is going to help you with this scenario:

Regarding putting the message back in the queue, you don't have to do anything special. It is a feature offered by Storage Queues. When you dequeue a message (GET Message in Azure Storage lingo), the message becomes invisible for certain amount of time and if not deleted by the process which dequeued it will become visible again and can be picked up by another process.

So when you dequeue the message, check the time and if it is not the right time you don't do anything. However please make sure that once the message is processed, you delete that message otherwise it will be picked up again.

One more thing you could do is when you dequeue the message and find that it is not the right time to process that message, you update that message and set its visibility timeout property to a value that will make the message visible again closer to the processing time. For example, you dequeued the message at 18:00 and found that this message needs to processed at 19:00. In this case you will update the message and set its visibility timeout to 50 minutes (or a value greater than 30 minutes as 30 minutes is the schedule for your web job). What this will do is ensure that when your webjob runs at 18:30, this message will not be picked up by the web job because the message will only become visible at 18:50.

You can read more about updating message here: https://docs.microsoft.com/en-us/rest/api/storageservices/update-message and about dequeuing messages here: https://docs.microsoft.com/en-us/rest/api/storageservices/get-messages.

Update

I completely forgot that it is in a WebJob so doing nothing would actually delete the message. I guess you have 2 options (kind of repeating what's mentioned in the comments):

  1. Throw an exception instead of doing nothing. This will ensure that WebJob processor does not delete the message. I have not tried it myself but you can also update the message and set its visibility timeout to a value closer to desired time in the WebJob itself (and then throw an exception). This is kind of an anti-pattern though.
  2. You add a new message in the queue and set its initial visibility timeout to a value closer to desired time (this is also covered in another answer) and let this message delete.


When you en-queue an item in Azure Storage queues you can add extra detail to the item that will cause the item to be hidden for a configurable duration. If your batch job can only run every two hours but you want to delay sending emails with more fine-grained time control then I suggest the two-hourly en-queuing batch job could use this "initialVisibilityDelay" feature.

Here is another SO question that describes the API.

Azure storage queue message (show at specific time)