Neo
Neo

Reputation: 16239

How to put message into dead-letter and process it after some interval in azure function

I have azure function which trigger when we ave new message into service bus topic.

In azure function I checked the customer api is available or not by calling it and checking its status code.

If status code is 200 I need to process the message else put this message into dead-letter and after some interval when customer api is available then process all dead letter message also.

 public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([ServiceBusTrigger("customer-order", "customer-order", Connection = "")]string mySbMsg, ILogger log)
        {
            // 1.call customer api to check it is available or not 
            // 2.if it is up and running then process the message else put message into dead-letter 
            // and after some interval when customer ai is available process dead-letter messages

            log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
        }
    }

I can able to invoke customer api using HTTPClient and process message also.

but how to put message into dead-letter and How to execute dead-letter after some interval when customer api is available ?

Proposed Flow Will be

in azure function app which will trigger if new message is there into.

topic start step - Check api is available or down if api is available

process the current message if api is down then do not process

message here we have two choices

1a.put current message into dead letter  

1b.put current message back into main but if we do this then again function app will trigger as its new message trigger based and start step will continue.

Upvotes: 2

Views: 1117

Answers (1)

Turbo
Turbo

Reputation: 2561

Rather than putting this in dead letter queue, a better approach would be to defer the message for a certain duration.

If a message cannot be processed because a particular resource for handling that message is temporarily unavailable but message processing should not be summarily suspended, a way to put that message on the side for a few minutes is to remember the SequenceNumber in a scheduled message to be posted in a few minutes, and re-retrieve the deferred message when the scheduled message arrives.

See this answer for an example to how to do deferral in Azure functions v2. Note that the input binding is using message of type Message and is also using the injected MessageReceiver. Those are needed to be able to call DeferAsync. The template code for service bus trigger sets the message type to string, so you would have to change signature as described in that answer.

About deferred messages:

Deferred messages remain in the main queue along with all other active messages (unlike dead-letter messages that live in a subqueue), but they can no longer be received using the regular Receive/ReceiveAsync functions. To retrieve a deferred message, its owner is responsible for remembering the SequenceNumber as it defers it. Any receiver that knows the sequence number of a deferred message can later receive the message explicitly with Receive(sequenceNumber).

How to schedule messages with sequence number of deferred messages so that deferred message can be processed at a later time:

You can schedule messages either by setting the ScheduledEnqueueTimeUtc property when sending a message through the regular send path, or explicitly with the ScheduleMessageAsync API

Upvotes: 2

Related Questions