1

I guess a similar question to Can durable functions have multiple triggers?, but I'm not attempting multiple triggers per se.

My use case is that I have an EventHub which is triggering a Durable Function. I would like to listen for the Nth event containing a specific pattern in the payload for a specific id (also in the payload)

When receiving the nth event, I can easily kick off another activity, but what I can work out is how to do the stateful bit at the start?

If durable functions can't support this, what other options are there in Azure so do something similar?

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)

This information is currently is coming from event hub and triggering my function.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
sambomartin
  • 6,663
  • 7
  • 40
  • 64
  • 1
    cant you just filter the events on the subscription level? you cant really receive new events while the function is running, since new events will spawn new functions – 4c74356b41 Feb 09 '20 at 15:40
  • I'm not really understanding your use case but if you need stateful filtering, have you look into Azure Stream Analytics? – silent Feb 09 '20 at 19:26

1 Answers1

1

This might be a good use case for Durable Entities (a newer feature of Durable Functions). The ID of your entity can be derived from the ID in your event payload. From your EventHub trigger function, you could send a signal to a particular entity every time you see the pattern you're looking for. The durable entity will be created automatically when the first event arrives, and could simply count the number of events before taking some action.

For example, here is the Event Hub trigger function:

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

    // ...
}

...and here is the entity function (implemented as a .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>();
}
Chris Gillum
  • 14,526
  • 5
  • 48
  • 61
  • this helps a lot. gives me plenty to think about. i could effective key each entity uniquely based on the journey and rule. can you auto expire entities? can you trigger on lifecycle events? i can find out. i guess you could always use a timed trigger if not – sambomartin Mar 11 '20 at 20:14