Search code examples
c#asp.netazureazure-storage-queues

Dequeue from a Azure Storage queue very often without running multiple instances of cron job?


I have a pretty typical challenge: I have an application where I want to send emails/SMS messages. I use third party API's for that (Mandrill and GatewayAPI). This is usually fast, but some of my messages are big and will take a while to send. That is why I want to move it to a different system.

My plan was to make a simple Azure Web Job reading from an Azure message queue that runs very often (ie 15 seconds), to make sure all new communication are sent quite fast.

My question is: is it a problem if my job sometimes takes longer than the interval? And if so, is there something obvious about my design that is flawed (ie using a message queue together with a web job with a low interval)?

(I hope this is not seen as an opinion based question - it's really not - I have an actual problem with how to program this setup)


Solution

  • What you need to do is to subscribe to a trigger to a specific queue.

    This is really simple. When you create a new Azure web job based on the standard template, you get the following function inside Functions.cs which is added by default:

        // This function will get triggered/executed when a new message is written 
        // on an Azure Queue called queue.
        public static void ProcessQueueMessage([QueueTrigger("QUEUENAME")] string message, TextWriter log)
        {
           // your code
        }
    

    By then moving the code in here you don't have the problem with intervals because it's processed right away by the trigger.