I have a long running process which performs matches between millions of records I call this code using a Service Bus, However when my process passes the 5 minute limit Azure starts processing the already processed records from the start again.
How can I avoid this
Here is my code:
private static async Task ProcessMessagesAsync(Message message, CancellationToken token)
{
long receivedMessageTrasactionId = 0;
try
{
IQueueClient queueClient = new QueueClient(serviceBusConnectionString, serviceBusQueueName, ReceiveMode.PeekLock);
// Process the message
receivedMessageTrasactionId = Convert.ToInt64(Encoding.UTF8.GetString(message.Body));
// My Very Long Running Method
await DataCleanse.PerformDataCleanse(receivedMessageTrasactionId);
//Get Transaction and Metric details
await queueClient.CompleteAsync(message.SystemProperties.LockToken);
}
catch (Exception ex)
{
Log4NetErrorLogger(ex);
throw ex;
}
}
Messages are intended for notifications and not long running processing.
You've got a fewoptions:
RenewLock()
operation to extend the lock.MessageHandlerOptions.MaxAutoRenewDuration
setting to auto-renew message's lock.SequenceNumber
. This will allow you to periodically receive a "reminder" message to see if the work is finished. If it is, complete the deferred message by itsSequenceNumber
. Otherise, complete the "reminder" message along with sending a new one. This approach would require some level of your architecture redesign.Note that option 1 and 2 are not guaranteed as those are client-side initiated operations.