如何延迟事件触发/接收

huangapple go评论80阅读模式
英文:

How to postponed event trigger/receive

问题

SUM:如何在Azure事件网格中推迟事件触发或事件接收?

我正在设计一个系统,需要对低频OBJECT状态(创建、启动、检查长时间启动状态、结束)做出反应。看起来适合使用事件处理。我想使用Azure函数来实现它...问题是,我需要对处于STARTED状态的OBJECT做出反应,而这些对象在STARTED状态下会持续10分钟(可配置)。我希望在事件发生后的10分钟才做出反应。如何延迟事件触发?如何安排事件触发?如何等待事件?
我正在寻找不会消耗我付费资源(函数处理时间)的解决方案。有什么想法可以解决这个延迟问题吗?谢谢。

函数1:

  • 定时触发(轮询)
  • 检查条件
    • 可能会创建OBJECT-created事件

函数2:

  • HTTP触发
  • 用户从UI启动OBJECT
    • 创建OBJECT-started事件,但是从现在起延迟10分钟触发它?(在函数代码中使用睡眠/定时器会在10分钟内消耗资源,成本过高)

函数3:???

  • ???
  • 检查状态并检测10分钟延迟
    • 触发OBJECT-10min-started事件

...

英文:

SUM: how to postponed event fire or event receive in azure event grid?

I designing system which needs to react to low frequency OBJECT states (create, start, check-long-time-in-started-state, end). It looks like candidate to event processing. I would like to implement it with azure functions ...
Problem is that I need to react to OBJECT which are 10 minutes (configurable) in STARTED state. To react 10 minutes AFTER a event happend. How to delay event fire? How to schedule event fire? How to wait for event ?
Iam looking for solution which will not consume my paid resources (function processing time).
Any idea how to solve this posponing ? Thank you.

Function 1:

  • time triggered (polling)
  • check condition
    • could create OBJECT-created event

Function 2:

  • http triggered
  • user start OBJECT from UI
    • create OBJECT-started event but fire it 10 minutes from NOW ? (to sleep/timer in function code would consume resources for 10 min and ends in too high cost)

Function 3: ???

  • ???
  • check states and detect 10 minutes delay
    • fire OBJECT-10min-started event

...

答案1

得分: 1

> 在事件发生后的10分钟后触发。

首先,我要强调事件应该代表已经发生的事情,而不是将要发生的事情。事件的预期是通知发生了什么。

其次,如果您对需要发生的事情有期望,命令比事件更合适。在这种情况下,您希望在10分钟内对blob进行处理,该blob的状态实际上已经准备好进行工作。我会通过向队列(Azure Service Bus)发送延迟消息来响应该事件,并提供处理blob所需的信息。这样,您就是通过准备未来/延迟的工作项来响应事件。

英文:

> To react 10 minutes AFTER a event happend.

First I will emphasize that events should represent something that has taken place in the past and not what will take place in the future. The expectation for events is to notify about what has happened.

Second, if you have an expectation of what needs to happen, a command is better than an event. In this case, you want the work to happen in 10 minutes on the blob that would be in the state it's actually available for work. I would respond to the event by sending a delayed message to a queue (Azure Service Bus) with the information needed to work on the blob. That way you're responding to the event by preparing a future/delayed work item.

答案2

得分: 1

以下是您要翻译的内容:

如何在Azure事件网格中推迟事件触发或接收事件?

Azure事件网格(AEG)本身没有内置此功能,但可以通过在Azure服务总线(ASB)实体中使用延迟(计划)消息来轻松扩展它,就像Sean在答案中提到的那样。

下面的屏幕截图显示了“推拉结合延迟”订阅者的概念:

如何延迟事件触发/接收

事件消息被推送到ASB主题,基于其订阅规则,事件消息被转发到队列实体作为计划消息。

主题订阅需要设置以下属性:

  • ForwardTo
    队列/主题实体的名称

  • $Default Rule
    过滤器:
    1=1
    操作(例如延迟10分钟):
    SET sys.TimeToLive = '00:10:00';
    SET EnqueuedTimeUtc = sys.EnqueuedTimeUtc;
    SET ScheduledEnqueueTimeUtc = sys.ExpiresAtUtc;
    SET sys.ScheduledEnqueueTimeUtc = sys.ExpiresAtUtc;
    SET sys.Label = 'Delay';
    SET sys.TimeToLive = '01:00:00';

目标队列:
EnableDeadLetteringOnMessageExpiration = true

根据上述设置,队列中的计划消息必须在TTL(例如'01:00:00')内被消耗,否则消息将被发送到DLQ(死信队列)。有关详细信息,请参阅Sean的评论。

使用ServiceBusTrigger函数,可以像AEG订阅者一样透明地从队列中提取延迟的事件消息。

在将延迟的事件发送回AEG以进行“扇出”分发并使用“推拉结合”模式的情况下,以下示例显示了将ServiceBusTrigger实现与输出绑定到AEG自定义主题:

run.csx:

#r "Newtonsoft.Json"
#r "Microsoft.Azure.EventGrid"
#r "Microsoft.Azure.ServiceBus"

