当前位置: 首页 > 知识库问答 >
问题:

Azure Durable Function - Do While in Orchestrator

邢皓
2023-03-14

我有一个业务流程协调程序调用一个活动函数来处理客户 ID 此活动的输出返回错误 ID(如果有的话),我希望通过再次执行活动来重新处理这些 Id,直到没有错误 ID(输出为 null)。

为管弦乐队准备一个do循环是好的做法吗?我如何在每次执行活动之前包括5分钟的延迟?

public static async Task<string> RunOrchestrator(
        [OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
    {
        log = context.CreateReplaySafeLogger(log);
        
        dynamic errorOutput = null;
        dynamic processCustomers = context.GetInput<dynamic>();
        log = context.CreateReplaySafeLogger(log);

        do
        {
            log.LogInformation("Calling activity");
            errorOutput = await context.CallActivityAsync<dynamic>("GetCSPCustomerLicenses_Activity", processCustomers);

            //Get customers to process from error object                   
            processCustomers = errorOutput;
            
           //Wait 5 minutes - how do I achieve this ?

        } while (errorOutput != null);

        return "Success";
    }

共有1个答案

鄂曦之
2023-03-14

也许你可以使用持久定时器来延迟执行,请先参考持久函数(Azure函数)中的定时器:

持久函数提供持久定时器,用于在orchestrator函数中实现延迟或设置异步操作的超时。持久计时器应用于编排器函数,而不是线程。睡眠和任务。延迟(C#),或setTimeout()和setInterval()(JavaScript),或时间。sleep()(Python)。

这是延迟使用的代码示例:

public static async Task<string> RunOrchestrator(
        [OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
    {
        log = context.CreateReplaySafeLogger(log);
        
        dynamic errorOutput = null;
        dynamic processCustomers = context.GetInput<dynamic>();
        log = context.CreateReplaySafeLogger(log);

        do
        {
            log.LogInformation("Calling activity");
            errorOutput = await context.CallActivityAsync<dynamic>("GetCSPCustomerLicenses_Activity", processCustomers);

            //Get customers to process from error object                   
            processCustomers = errorOutput;
            
            //Wait 5 minutes - how do I achieve this ?
            DateTime deadline = context.CurrentUtcDateTime.Add(TimeSpan.FromMinutes(5));
            await context.CreateTimer(deadline, CancellationToken.None);
        } while (errorOutput != null);

        return "Success";
    }
 类似资料:

相关问答

相关文章

相关阅读