Azure Durable functions with Chaining and parallel workflow

Viewed 102

In my project there are around 6 azure functions which are required to run in a sequence based on some JSON configuration.

The Configuration looks like this

{
  "configurations": [
    {
      "Name": "Function1",
      "Sequence": 1
    },
    {
      "Name": "Function2",
      "Sequence": 2
    },
    {
      "Name": "Function3",
      "Sequence": 2
    },
    {
      "Name": "Function4",
      "Sequence": 3
    },
    {
      "Name": "Function5",
      "Sequence": 4
    },
    {
      "Name": "Function6",
      "Sequence": 5
    }
  ]
}

enter image description here Which means, first it should trigger function1, then function2 and function3. when both function2 and 3 completed function 4 should execute.. then 5,6

I know for chaining in orchestration function it is like this

    [FunctionName(nameof(FunctionChaining))]
    public static async Task<List<string>> FunctionChainingRunOrchestrator(
        [OrchestrationTrigger] 
        IDurableOrchestrationContext context)
    {
        // Serial calls
        await context.CallActivityAsync<string>(
            nameof(Function1), "param1");            
        await context.CallActivityAsync<string>(
            nameof(Function2), "param2");
        await context.CallActivityAsync<string>(
            nameof(Function3), "param3");

    }

and for parallel calls

    [FunctionName(nameof(parallelcalls))]
    public static async Task<List<string>> ParallelOrchestrator(
        [OrchestrationTrigger]
        IDurableOrchestrationContext context)
    {
        // Parallel calls
        var tasks = new List<Task<string>>();
        tasks.Add(context.CallActivityAsync<string>(
            nameof(Function2),
            "param1"));
        tasks.Add(context.CallActivityAsync<string>(
            nameof(Function3),
            "param2"));
        await Task.WhenAll(tasks);
    }

So I need to fire function1 then when it completes 2 & 3 together when await Task.WhenAll(tasks);, I need to fire function 4 and 5 as just chaining

But I really dont know how to make it like a workflow based on my configuration. I am with durable function just for a day...

Please help

1 Answers

You can dynamically create the tasks you need called.

Grouping the configured tasks by sequence, will allow you to run the tasks in parallel when there are multiple tasks configured for the same sequence.

class Configuration {
    public string Name { get;set; }
    public int Sequence { get;set; }
}

[FunctionName(nameof(Run))]
public static async Task<List<string>> RunConfiguredTasks(
    [OrchestrationTrigger] IDurableOrchestrationContext context
)
{
    List<Configuration> configuredTasks = ...; // however you get the configuration

    // iterate over all task groups, grouped by sequence number
    foreach (var configGroup in configuredTasks.GroupBy(t => t.Sequence))
    {
        var tasks = new List<Task<string>>();

        // iterate over all tasks in the task group
        foreach (var config in configGroup)
        {
            var taskInput = ... // get input for the task
            tasks.Add(context.CallActivityAsync<string>(config.Name, taskInput));
        }

        // wait for all tasks in the current task group
        await Task.WhenAll(tasks);
    }
}

(untested code, but it should be close :p)

Related