带有 serviceBusTrigger 的 Azure 功能在 DrainMode/stop 后 5 分钟重新启动

问题描述 投票:0回答:1

我有一个 Azure 函数(消费者计划),应该在服务总线事件上触发,但我在日志中看到 Aazure 函数已停止并耗尽,并且 azure 函数需要 5 分钟才能启动,为什么该函数需要这么长时间当队列中有消息时重新启动

11/3/2024, 3:32:21.342 PM DrainMode mode enabled

11/3/2024, 3:32:21.343 PM Calling StopAsync on the registered listeners

11/3/2024, 3:32:21.346 PM Stopping the listener 'Microsoft.Azure.WebJobs.ServiceBus.Listeners.ServiceBusListener' for function '**
 
11/3/2024, 3:32:21.389 PM Stopped the listener 'Microsoft.Azure.WebJobs.ServiceBus.Listeners.ServiceBusListener' for function '******
 
11/3/2024, 3:32:21.391 PM Call to StopAsync complete, registered listeners are now stopped
 
11/3/2024, 3:32:21.426 PM Stopped the listener 'Microsoft.Azure.WebJobs.ServiceBus.Listeners.ServiceBusListener' for functio 
 
11/3/2024, 3:32:21.428 PM Call to StopAsync complete, registered listeners are now stopped

11/3/2024, 3:38:38.016 PM Initializing Warmup Extension.
 
11/3/2024, 3:38:38.031 PM Initializing Host. OperationId: '*****'.
 
11/3/2024, 3:38:38.032 PM Host initialization: ConsecutiveErrors=0, StartupCount=3, Oper 9
 
11/3/2024, 3:38:38.056 PM ApplicationInsightsLoggerOptions {
azure function
1个回答
0
投票

服务总线触发器 Azure Function 中的 Drain 模式是一项可避免处理消息时发生冲突的功能,请参阅我的 SO 答案

对于消费和弹性保费计划,规模控制器决定扩展行为。当规模控制器决定在负载较少时缩小工作人员规模时,将启用排水模式。这是为了避免当您需要对功能执行更新时发生冲突而不丢失任何消息或导致错误。

启用排水模式时:

  1. 该函数将停止从服务总线实体拾取新消息。
  2. 它会继续处理已经处理的消息,直到完成。
  3. 处理完所有正在进行的消息后,该功能将完全停止。

此时,它向管理 API 发送请求以进入耗尽模式,并且该实例上当前正在执行的所有函数都会收到一个可以使用以下代码处理的取消令牌。这允许函数在停止之前完成正在进行的任务,请参阅MSDOC

代码片段:

      public async Task Run(
          [ServiceBusTrigger("sbqueue", Connection = "demo")]
          ServiceBusReceivedMessage message, CancellationToken cancellationToken,
          ServiceBusMessageActions messageActions)
      {
          try
          {
                  if (cancellationToken.IsCancellationRequested)
                  {
                      _logger.LogInformation("A cancellation token was received. Taking precautionary actions.");
                  //Take precautions like noting how long you are with processing the batch
                  _logger.LogInformation("Precautionary activities --complete--.");
                  }
                  else
                  {
                  _logger.LogInformation($"Message: {message} was processed.");
                  _logger.LogInformation("Message ID: {id}", message.MessageId);
                  _logger.LogInformation("Message Body: {body}", message.Body);
                  _logger.LogInformation("Message Content-Type: {contentType}", message.ContentType);
              }
          }
          catch (Exception ex)
          {
              _logger.LogInformation($"Something unexpected happened: {ex.Message}");
          }
          await messageActions.CompleteMessageAsync(message);
      }

输出:

Functions:

Function1: serviceBusTrigger

For detailed output, run func with --verbose flag.  
[2024-11-27T12:01:41.929Z] Host lock lease acquired by instance ID '000000000000000xxx9'.  
[2024-11-27T12:01:48.096Z] Executing 'Functions.Function1' (Reason='(null)', Id=40efdb2a-6fe2-44b6-9071-8ec096e7e029)  
[2024-11-27T12:01:48.101Z] Trigger Details: MessageId: 9734a2b8ff754ecea689d9ce05ed331b, SequenceNumber: 1, DeliveryCount: 1, EnqueuedTimeUtc: 2024-11-27T12:01:44.7220000+00:00, LockedUntilUtc: 2024-11-27T12:02:44.9400000+00:00, SessionId: (null)  
[2024-11-27T12:01:48.603Z] Message: {MessageId:9734a2b8ff754ecea689d9ce05ed331b} was processed.  
[2024-11-27T12:01:48.608Z] Message ID: 9734a2b8ff754ecea689d9ce05ed331b  
[2024-11-27T12:01:48.614Z] Message Body: Hello World  
[2024-11-27T12:01:48.620Z] Message Content-Type: (null)  
[2024-11-27T12:01:48.649Z] Start processing HTTP request POST [http://127.0.0.1:64966/Settlement/Complete](http://127.0.0.1:64966/Settlement/Complete "http://127.0.0.1:64966/settlement/complete")  
[2024-11-27T12:01:48.658Z] Sending HTTP request POST [http://127.0.0.1:64966/Settlement/Complete](http://127.0.0.1:64966/Settlement/Complete "http://127.0.0.1:64966/settlement/complete")  
[2024-11-27T12:01:49.184Z] Received HTTP response headers after 506.2569ms - 200  
[2024-11-27T12:01:49.195Z] End processing HTTP request after 550.2789ms - 200  
[2024-11-27T12:01:49.257Z] Executed 'Functions.Function1' (Succeeded, Id=40efdb2a-6fe2-44b6-9071-8ec096e7e029, Duration=1242ms)
© www.soinside.com 2019 - 2024. All rights reserved.