天天看點

【Azure 應用服務】Azure Function HTTP 觸發後, 230秒就逾時。而其他方式觸發的Function, 執行5分鐘後也逾時,如何調整逾時時間?

問題描述

Azure Function HTTP 觸發後, 230秒就逾時,而其他方式觸發的Function, 執行5分鐘後也逾時,如何調整逾時時間?

HTTP觸發的Function 報錯500 - The Request timed out 消息截圖:

【Azure 應用服務】Azure Function HTTP 觸發後, 230秒就逾時。而其他方式觸發的Function, 執行5分鐘後也逾時,如何調整逾時時間?

問題分析

查閱官方文檔,對函數應用逾時持續時間有詳細介紹:最新的3.X版本的預設值根據Function計劃的類型不同而不同,預設的最少為5分鐘,最大30分鐘。消耗計劃的Function最大可調整為10分鐘,而其他兩種無限制。

但是,非常非常重要的一點是:如果Function是HTTP觸發類型。它的響應時間最大最大就是230秒。 這是因為 Azure 負載均衡器的預設空閑逾時就是230秒,作為PaaS服務的Azure Funciton (相同的還有App Service)無法改動。

【Azure 應用服務】Azure Function HTTP 觸發後, 230秒就逾時。而其他方式觸發的Function, 執行5分鐘後也逾時,如何調整逾時時間?

(Source :  https://docs.microsoft.com/zh-cn/azure/azure-functions/functions-scale#function-app-timeout-duration)

問題解決

1) 進入Function App的Azure 門戶頁面: Function App - Microsoft Azure 由世紀互聯營運

2) 點選“App Service Editor “, 進入源代碼檢視頁面,選擇 host.json。 修改 functionTimeout 内容。如沒有  functionTimeout,則根據以下格式自行添加。

【Azure 應用服務】Azure Function HTTP 觸發後, 230秒就逾時。而其他方式觸發的Function, 執行5分鐘後也逾時,如何調整逾時時間?

3)如果是 HTTP觸發的函數,而且其Function所運作的任務會處理很長時間,建議使用Function的另一種模式 【Durable Function 異步模式】, 或者通過代碼的方式,自行解決延遲響應傳回。

Durable Function 異步模式

異步 HTTP API 模式解決了使用外部用戶端協調長時間運作的操作的狀态時出現的問題。 實作此模式的一種常用方式是讓 HTTP 終結點觸發長時間運作的操作。 然後,将用戶端重定向到某個狀态終結點(Location),用戶端可輪詢該終結點(Location),以了解操作是何時完成的。

【Azure 應用服務】Azure Function HTTP 觸發後, 230秒就逾時。而其他方式觸發的Function, 執行5分鐘後也逾時,如何調整逾時時間?

Durable Functions 預設支援HTTP API 異步模式,可以簡化甚至消除為了與長時間運作的函數執行進行互動而需要編寫的代碼。

  • C#代碼可以參考:使用 C# 建立你的第一個持久函數(https://docs.microsoft.com/zh-cn/azure/azure-functions/durable/durable-functions-create-first-csharp?pivots=code-editor-vscode)
  • JavaScript代碼參考:使用 JavaScript 建立你的第一個持久函數(https://docs.microsoft.com/zh-cn/azure/azure-functions/durable/quickstart-js-vscode)

啟動執行個體後,該擴充會公開 Webhook HTTP API 用于查詢業務流程協調程式函數的狀态。

【Azure 應用服務】Azure Function HTTP 觸發後, 230秒就逾時。而其他方式觸發的Function, 執行5分鐘後也逾時,如何調整逾時時間?

Durable Function 異步模式Demo:

根據入門文檔建立的簡易代碼中,主要有三個部分:

1)第一個函數 HttpStart, Demo中調用名為 DurableFunctionsOrchestrationCSharp1_HttpStart, 它是Durable Function函數的觸發入口,他會啟動真正的業務代碼,并且傳回一個響應URL,用于輪詢執行結果。

2)第二個函數 RunOrchestrator,Demo中的調用名為 DurableFunctionsOrchestrationCSharp1, 它是一個把需要執行的任務集中在一起,形成一個執行清單,當清單中的函數執行完成後,傳回結果。

3)第三個函數 SayHello, Demo中的調用名為 DurableFunctionsOrchestrationCSharp1_Hello, 它裡面是需要真正執行的業務邏輯。

PS: 為了能讓函數執行時間更長,是以在第二步中,為每個函數之間添加了100秒的延遲,這樣總的執行時間就會達到300秒。超過230秒的HTTP平台限制。

public static class DurableFunctionsOrchestrationCSharp1
    {
        [FunctionName("DurableFunctionsOrchestrationCSharp1")]
        public static async Task<List<string>> RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context)
        {
            var outputs = new List<string>();

            // Replace "hello" with the name of your Durable Activity Function.            
            outputs.Add(await context.CallActivityAsync<string>("DurableFunctionsOrchestrationCSharp1_Hello", "Tokyo"));
            Task.Delay(100000).Wait();
            outputs.Add(await context.CallActivityAsync<string>("DurableFunctionsOrchestrationCSharp1_Hello", "Seattle"));
            Task.Delay(100000).Wait();
            outputs.Add(await context.CallActivityAsync<string>("DurableFunctionsOrchestrationCSharp1_Hello", "London"));
            Task.Delay(100000).Wait();
            // returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"]
            return outputs;
        }

        [FunctionName("DurableFunctionsOrchestrationCSharp1_Hello")]
        public static string SayHello([ActivityTrigger] string name, ILogger log)
        {


            log.LogInformation($"Saying hello to {name}.");
            return $"Hello {name}!";
        }

        [FunctionName("DurableFunctionsOrchestrationCSharp1_HttpStart")]
        public static async Task<HttpResponseMessage> HttpStart(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
            [DurableClient] IDurableOrchestrationClient starter,
            ILogger log)
        {
            // Function input comes from the request content.
            string instanceId = await starter.StartNewAsync("DurableFunctionsOrchestrationCSharp1", null);

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

            return starter.CreateCheckStatusResponse(req, instanceId);
        }
    }      

注意:.Net Core 中使用 Task.Delay(100000).Wait(); 方法來實作線程睡眠。與傳統的Thread.Sleep(1000)有一些差別。

使用VS Code建立和釋出 Durable Function 異步模式Demo 到Azure示範

【Azure 應用服務】Azure Function HTTP 觸發後, 230秒就逾時。而其他方式觸發的Function, 執行5分鐘後也逾時,如何調整逾時時間?

測試 Durable Function HTTP異步示範(使用浏覽器)

【Azure 應用服務】Azure Function HTTP 觸發後, 230秒就逾時。而其他方式觸發的Function, 執行5分鐘後也逾時,如何調整逾時時間?

參考資料

函數應用逾時持續時間: https://docs.microsoft.com/zh-cn/azure/azure-functions/functions-scale#function-app-timeout-duration

Function host.json functionTimeout :  https://docs.microsoft.com/zh-cn/azure/azure-functions/functions-host-json#functiontimeout

Durable Function 異步 HTTP API 模式: https://docs.microsoft.com/zh-cn/azure/azure-functions/durable/durable-functions-overview?tabs=csharp#pattern-3-async-http-apis

【完】

當在複雜的環境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 雲中,恰是如此!

繼續閱讀