简体   繁体   English

如何在 Azure 函数 eventthub 触发器中处理 CancellationToken

[英]How to handle CancellationToken in Azure function eventhub trigger

In an Azure function event hub trigger (v3) it takes in a cancellation token in the Run method.在 Azure 函数事件中心触发器 (v3) 中,它在Run方法中接收取消令牌。 When cancellation is signaled it means the server it shutting down.当发出取消信号时,这意味着它正在关闭的服务器。 If I send this token to for example a Get operation using httpClient it will throw a TaskCanceledException and the function will end.如果我将此令牌发送到例如使用httpClientGet操作,它将抛出TaskCanceledException并且函数将结束。

Will the events that this function was processing be sent to another instance of the function on another server or are they lost?此函数正在处理的事件是否会被发送到另一台服务器上的函数的另一个实例,还是会丢失? Should cancellation be handle in a different way?是否应该以不同的方式处理取消?

[FunctionName(nameof(MyFunction)), FixedDelayRetry(10, "00:00:15")]
public async Task RunAsync(
   [EventHubTrigger("%InEventHubName%", 
     Connection = "InEventHubConnectionString", 
     ConsumerGroup = "%ConsumerGroup%")] 
   EventData[] events,
   PartitionContext partitionContext,
   CancellationToken cancellationToken)
{
       foreach (var ev in events)
       {
            var response = await _httpClient.GetAsync("http://example.com/fetch?key=" + ev.Properties["Key"],
                             cancellationToken);
            await Process(response, cancellationToken);
       }
}

Will the events that this function was processing be sent to another instance of the function on another server or are they lost?此函数正在处理的事件是否会被发送到另一台服务器上的函数的另一个实例,还是会丢失?

They are lost : 他们迷路了

Unhandled exceptions may cause you to lose messages.未处理的异常可能会导致您丢失消息。 Executions that result in an exception will continue to progress the pointer.导致异常的执行将继续执行指针。


Should cancellation be handle in a different way?是否应该以不同的方式处理取消?

You could choose to ignore cancellation.您可以选择忽略取消。 That may be best for this kind of situation.对于这种情况,这可能是最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM