简体   繁体   中英

Azure Durable Function - Do While in Orchestrator

I have an orchestrator that calls an activity function to process customer Id's The output of this activity returns error ID's (if there are any) and I wish to reprocess these Id's by executing the activity again until there are no error id's (output is null).

Is it good practice to have a do loop for an orchestrator? How do I include a 5 min delay before each time the activity gets executed?

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";
    }

Maybe you can use durable timers for delaying execution, please refer to Timers in Durable Functions (Azure Functions) first:

Durable Functions provides durable timers for use in orchestrator functions to implement delays or to set up timeouts on async actions. Durable timers should be used in orchestrator functions instead of Thread.Sleep and Task.Delay (C#), or setTimeout() and setInterval() (JavaScript), or time.sleep() (Python).

This is a code sample for delay usage:

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";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM