Andrew Williamson
Andrew Williamson

Reputation: 8696

Is there a way I can delay the retry for a service bus message in an Azure function?

I have a function which pulls messages off a subscription, and forwards them to an HTTP endpoint. If the endpoint is unavailable, an exception is thrown. When this happens, I would like to delay the next attempt of that specific message for a certain amount of time, e.g. 15 minutes. So far, I have found the following solutions:

What I would ideally like to happen is to catch the exception, and exit the function without releasing the lock on the message. That way, as soon as the lock expires the message will be retried again. However, it seems that after completing successfully, the function calls Complete() on the message, and after an exception is thrown, the function calls Abandon() on the message. Is it possible to bypass this, or to achieve the delay some other way?

Upvotes: 4

Views: 2319

Answers (3)

Douglas
Douglas

Reputation: 54887

This is now supported natively through retry policies, which were added to Azure Functions around November 2020 (preview). You can configure the retry policy as fixed-delay or exponential-backoff.

[FunctionName("MyFunction")]
[FixedDelayRetry(10, "00:15:00")]   // retries with a 15-minute delay
public static void Run(
    [ServiceBusTrigger("MyTopic", "MySubscription", Connection = "ServiceBusConnection")] string myQueueItem) 
{
    // Forward message to HTTP endpoint, throwing exception if endpoint unavailable
}

Upvotes: 2

Marie Hoeger
Marie Hoeger

Reputation: 1331

Although still in preview (not recommended for production code), you could use Durable Functions. If you want to maintain the ability to manipulate objects in code, this is likely your best bet!

(+1 to LogicApp solution too!)

Upvotes: 1

DTRT
DTRT

Reputation: 11040

I will address your situation by offering that the flow you are proposing is better handled by a LogicApp over a pure function.

It's quite easy to implement the wait/retry/dequeue next pattern in a LogicApp since this type of flow control is exactly what LogicApps was designed for.

Upvotes: 1

Related Questions