Search code examples
azure-functionsazure-webjobsdeferredservicebusazure-servicebus-queues

Defer or Abandon message servicebus queue from azure function


In my scenario, I'm sending messages to a ServiceBus Queue. I have an azure function that is being triggered on that Queue. The function receives the message, checks status using some other API endpoint, if it's processed I send out an email. But when the processing of that request is not complete, I want to put the message on the queue (with some delay or scheduled time later in the future) so that function receives the same message again and checks again. The order doesn't necessary matter that much for me.

I have tried these approaches so far:

BrokeredMessage.Abandon() - which immediately triggers the function - not desirable behavior BrokeredMessage.Abandon() with ScheduledEnqueueTimeUtc property -- same behavior

BrokeredMessage.Defer() - here i need to keep track of message sequence number to receive the message using OnReceive - not convenient (or even possible within a function?)

Resending the same message to the queue with ScheduledEnqueueTimeUtc property set to later time - For this i need to get reference to the Queue Client again and send the message -- THIS sort of works but feels wrong (as i have to complete the actual message i received and dispatch another message from within the function).

Moreover, I've tried using WebJobs and Azure Storage Queue - pretty much same issues i encountered there.

Is there any better way to get this done? I'm open to use other approaches as well. I'm not sure if I can achieve something similar with LogicApp?

Thanks Sanjay


Solution

  • I think your best bet is to send a new message. For that, use an output binding and return an instance of BrokeredMessage with proper ScheduledEnqueueTimeUtc value. You can Clone the original message, if you get it as BrokeredMessage in your function. Propably, you will need to use ICollector for this output binding, since your output is conditional.

    Functions runtime calls Complete/Abandon methods itself based on function execution result, so you can't do that manually inside the function. Defer-Receive combination isn't supported either.

    The same approach should apply to Storage Queues.