【问题标题】:Azure Function, delayAzure 函数,延迟
【发布时间】:2021-03-03 12:53:29
【问题描述】:

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

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

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

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

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

我正在使用 Powershell 内核。

服务总线队列是最好的方法吗?

【问题讨论】:

  • “需要大约 5 分钟的延迟”。在我看来,您触发了网络挂钩以快速运行。当所有处理完成时触发它
  • 我无法控制 webhook 的时间,当 CRM 中的内置函数添加联系人时,它会立即由​​ CRM 触发

标签: azure azure-functions powershell-core


【解决方案1】:

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

注意:Durable Functions do support 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 Function 中添加暂停或延迟语句,因为有超时限制,而且这是一个消耗计划,所以我希望每个函数都能快速行动。

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

【讨论】:

  • 我必须使用 powershell,Accounting 系统的“API”基于我需要加载以进行交互的 Powershell 模块。
  • @user3565039 好吧,正如我所说,您可以使用 powershell 来完成。我的答案中的代码基于 c#,但确实显示了这种带有计时器的持久函数在概念上是如何工作的。
【解决方案2】:

服务总线队列是最好的方法吗?

您可以使用它,但 Azure 存储队列对于您的方案来说更便宜。

您可以做的是创建一个时间触发函数 (* */5 * * * *) 并检查队列中的消息。如果执行与消息创建时间之间的时间大于分钟,则处理并完成该消息,否则,不完成该消息,它将返回到队列中以进行下一次执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多