gpt4 book ai didi

c# - Azure Durable 编排功能触发两次

转载 作者:行者123 更新时间:2023-12-03 19:23:27 27 4
gpt4 key购买 nike

我正在尝试实现 Azure Durable Function 工作流。

每隔 6 分钟,我就有一个 Azure TimerTrigger 函数调用一个 Azure 编排函数 (OrchestrationTrigger),该函数又启动许多事件函数 (ActivityTrigger)。

然而,有时,Orchestration 函数会在几秒钟内被调用两次!这是一个大问题,因为我的事件函数不是幂等的!

下面是我的代码是如何被调用的。

定时器触发功能:

[FunctionName("StartupFunc")]
public static async Task Run([TimerTrigger("0 */6 * * * *", RunOnStartup = true, UseMonitor = false)]TimerInfo myStartTimer, [OrchestrationClient] DurableOrchestrationClient orchestrationClient, TraceWriter log)
{
List<OrchestrationModel> ExportModels = await getData();

string id = await orchestrationClient.StartNewAsync("OrchestratorFunc", ExportModels);
}

编排功能:
[FunctionName("OrchestratorFunc")]
public static async Task<string> TransformOrchestration([OrchestrationTrigger] DurableOrchestrationContext context, TraceWriter log)
{
var dataList = context.GetInput<List<OrchestrationModel>>();
var tasks = new List<Task>();

foreach (var data in dataList)
{
tasks.Add(context.CallActivityAsync<string>("TransformToSql", new TransformModel(data));
}
await Task.WhenAll(tasks);
}

事件功能:
[FunctionName("TransformToSql")]
[public static async Task<string> RunTransformation([ActivityTrigger] DurableActivityContext context, TraceWriter log)
{
TransformModel = context.GetInput<TransformModel>();

//Do some work with TransformModel
}

最佳答案

随着 Durable Function 框架的重放,编排功能将更频繁地运行。你看到你的事件函数也被再次触发了吗?如果是这样,那么这确实是一种奇怪的行为。

耐用功能使用 storage queues和表来控制编排的流程和捕获状态。

每次触发编排功能时(通过从控制队列接收消息),它将重播编排。您可以使用 IsReplaying 在代码中进行检查DurableOrchestrationContext 上的属性(property).

if (!context.IsReplaying)
{
// this code will only run during the first execution of the workflow
}

不要使用 IsReplaying确定运行事件,但将其用于记录/调试目的。

当达到事件功能时,会将一条消息放入工作项队列。然后,Durable Functions 框架将查看历史记录表以确定此事件函数之前是否已经运行过(使用给定的参数)。如果是,则它不会再次运行事件功能,它将继续执行其余的编排功能。

Durable Functions 的检查点和重放功能仅在编排代码具有确定性时才有效。因此,切勿使用基于 DateTime 或随机数的决策。

更多信息: https://docs.microsoft.com/en-us/azure/azure-functions/durable-functions-checkpointing-and-replay

关于c# - Azure Durable 编排功能触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52288577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com