Azure耐用功能具有链接和平行工作流程

发布于 2025-02-08 19:36:43 字数 2048 浏览 1 评论 0原文

在我的项目中,大约有6个Azure函数,这些功能是根据某些JSON配置以序列运行的。

配置看起来像

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

“在此处输入图像描述” 这意味着,首先,它应该触发函数1,然后是function2和function3。当函数2和3完成的函数4都应执行时。然后,

我知道5,6在编排函数中链接,就是这样

    [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");

    }

,对于并​​行呼叫,

    [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);
    }

所以我需要触发函数1,然后当它完成2&amp; 3在等待任务时在一起。Whenall(任务);我需要将功能4和5放置为链接

,但我真的不知道如何根据我的配置将其像工作流一样使其像工作流一样。我只有一天的耐用功能...

请帮忙

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅我要高飞 2025-02-15 19:36:43

您可以动态创建所需的任务。

按序列对配置的任务进行分组,将允许您在为同一序列配置多个任务时并行运行任务。

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

(未经测试的代码,但应该接近:P)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文