简体   繁体   English

具有链接和并行工作流的 Azure Durable 函数

[英]Azure Durable functions with Chaining and parallel workflow

In my project there are around 6 azure functions which are required to run in a sequence based on some JSON configuration.在我的项目中,大约有 6 个 azure 函数需要根据一些 JSON 配置按顺序运行。

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
    }
  ]
}

在此处输入图像描述 Which means, first it should trigger function1, then function2 and function3.这意味着,首先它应该触发function1,然后是function2和function3。 when both function2 and 3 completed function 4 should execute.. then 5,6当函数 2 和 3 都完成时,函数 4 应该执行.. 然后 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所以我需要触发函数1,然后当它在等待Task.WhenAll(tasks);时一起完成2和3时,我需要触发函数4和5,就像链接一样

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请帮忙

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) (未经测试的代码,但应该很接近:p)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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