首页 > 解决方案 > 是否可以创建一个可以从 EventHub 中的多个事件触发的编排持久功能?

问题描述

我想一个类似的问题可以持久功能有多个触发器吗?,但我本身并没有尝试多个触发器。

我的用例是我有一个触发持久功能的 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)

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

标签: c#azureazure-functionsazure-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 类):

[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>();
}

推荐阅读