using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Azure.EventGrid.Models;
using Microsoft.Azure.ServiceBus;

public static async Task Run(Message queueItem, IAsyncCollector<EventGridEvent> outputEvents, ILogger log)
{   
    string jsontext = JToken.Parse(Encoding.UTF8.GetString(queueItem.Body)).ToString(Formatting.Indented); 
    log.LogInformation(jsontext);

    EventGridEvent eventGridEvent = JsonConvert.DeserializeObject<EventGridEvent>(jsontext);
    eventGridEvent.Topic = null;
    eventGridEvent.Subject += "/delayed";
    await outputEvents.AddAsync(eventGridEvent);

    await Task.CompletedTask;
}

function.json:

{
  "bindings": [
    {
      "name": "queueItem",
      "type": "serviceBusTrigger",
      "direction": "in",
      "queueName": "aeg",
      "connection": "rk2016_SERVICEBUS"
    },
    {
      "type": "eventGrid",
      "direction": "out",
      "name": "outputEvents",
      "topicEndpointUri": "AEG_TOPIC_XX_ENDPOINT",
      "topicKeySetting": "AEG_TOPIC_XX_KEY"
    }
  ]
}

正如您所见,为了过滤目的,已经修改了主题属性,附加了后缀*/delayed*,以避免循环等。

英文:

> how to postponed event fire or event receive in azure event grid?

The Azure Event Grid (AEG) doesn't have built in this feature, however is very easy to extent it using an Azure Service Bus (ASB) entities for delayed (scheduled) message like is mentioned in the answer by Sean.

The following screen snippet shows a concept of the Push-and-Pull with delay subscriber:

如何延迟事件触发/接收

The event message is pushed into the ASB topic and based on its subscription rule, the event message is forwarded to the queue entity as a scheduled message.

The topic subscription requires to setup the following properties:

  • ForwardTo

    name of the queue/topic entity
    
  • $Default Rule

    Filter:

    1=1 
    

    Action (example for 10 minutes):

    SET sys.TimeToLive = &#39;00:10:00&#39;;
    SET EnqueuedTimeUtc = sys.EnqueuedTimeUtc;
    SET ScheduledEnqueueTimeUtc = sys.ExpiresAtUtc;
    SET sys.ScheduledEnqueueTimeUtc = sys.ExpiresAtUtc;
    SET sys.Label = &#39;Delay&#39;;
    SET sys.TimeToLive = &#39;01:00:00&#39;;
    

Destination queue:

 EnableDeadLetteringOnMessageExpiration = true

Based on the above setting, the scheduled message in the queue must be consumed within the TTL such as '01:00:00', otherwise the message is sent to the DLQ. More details in the Sean's comment.

Using the ServiceBusTrigger function, the delayed event message can be pulled from the queue in the transparent manner like AEG subscriber.

In the case, when the delayed event is sent back to the AEG for Fan-Out distributing and using a Push-and-Push pattern, the following example shows this implementation of the ServiceBusTrigger with an output binding to the AEG custom topic:

run.csx:

#r &quot;Newtonsoft.Json&quot;
#r &quot;Microsoft.Azure.EventGrid&quot;
#r &quot;Microsoft.Azure.ServiceBus&quot;


using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Azure.EventGrid.Models;
using Microsoft.Azure.ServiceBus;


public static async Task Run(Message queueItem, IAsyncCollector&lt;EventGridEvent&gt; outputEvents, ILogger log)
{   
    string jsontext = JToken.Parse(Encoding.UTF8.GetString(queueItem.Body)).ToString(Formatting.Indented); 
    log.LogInformation(jsontext);

    EventGridEvent eventGridEvent = JsonConvert.DeserializeObject&lt;EventGridEvent&gt;(jsontext);
    eventGridEvent.Topic = null;
    eventGridEvent.Subject += &quot;/delayed&quot;;
    await outputEvents.AddAsync(eventGridEvent);

    await Task.CompletedTask;
}

function.json:

{
  &quot;bindings&quot;: [
    {
      &quot;name&quot;: &quot;queueItem&quot;,
      &quot;type&quot;: &quot;serviceBusTrigger&quot;,
      &quot;direction&quot;: &quot;in&quot;,
      &quot;queueName&quot;: &quot;aeg&quot;,
      &quot;connection&quot;: &quot;rk2016_SERVICEBUS&quot;
    },
    {
      &quot;type&quot;: &quot;eventGrid&quot;,
      &quot;direction&quot;: &quot;out&quot;,
      &quot;name&quot;: &quot;outputEvents&quot;,
      &quot;topicEndpointUri&quot;: &quot;AEG_TOPIC_XX_ENDPOINT&quot;,
      &quot;topicKeySetting&quot;: &quot;AEG_TOPIC_XX_KEY&quot;
    }
  ]
}

As you can see, the subject property has been modified with suffix /delayed for filtering purposes such as avoiding a looping, etc.

huangapple
  • 本文由 发表于 2020年9月17日 15:20:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63933101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定