Wednesday, January 25, 2017

Replace Azure Scheduler with Azure Functions

I have written before about the shortcomings of the Azure Scheduler (doesn't integrate with classic [non-RM] Azure Storage accounts, message body pushed to Azure Storage Queue is wrapped in XML, costs $15/mo for basic scheduling tasks...) but the good news is, now with Azure Functions, your Scheduler Tasks can mostly be replaced with Functions.


Here are a couple tables to break down the differences in Triggers and Actions.


Scheduler Triggers compared to Function Input Bindings



Scheduler
Functions
Manual
X
X
Timer
X
X
Http

X
Storage Queue

X
Service Bus Queue

X
Service Bus Topic

X
Blob

X
Event Hub

X


Scheduler Actions compared to Built-in Function Output Bindings 



Scheduler
Functions
Http
X
X
Storage Queue
X
X
Service Bus Queue
X
X
Service Bus Topic
X
X
Notification Hub

X
Event Hub

X
Table Storage

X
DocumentDB

X
Blob

X
Bot Framework

X
SendGrid

X
Twilio

X
[custom code]

X


You lose some of the nice built-in Azure Scheduler Retry functionality, and Error Action functionality, however for most of my use cases, those could easily be replaced with some custom code in the Azure Function itself.


Another great benefit of Azure Functions, is that they are essentially free. As with AWS Lambdas, the first 1 Million Executions, and 400,000 GB-s of Execution Time, are 100% free, and the costs are very reasonable if you go beyond that (1M Executions for $0.20, and $16 for 1M GB-s of Execution Time)


Below, I'll demonstrate replacing a Timer Trigger Azure Scheduler Task that drops a message into a Storage Queue, with an Azure Function that performs the same task.


On the Azure Scheduler side, this setup is very easy, everything is driven through the Azure Portal UI. On the Azure Functions side, everything can still be accomplished through the Portal UI, but there are some differences. After you have added an Azure Function App to your subscription, open the app and select the "New Function" option.

You'll be greeted with the Choose a Template UI:


Select the "TimerTrigger-CSharp" template, name your new function, and configure the Time schedule. The schedule uses a cron scheduler expression, here are some examples:


"* * * * * *" - run every second
"0 * * * * *" - run every minute, at *:00
"0 */5 * * * *" - run every 5 minutes
"0 0 * * * *" - run every hour, on the hour *:00:00

The format is:
{second} {minute} {hour} {day} {month} {dayofweek}


After you click the Create button, you'll see some C# code like this:


You can modify the code directly in the Portal UI, save, run and test the results.

Click on the "Integrate" tab on the left hand side, to see the various Input/Output bindings. Select "+ New Output", then "Azure Queue Storage":



On the next screen, configure the Queue you would like the Function to write to, and the name of the output parameter we'll need to modify in the code. By default, the param name is outputQueueItem.


Back on the Develop tab, add the outputQueueItem output param to your Azure Function method signature, and assign a value to the string variable. This will be the exact value that is added to the Storage Queue (imagine that, MS won't wrap it in XML... amazing...)



And that's it! Now a message will appear in your Storage Queue every 5 minutes! You can pick it up with a WebJob, or handle it with yet another Function... whatever you'd like.



Hope this helps!
Aaron






Post a Comment