【问题标题】:How to pass a POST parameter to a Durable Function and then pass this param to a Timer Triggered function如何将 POST 参数传递给 Durable Function,然后将此参数传递给 Timer Triggered 函数
【发布时间】:2019-01-28 16:13:27
【问题描述】:
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;

namespace mynamespace
{
    public static class myfuncclass
    {
        [FunctionName("mydurablefunc")]
        public static async void Run([OrchestrationTrigger] DurableOrchestrationContextBase context)
        {
            await context.CallActivityAsync<string>("timer", "myparam");
        }

        [FunctionName("timer")]
        public static void RunTimer([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log)
        {
            if (myTimer.IsPastDue)
            {
                log.Info("Timer is running late!");
            }
            log.Info($"Timer trigger function executed at: {DateTime.Now}");
        }
    }
}

我希望我的 Durable Function 启动另一个基于计时器的功能,该功能必须每 5 分钟重新发生一次。到目前为止一切顺利,这是我的代码。现在我希望这个活动在我使用 HTTP 调用(POST、GET 等)调用持久函数时开始(我更喜欢使用 Queue,但不知道如何做)并向它传递一个参数,然后它传递这个参数到被调用的函数。怎么样?

【问题讨论】:

  • 我认为您不能“启动”计时器触发功能。它将始终以 cron-string 设置的间隔运行。
  • 好的,我可以让它一直运行,但是如何传递参数给它呢?
  • 对于计时器触发函数,最好的方法是让函数查询其他服务、队列或数据库以检索处理所需的数据。也许您应该改用 HttpTriggered 函数?见docs.microsoft.com/en-us/azure/azure-functions/…

标签: .net azure http-post azure-functions azure-durable-functions


【解决方案1】:

您不能“启动”定时器触发器。 Orchestrator 只能管理 Activity 函数,如下所示:

[FunctionName("mydurablefunc")]
public static async void Run([OrchestrationTrigger] DurableOrchestrationContextBase context)
{
    for (int i = 0; i < 10; i++)
    {
        DateTime deadline = context.CurrentUtcDateTime.Add(TimeSpan.FromMinutes(5));
        await context.CreateTimer(deadline, CancellationToken.None);
        await context.CallActivityAsync<string>("myaction", "myparam");
    }
}

[FunctionName("myaction")]
public static Task MyAction([ActivityTrigger] string param)
{
    // do something
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 2021-04-18
    • 1970-01-01
    相关资源
    最近更新 更多