【问题标题】:Is it possible to create an orchestration durable function that can be triggered from multiple events from an EventHub?是否可以创建一个可以从 EventHub 中的多个事件触发的编排持久功能?
【发布时间】:2020-02-26 04:44:07
【问题描述】:

我猜想与Can durable functions have multiple triggers? 有类似的问题,但我本身并没有尝试多个触发器。

我的用例是我有一个触发持久功能的 EventHub。我想在负载中侦听包含特定模式的第 N 个事件以获取特定 id(也在负载中)

当收到第n个事件时,我可以轻松地启动另一个活动,但我可以解决的是如何在开始时做有状态的位?

如果持久函数不能支持这一点,Azure 中还有哪些其他选项可以做类似的事情?

event       id      event name
1           1       login
2           1       navigate
3           2       login
4           2       do something
5           1       do something of interest
6           1       do something of interest (again, this is what I was to trigger the activity on)

此信息当前来自事件中心并触发我的功能。

【问题讨论】:

  • 您不能只过滤订阅级别的事件吗?在函数运行时你不能真正接收到新事件,因为新事件会产生新函数
  • 我不太了解您的用例,但如果您需要状态过滤,您是否考虑过 Azure 流分析?

标签: c# azure azure-functions azure-durable-functions


【解决方案1】:

这可能是Durable Entities(Durable Functions 的新功能)的一个很好的用例。您的实体 ID 可以从您的事件负载中的 ID 派生。通过 EventHub 触发函数,您可以在每次看到您正在寻找的模式时向特定实体发送信号。持久实体将在第一个事件到达时自动创建,并且可以在采取某些操作之前简单地计算事件的数量。

例如,这里是事件中心触发函数:

[FunctionName("ProcessEvents")]
public static async Task ProcessEvents(
    [EventHubTrigger("event-source")] EventData input,
    [DurableClient] IDurableClient client)
{
    if (IsOfInterest(input))
    {
        var id = new EntityId("MyDetector", (string)input.Properties["ID"]);
        await client.SignalEntityAsync(id, nameof(MyDetector.Process), input);
    }

    // ...
}

...这里是实体函数(实现为.NET class):

[JsonObject(MemberSerialization.OptIn)]
public class MyDetector
{
    [JsonProperty]
    public int CurrentEventCount { get; set; }

    public void Process(EventData input) 
    {
        // Take some action if this event happens N or more times
        if (++this.CurrentEventCount >= 10)
        {
            TakeSomeAction(input);

            // reset the counter
            this.CurrentEventCount = 0;
        }
    }

    [FunctionName(nameof(MyDetector))]
    public static Task Run([EntityTrigger] IDurableEntityContext ctx)
        => ctx.DispatchAsync<MyDetector>();
}

【讨论】:

  • 这很有帮助。给了我很多思考。我可以根据旅程和规则有效地对每个实体进行唯一的键控。您可以自动使实体过期吗?你能触发生命周期事件吗?我可以找出来。我想如果不是,你总是可以使用定时触发器
猜你喜欢
  • 2022-11-01
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
  • 2019-03-05
相关资源
最近更新 更多