繁体   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