Azure durable function that run once each day

john john Pter 1,040 Reputation points
2024-02-04T15:32:54.8466667+00:00

i want to create an azure durable function which run once a day, now using visual studio 2022 , i create a new azure function of type durable function orchestration using .net 8.0 isolated, as follow:-

User's image

, and i got this default code:-

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.DurableTask;
using Microsoft.DurableTask.Client;
using Microsoft.Extensions.Logging;

namespace FunctionApp1
{
    public static class Function1
    {
        [Function(nameof(Function1))]
        public static async Task<List<string>> RunOrchestrator(
            [OrchestrationTrigger] TaskOrchestrationContext context)
        {
            ILogger logger = context.CreateReplaySafeLogger(nameof(Function1));
            logger.LogInformation("Saying hello.");
            var outputs = new List<string>();

            // Replace name and input with values relevant for your Durable Functions Activity
            outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "Tokyo"));
            outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "Seattle"));
            outputs.Add(await context.CallActivityAsync<string>(nameof(SayHello), "London"));

            // returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"]
            return outputs;
        }

        [Function(nameof(SayHello))]
        public static string SayHello([ActivityTrigger] string name, FunctionContext executionContext)
        {
            ILogger logger = executionContext.GetLogger("SayHello");
            logger.LogInformation("Saying hello to {name}.", name);
            return $"Hello {name}!";
        }

        [Function("Function1_HttpStart")]
        public static async Task<HttpResponseData> HttpStart(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
            [DurableClient] DurableTaskClient client,
            FunctionContext executionContext)
        {
            ILogger logger = executionContext.GetLogger("Function1_HttpStart");

            // Function input comes from the request content.
            string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(
                nameof(Function1));

            logger.LogInformation("Started orchestration with ID = '{instanceId}'.", instanceId);

            // Returns an HTTP 202 response with an instance management payload.
            // See https://learn.microsoft.com/azure/azure-functions/durable/durable-functions-http-api#start-orchestration
            return client.CreateCheckStatusResponse(req, instanceId);
        }
    }
}

my question is how i can configure the durable function to run on schedule bases and not on http request?

Thanks

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MikeUrnun 9,777 Reputation points Moderator
    2024-02-07T07:39:18.49+00:00

    Hi @john john Pter - If running the .net 8.0 isolated process, the following should do the trick:

    [FunctionName("ScheduledStart")]
    public static async Task RunScheduled(
        [TimerTrigger("0 0 * * * *")] TimerInfo timerInfo,
        [DurableClient] DurableTaskClient client,
        ILogger log)
    {
        string functionName = "E1_HelloSequence";
        string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(functionName, null);
        log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
    }
    

    Hope this helps. If any follow-up questions, feel free to tag me in the comments below and ask away.


    Please "Accept Answer" if the answer is helpful so that others in the community may benefit from your experience.

    1 person found this answer helpful.
    0 comments No comments

  2. Azar 29,520 Reputation points MVP Volunteer Moderator
    2024-02-04T15:41:02.4366667+00:00

    Hey john john Pter

    Guess you can use an Azure Timer Trigger attribute on a separate function. lemme give you a code snipp by modufing.

    I have added a new function with a Timer Trigger attribute below

    [Function("ScheduledFunction")] 
    public static void ScheduledFunction([TimerTrigger("0 0 0 * * *")] TimerInfo myTimer, [DurableClient] DurableOrchestrationClient starter, ILogger log) {     
    // Schedule the durable function orchestration    
    string instanceId = starter.StartNewAsync(nameof(Function1), null).Result;     log.LogInformation($"Started orchestration with ID = '{instanceId}'.");  
    
      
    

    now change HTTP-triggered function to call the orchestration function.

    [Function(nameof(HttpStart))] 
    public static async Task<HttpResponseData> HttpStart(     [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,     [DurableClient] DurableOrchestrationClient starter,     
    FunctionContext executionContext) {     
    // Your existing code remains here      
    // Returns an HTTP 202 response with an instance management payload.     
    // See https://learn.microsoft.com/azure/azure-functions/durable/durable-functions-http-api#start-orchestration     
    
    return starter.CreateCheckStatusResponse(req, instanceId); } 
    

    replace the DurableTaskClient with DurableOrchestrationClient.

    If this helps kindly accept the answer thanks miuch.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.