Creating a Scheduled Azure Function
Tags: azure, function, schedule
Published: 2019-08-08 05:37:15

---
title: Creating a Scheduled Azure Function
published: true
description: Creating a Scheduled Azure Function
tags: Azure, Function, Schedule
---

<link rel="canonical" href="https://www.pmichaels.net/2018/02/11/creating-scheduled-azure-function/" />

I've <a href="http://pmichaels.net/2017/09/17/function-apps-azure/">previously written</a> about creating Azure functions.  I've also <a href="http://pmichaels.net/2017/07/23/using-azure-functions-send-e-mail-alert-service-bus/">written about how to react to service bus queues</a>.  In this post, I wanted to cover creating a scheduled function.  Basically, these allow you to create a scheduled task that executes at a given interval, or at a given time.

<h1>Timer Trigger</h1>

The first thing to do is create a function with a type of Timer Trigger:

<a href="http://pmichaels.net/wp-content/uploads/2018/02/schedule-1.png"><img src="http://pmichaels.net/wp-content/uploads/2018/02/schedule-1-300x187.png" alt="" width="300" height="187" class="alignnone size-medium wp-image-3004" /></a>

<h1>Schedule / CRON format</h1>

The next thing is to understand the schedule, or CRON, format.  The format is:

<blockquote>
{second} {minute} {hour} {day} {month} {day-of-week}
</blockquote>


<h2>Scheduled Intervals</h2>

The example you'll see when you create this looks like this:

    0 */5 * * * *

The notation <code>*/[number]</code> means once every number; so */5 would mean once every 5... and then look at the placeholder to work out 5 what; in this case it means once every 5 minutes.  So, for example:


    */10 * * * * *


Would be once every 10 seconds.


<h2>Scheduled Times</h2>

Specifying numbers means the schedule will execute at that time; so:

    0 0 0 * * *

Would execute every time the hour, minute and second all hit zero - so once per day at midnight; and:

    0 * * * * *

Would execute every time the second hits zero - so once per minute; and:

    0 0 * * * 1

Would execute once per hour on a Monday (as the last placeholder is the day of the week).

<h2>Time constraints</h2>

These can be specified in any column in the format [lower bound]-[upper bound], and they restrict the timer to a range of values; for example:

    0 */20 5-10 * * *

Means every 20 minutes between 5 and 10am (as you can see, the different types can be used in conjunction).

<h2>Asterisks (*)</h2>

You'll notice above that there are asterisks in every placeholder that a value has not been specified.  The askerisk signifies that the schedule will execute at every interval within that placeholder; for example:

    * * * * * *

Means every second; and:

    0 * * * * *

Means every minute.

<h1>Back to the function</h1>

Upon starting, the function will detail when the next several executions will take place:

<a href="http://pmichaels.net/wp-content/uploads/2018/02/schedule-2.png"><img src="http://pmichaels.net/wp-content/uploads/2018/02/schedule-2-300x58.png" alt="" width="300" height="58" class="alignnone size-medium wp-image-3003" /></a>

But what if you don't know what the schedule will be at compile time.  As with many of the variables in an Azure Function, you can simply substitute the value for a placeholder:

    [FunctionName("MyFunc")]
    public static void Run([TimerTrigger("%schedule%")]TimerInfo myTimer, TraceWriter log)
    {
        log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
    }

This value can then be provided inside the local.settings.json:

    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "DefaultEndpointsProto . . .",
        "AzureWebJobsDashboard": "DefaultEndpointsProto . . .",
        "schedule": "0 * * * * *"
      }
    }


<h1>References</h1>

<a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer">https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer</a>

<a href="http://cronexpressiondescriptor.azurewebsites.net/?expression=1+*+*+*+*+*&locale=en">http://cronexpressiondescriptor.azurewebsites.net/?expression=1+*+*+*+*+*&locale=en</a>

Originally posted <a href="https://www.pmichaels.net/2018/02/11/creating-scheduled-azure-function/">here</a>.