fgiuliani
fgiuliani

Reputation: 390

Azure Function with HTTP trigger return "received" message before start processing

I'm creating an Azure Function to process files. The Azure Function will be activated with a HTTP Trigger, so the function should be executed whenever a page from a site makes a HTTP request to it.

The Function will take some time to finish processing the files, but I can't get the site waiting for the Function to finish to know If everything was ok. So what I want is some kind of "received" message from the Azure Function just to know that it received the HTTP request, before it starts processing.

Is there any way to do that with a HTTP Trigger? Can I let the caller know that its request was correctly received and, after that, start executing the Azure Function?

Upvotes: 2

Views: 1511

Answers (1)

Thiago Custodio
Thiago Custodio

Reputation: 18362

Yes, it's super easy to do that using Durable Functions:

1- Install 'Microsoft.Azure.WebJobs.Extensions.DurableTask' nuget package;

2-

   [FunctionName("Function1")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        [DurableClient] IDurableOrchestrationClient starter,
        ILogger log)
    {
        Guid instanceId = Guid.NewGuid();
        string x = await starter.StartNewAsync("Processor", instanceId.ToString(), null);

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

        return starter.CreateCheckStatusResponse(req, x);
    }

3-

    [FunctionName("Processor")]
    public static async Task<string> Search([OrchestrationTrigger] IDurableOrchestrationContext context)
    {
        var output= await context.CallActivityAsync<string>("DoSomething", null);

        return output;
    }



   [FunctionName("DoSomething")]
    public static async Task<string> Execute([ActivityTrigger] string termo, ILogger log)
    {
        //do your work in here
    }

In the previous code we're creating an Orchestrator (Processor) function, and it will start an activity which will do the process DoSomething function.

More info: https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-overview

Upvotes: 3

Related Questions