hjavaher
hjavaher

Reputation: 2617

Azure Webjobs SDK and Queue handling errors

I'm working on a webjob that checks a Webserver for some data. Sometimes the Webserver is busy and will come back with a try again in a few seconds.

Problem is that if I just ignore it and go through the function, it will delete the message from the queue and I will not be able to retry the call.

My current solution (which I don't like at all) is to through an exception which will increase the dequque number and put the message back on the queue. This however seems very brutal and requires the thread which the webjob is running on to restart.

Is there any other way of handling this?

Upvotes: 3

Views: 3422

Answers (1)

Victor Hurdugaci
Victor Hurdugaci

Reputation: 28425

The webjobs SDK will retry queue triggered functions if they throw an exception. The number of retries is configured through the JobHostConfiguration.Queues.MaxDequeueCount property as described here.

If you don't want to rely on the SDK's retry policy, you can always handle the exception in your own code and retry. Just catch the exception or whatever happens when the server is busy and then retry the operation until it succeeds. However, this is exactly what the SDK does behind the scene and by doing this yourself, you don't get the automatic move to poison queue functionality.

Unless the function cannot be designed to be reentrant, I recommend letting the SDK do the retry job. If you still decide to handle it yourself, here is how the code could look like:

public static void WebJobFunction([QueueTrigger("temp")] string message)
{
    int retriesLeft= 5;

    while(true) 
    {
       try
       {
            --retriesLeft;
            DoServerOperation(message);
            break;

        }
        catch(TimeoutException)
        {
            if (retriesLeft == 0) 
            {
               throw;
            }
        }       
    }
}

Upvotes: 5

Related Questions