簡體   English   中英

Azure 函數:如何使用 Blob 觸發器管理持久函數?

[英]Azure Functions: How to manage Durable Functions with Blob Triggers?

想象一下,我有一個帶有 blob 容器的存儲帳戶,它最終會上傳文件。 我想處理到達 blob 存儲的每個文件,打開它,提取和存儲信息。 絕對是一項可以適應 Durable Functions 場景的昂貴操作。

這是觸發器:

        [FunctionName("PayrollFileTrigger")]
        public static async Task Start(

         [BlobTrigger("files/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name,
         [DurableClient] IDurableOrchestrationClient starter,
         ILogger log)
        {

            string instanceId = await starter.StartNewAsync("PayrollFile_StartFunction", "payroll_file", name);

        }

...調用編排:


        [FunctionName("PayrollFile_StartFunction")]
        public async static Task<IActionResult> Run(
            [OrchestrationTrigger] IDurableOrchestrationContext context, string blobName, 

            ExecutionContext executionContext, ILogger log)
        {

            //Downloads the blob
            string filePath = 
                await context.CallActivityWithRetryAsync<string>("DownloadPayrollBlob", options, blobName);

            if (filePath == null) return ErrorResult(ERROR_MSG_1, log);

            //Extract data
            var payroll = 
                await context.CallActivityWithRetryAsync<Payroll>("ExtractBlobData", options, filePath);

           ... and so on (just a sample here) ...
         }

但有一個問題。 在測試此錯誤時,我認為這意味着我無法使用相同的 ID 啟動另一個編排:

An Orchestration instance with the status Pending already exists.



1 - 因此,如果我將許多文件推送到觸發器正在“偵聽”的容器中,在短時間內,編排將忙於其中之一並忽略其他進一步的事件?

2 - 編排何時會擺脫pending狀態? 它會自動發生嗎?

3 - 我應該為每個要處理的文件創建一個新的編排實例嗎? 我知道你可以省略instanceId參數,所以它是隨機生成的,永遠不會與已經啟動的參數發生沖突。 但是,這樣做安全嗎? 我如何管理它們並確保它們會在某個時候完成?

string instanceId = await starter.StartNewAsync("PayrollFile_StartFunction", "payroll_file", name);

第二個參數是 instanceId,它必須是 unique

相反,請嘗試:

string instanceId = await starter.StartNewAsync("PayrollFile_StartFunction", input: name);

根據您的需要,您可能希望每個文件只有 1 個持久實例。 微軟聲明你應該

對實例 ID 使用隨機標識符。 當您跨多個 VM 擴展協調器功能時,隨機實例 ID 有助於確保均衡的負載分配。 使用非隨機實例 ID 的正確時間是當 ID 必須來自外部源時,或者當您正在實現單例協調器模式時。

https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-instance-management?tabs=csharp#start-instances

在您的特定情況下,我會說您可以不提供instanceId自己,並且可能記錄生成的instanceId或將其與有關啟動編排的文件的信息一起寫入存儲解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM