[英]What to do when Azure Function takes long time to complete?
I'm new to Azure Functions and trying to do a new project.我是 Azure 函数的新手,正在尝试做一个新项目。 I've run into some issues when my function takes too long to process the data.
当我的 function 处理数据的时间过长时,我遇到了一些问题。 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.
我正在使用 BlobTrigger,每次有人上传新的 Blob 时,我都需要 Blob 中的数据来使用其他 Microsoft 服务分析某些内容。
Here is the code for my Azure Function:这是我的 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. function 正常运行,但有时会超时,如果 blob 包含大量数据,则会失败。 How can I avoid timeouts and wait for the whole process to complete using Azure Functions?
如何使用 Azure 函数避免超时并等待整个过程完成?
Note: I've read about Azure Durable Functions but have a hard time understanding how to implement them and use them in my code.注意:我已阅读有关 Azure 持久函数的信息,但很难理解如何在我的代码中实现和使用它们。
How can I avoid timeouts and wait for the whole process to complete using Azure Functions?
如何使用 Azure 函数避免超时并等待整个过程完成?
As you not mentioned which hosting plan using for the Azure Function App.正如您没有提到 Azure Function 应用程序使用哪个托管计划。 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.
但是,如果请求处理需要很长时间并给出超时异常,则更改为 Azure Function App 的高级或专用托管计划。
I've read about Azure Durable Functions but have a hard time understanding how to implement them and use them in my code.
我已经阅读了有关 Azure 持久函数的信息,但很难理解如何在我的代码中实现它们并使用它们。
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.两个功能 - 一个用于 Durable Function 中的 Orchestrator,另一个用于 Blob 触发器,用于启动/触发运行持久协调器 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 :耐用 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. function 应尽快完成,您应该将长任务逻辑放入由队列消息触发的不同解决方案中,因此 Azure ZC1C425268E68385D1AB5074C17A94F14 立即将消息排入队列并完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.