Create Azure TimerTrigger Durable Function with Example

In one of my previous article, we discussed about Azure Durable Function and how to create HTTP Trigger durable function. Today, we will discuss how to create Timer Trigger Durable Function which runs for every specific time.

As shown in Microsoft Azure Durable Functions Introduction, create new Azure Functions and Add Durable Functions Orchestrations function. Update the code as below, here also we are trying to update the blob storage file with the date and time.

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace DurableFunctionExp
    public static class TimerTriggerDurableFunction
        public static async Task RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context)
            string data = await context.CallActivityAsync<string>("GetData", null);

            bool result = await context.CallActivityAsync<bool>("UpdateBlobFile", data);

        public static string First([ActivityTrigger] object obj)
            string sData = "Time from First() function - " + DateTime.Now.ToString() + Environment.NewLine;
            return sData;

        public static bool Second([ActivityTrigger] string content)
            return Update(content);

        private static bool Update(string content)
            string storageAccntConnection = "please enter your storage account connection string here";
            string blobFileName = @"blobTestFile.txt";

            var storageAccnt = CloudStorageAccount.Parse(storageAccntConnection);

            var blobClient = storageAccnt.CreateCloudBlobClient();

            var container = blobClient.GetContainerReference("techblobcontainer");            

            var blob = container.GetBlockBlobReference(blobFileName);

            using (MemoryStream ms = new MemoryStream())
                byte[] timeBytes = Encoding.UTF8.GetBytes(content + Environment.NewLine);
                ms.Write(timeBytes, 0, timeBytes.Length);
                ms.Position = 0;

            return true;

        public static async Task TimerTriggerStart(
            [TimerTrigger("*/5 * * * * *")] TimerInfo timerInfo,
            [DurableClient] IDurableOrchestrationClient starter,
            ILogger log)
            string instanceId = await starter.StartNewAsync("TimerTriggerDurableFunction", null);

            log.LogInformation($"Started orchestration with ID = '{instanceId}'.");

As shown in the code, we have two Activity functions GetData() & UpdateBlobFile(). GetData() returns current date & time as input to the second Activity function UpdateBlobFile(). UpdateBlobFile() updates the blob storage file blobTestFile.txt with the data received from the GetData() function. TimerTriggerStartClient is the client which calls the Orchestrator Function TimerTriggerDurableFunction(). We have changed the Http Trigger to Timer Trigger for TimerTriggerStartClient() function which runs for every five seconds (*/5 * * * * *).

Publish the code to Azure Portal, Function App displays with all functions as shown below.

The above code updates the blobTestFile.txt file in the given blob storage with the current date and time for every five seconds as shown below.

Add comment