持久功能可以有多个触发器吗? [英] Can durable functions have multiple triggers?

查看:158
本文介绍了持久功能可以有多个触发器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个持久功能,该功能每天由计时器触发器触发一次:

I have a durable function that is triggered once a day by a Timer Trigger:

[FunctionName("MyDurableFunction")]
public static async Task Run(
    [TimerTrigger("0 0 23 * * *", RunOnStartup = false)] TimerInfo myTimer,
    [OrchestrationClient] DurableOrchestrationClient starter,
    ILogger log)
{
    await starter.StartNewAsync("OrchestrationFunction", null);
}

[FunctionName("OrchestrationFunction")]
public static async Task OrchestrationFunction(
    [OrchestrationTrigger]DurableOrchestrationContext context,
    ILogger log)
{
    // do stuff
}

这很好.为了进行测试,我还希望能够通过Http触发器来触发持久功能,因此我添加了以下内容:

This works fine. For testing purposes I would also like to be able to trigger the durable function via a Http Trigger, so I added this:

[FunctionName("MyDurableFunctionHttpTrigger")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "demo")]HttpRequest req, 
    [OrchestrationClient] DurableOrchestrationClient starter, 
    ILogger log)
{
    await starter.StartNewAsync("OrchestrationFunction", null);
    return new OkObjectResult("blah");
}

在本地运行这些命令(包括http触发器或计时器触发器)将触发该函数,但是在类中同时包含这两个函数将意味着不会发生任何触发事件.可以有多种触发器类型来启动业务流程触发器吗?

Running these locally, including either the http trigger or the timer trigger will trigger the function, but including both in the class means that neither trigger events will occur. Is it possible to have multiple trigger types start an orchestration trigger?

推荐答案

我相信每个函数只能有一个触发器类型,但是建议您将所有逻辑写入一个单独的项目/程序集中,然后仅引用该程序集并通过参数调用入口点,使您的函数实现简洁明了,并将执行逻辑集中在另一个项目(或同一项目中的类)上.

I believe you can only have one trigger type per function but can suggest you write all your logic in to a separate project/assembly and then just reference the assembly and call the entry point via parameters, keeping your function implementation clean and simple and centralising the execution logic in another project (or classes within the same project).

在您的代码上,您应该具有Orchestrator和Activity函数,因此您可以编写一个Activity函数来执行工作,并从两个Orchestrators进行调用.关于持久功能的指导是保持编排人员的清洁和简单管理,即编排,将工作转移到活动中.

On your code, you should have Orchestrator and Activity functions, so you could write one Activity function to do the work and call it from two orchestrators. The guidance on Durable Functions is to keep the orchestrator clean and simple managing just that - the orchestration, offloading the work to the Activities.

我建议您查看持久监视器模式,以满足您基于计时器的要求,然后查看用于HTTP触发器的HTTP API .

I recommend you look at the durable monitor pattern for your timer based requirement and look at the HTTP APIs for HTTP Triggers.

这篇关于持久功能可以有多个触发器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