azureazure-functionsserverless-architectureserverless

How to invoke Durable function by timer trigger?


I am new to Durable function(Orchestration function) and seen sample application as per Microsoft documentation.So I have few doubts.

example:

public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, methods: "post", 
            Route = "orchestrators/{functionName}")] HttpRequestMessage req,
            [OrchestrationClient] DurableOrchestrationClient starter,
            string functionName,
            TraceWriter log)
        {
            // Function input comes from the request content.
            dynamic eventData = await req.Content.ReadAsAsync<object>();
            string instanceId = await starter.StartNewAsync(functionName, eventData);

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

            return starter.CreateCheckStatusResponse(req, instanceId);
        } 

to invoke it I made HTTP POST request using postman so request processed successfully but when I configured different verb like HTTP GET it was responded with NotFound" error in console as well as request made to it with http request from browser responded with "NotFound" error in console .Why this happened?

Can I invoke any Orchestration function with in timer trigger azure function?

If not why?

UPDATE:

Some additional details about question

    [FunctionName("TimerTrigger")]
            public static async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log)
            {//this runs for every 5minutes
                using (HttpClient client = new HttpClient())
                {
                    var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("", "")
                });
               //making request to above function by http trigger
                    var result = await client.PostAsync("http://localhost:7071/orchestrators/E1_HelloSequence", content);
                }
                log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
                return;
            }

can I make request to http trigger by timer triggred why because my durable function has long running process so if invoke orchestration function in timer trigger itself so there might be possibility of timer triggered timeout so that why I am trying to follow this approach.Is it possible to invoke by above code?


Solution

  • This is general Azure Functions behavior. The reason GET doesn't work is because the function in the sample is only configured to work with POST. See the [HttpTrigger] attribute in the function signature:

    [HttpTrigger(AuthorizationLevel.Anonymous, methods: "post", 
            Route = "orchestrators/{functionName}")]
    

    If you want to support GET, then change the methods parameter accordingly:

    [HttpTrigger(AuthorizationLevel.Anonymous, methods: "get", 
            Route = "orchestrators/{functionName}")]
    

    Note that Visual Studio seems to have a caching bug where making changes to route information is not properly saved when debugging locally. I opened a GitHub issue to track that here: https://github.com/Azure/Azure-Functions/issues/552

    For more information on HTTP triggers, see this documentation: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook

    If you want to trigger a Durable Function using a timer trigger, then just change the trigger type. For example:

    [FunctionName("ScheduledStart")]
    public static async Task RunScheduled(
        [TimerTrigger("0 0 * * * *")] TimerInfo timerInfo,
        [OrchestrationClient] DurableOrchestrationClient starter,
        TraceWriter log)
    {
        string functionName = "E1_HelloSequence";
        string instanceId = await starter.StartNewAsync(functionName, null);
        log.Info($"Started orchestration with ID = '{instanceId}'.");
    }
    

    EDIT: If you're using Durable v2.x, then the syntax looks like this:

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

    For more information on Timer triggers, see this documentation: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer