0
votes

When my app needs to send an email, it creates a db entry and puts it into the database and then puts a message on a Service Bus that is monitored by an Azure Function running as a WebJob.

When the WebJob process finishes sending the email, I would like it to conditionally put a new scheduled message on the Service Bus queue - really, I want the WebJob to be woken up in 5 minutes to see if there is an email in the db that needs to be sent.

But if there is already a message on the queue to that the WebJob has to process, then I don't want to put this scheduled message on the queue, I just want to finish processing because I know that the queue is going to wake up the WebJob immediately again, so there is no need to add the 5 minute scheduled message on the queue this time, it will be put on the queue after the next WebJob completion.

I am also using Scheduled messages from the App because there are times when I need to send a message in the future, so I add a scheduled message to the service bus queue to wake up the sender at a specific time.

I would like to do a peekBatch on the queue to see if there is already a message that will cause the WebJob to be woken up before the 5 minutes elapse. If there is, I want to skip putting a new message on the queue, but if there isn't, I would like to add a message to the queue.

To be clear, this peekBatch needs to happen inside an Azure Function inside a WebJob that uses a "ServiceBusTrigger".

What I can't figure out is how to get access to something to do the PeekBatch on. I have found examples of how to send the message from within the ServiceBusTrigger function.

1

1 Answers

0
votes

MessageReceiver instance is passed in as a parameter for Service bus trigger. https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger?tabs=csharp#usage

Using this instance, you should be able to call PeekAsync on the subscription.

Just as a side thought, if you are using the scheduled message solely for the purpose of triggering the function to check the DB, one thing I'd try for myself is to refactor the main functionality into a service and set up a second function with a timer trigger.