Im building a queue processing webjob to read messages from a queue and use the data to retrieve a report uri from blob storage then send this as a link in an email. I have the process working perfectly well, but I need to send the email within a specific time window.
I have another process (webjob) that retrieves this data from a sql backend and places it in the email queue.
This webjob runs every 30 mins and only gets data for the current day and within a 2 hour window of the current time. So I know that anything in the queue is for today and within 2 hours of 'now'. How would i narrow this down further to read data from the queue, and if the 'email out' time is set to say 19:00 and the current time is 18:00, I can put this message back in the queue to read it again later, next time it should be closer to 19:00 then I can process it and send it out in an email.
The time doesn't have to be spot on, so even if its within, say 30 mins of 19:00 (or whatever time its set to be sent) It can be processed. So Im effectively taking an object from the queue, checking its time and if its not within say 30 mins of its allotted 'email out' time, I place it back in the queue and its processed again
** in my webjob, I have a 'Functions' class that contains a method 'ProcessQueueMessage' which is triggered whenever a message is placed in the queue.
// This function will get triggered/executed when a new message is written
// on an Azure Queue called queue.
public async Task ProcessQueueMessage([QueueTrigger("%reportgenerator%")] Data.Dto.Schedule.ScheduleDto schedule)
{
var reports = await this._scheduledReportGenerationService.GenerateScheduledEmails(schedule.ID);
}
The ScheduleDto class will have a generation time property, I can read this and compare it to the current time and process it only if its within my specified 'time window'. How would i stop the queue message from being deleted here so that I can re-process it ?
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);