首页 > 解决方案 > Azure 函数,延迟

问题描述

我有一个 CRM 系统,当添加联系人时,我想将他们添加到会计系统中。

我在 CRM 系统中设置了一个 webhook,将联系人传递给 Azure 函数。Azure 函数连接到会计系统 API 并在那里创建它们。

在将用户添加到会计系统之前,我还需要做一些其他处理。

在收到 webhook 后,我需要大约 5 分钟的延迟才能将用户添加到会计系统。

我宁愿不在 Azure 函数中添加暂停或延迟语句,因为有超时限制,而且它是一个消耗计划,所以我希望每个函数都能快速行动。

我正在使用 Powershell 内核。

服务总线队列是做到这一点的最佳方式吗?

标签: azureazure-functionspowershell-core

解决方案


为此,您可以在 Durable Function中使用Timer 。然后你就不需要像队列这样的额外组件了。耐用的功能就是您所需要的。例如(警告:未编译此):

注意:Durable Functions确实支持 powershell,但我不支持 ;-) 所以下面的代码是为了理解这个概念。

[FunctionName("Orchestration_HttpStart")]
public static async Task<HttpResponseMessage> HttpStart(
  [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
  [DurableClient] IDurableOrchestrationClient starter,
  ILogger log)
{
  // Function input comes from the request content.
  string content = await req.Content.ReadAsStringAsync();
  string instanceId = await starter.StartNewAsync("Orchestration", content);

  log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
  return starter.CreateCheckStatusResponse(req, instanceId);
}

[FunctionName("Orchestration")]
public static async Task Run(
    [OrchestrationTrigger] IDurableOrchestrationContext context)
{
    var requestContent = context.GetInput<string>();

    DateTime waitAWhile = context.CurrentUtcDateTime.Add(TimeSpan.FromMinutes(5));
    await context.CreateTimer(waitAWhile, CancellationToken.None);
    await context.CallActivityAsync("ProcessEvent", requestContent);
}

[FunctionName("ProcessEvent")]
public static string ProcessEvent([ActivityTrigger] string requestContent, ILogger log)
{
  // Do something here with requestContent

  return "Done!";
}

我宁愿不在 Azure 函数中添加暂停或延迟语句,因为有超时限制,而且它是一个消耗计划,所以我希望每个函数都能快速行动。

计时器引入的 5 分钟延迟不会算作活动时间,因此您不会用完这些分钟的消费计划时间。


推荐阅读