简体   繁体   中英

What to do when Azure Function takes long time to complete?

I'm new to Azure Functions and trying to do a new project. I've run into some issues when my function takes too long to process the data. I'm using a BlobTrigger and every time someone uploads a new blob I need the data in the blob to analyze some things using other Microsoft Services.

Here is the code for my Azure Function:

[StorageAccount("BlobConnectionString")]
public class AnalyzeAzureBlob
{
    private readonly IAnalyzeResult analyzeResult;

    public AnalyzeAzureBlob(IAnalyzeResult analyzeResult)
    {
        this.analyzeResult = analyzeResult;
    }
    [FunctionName("AnalyzeAzureBlob")]
    public void Run(
        [BlobTrigger("samples-analyze/{name}")]Stream inputBlob,
        [Blob("analyzed/{name}", FileAccess.Write)] Stream outputBlob,
        string name,
        ILogger log)
    {

        log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {inputBlob.Length} Bytes");

        try
        {
            this.analyzeResult.AnalyzeData(inputBlob, outputBlob);
            log.LogInformation("The file has been analyzed");
        }
        catch (Exception ex)
        {
            log.LogError("The analyze failed", ex);
            Console.WriteLine(ex.ToString());
        }
    }
}

The function runs as it should but sometimes the timeout is reached and it fails if the blob contains a large amount of data. How can I avoid timeouts and wait for the whole process to complete using Azure Functions?

Note: I've read about Azure Durable Functions but have a hard time understanding how to implement them and use them in my code.

How can I avoid timeouts and wait for the whole process to complete using Azure Functions?

As you not mentioned which hosting plan using for the Azure Function App. But if the request processing takes long time and giving the timeout exceptions then change to Premium or dedicated hosting plan of Azure Function App.

  • Implement chunks (File Chunking) which means the blob file could be processed in a chunks format using Azure Function Blob Trigger and a sample workaround given by Andrew Hoefling in his blog.

I've read about Azure Durable Functions but have a hard time understanding how to implement them and use them in my code.

Two Functions - One is for Orchestrator in Durable Function and the other is for Blob trigger that would be to start/trigger running the durable orchestrator function.

For example:

Blob Trigger :

[FunctionName("StartOrchestratorBlobTriggerFunction")]
public async Task StartOrchestratorBlobTriggerFunction(
[BlobTrigger("sample-workitems/{name}", Connection = "<Storage-Account-Name>")] Stream myBlob,
             string name,
             [OrchestrationClient] DurableOrchestrationClient durableOrchestrationClient,
             ILogger log)
    {
        // Obtain your blob content, deserialize it if necessary, and provide it to the orchestrator rather than the stream as seen below.
        await durableOrchestrationClient.StartNewAsync("YourNewDurableFunction", myBlob);   
    }

Durable Function :

[FunctionName("YourDurableFunction")]
 public async Task YourDurableFunction(
     [OrchestrationTrigger] DurableOrchestrationContextBase orchestrationContext,
     ILogger logger)
{
    // here Calling the activity functions
}

The function should be finished as quick as possible, you should take the long task logic into a different solution, triggered by queue message, so the Azure function only enqueues the message into the queue and finish instantly.

I had the exact use case here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM