如何识别azure函数执行是重试?

velaa5lx  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(101)
[FunctionName("Function1")]
    [ExponentialBackoffRetry(-1, "00:00:03", "00:00:30")]      
    public static async Task Run([EventHubTrigger("test", Connection = "EventHubConnection", 
        ConsumerGroup = "eventprocessor")] EventData[] events,
        ILogger log)
    { }

上面的重试策略会让“EventHubTrigger”Azure函数在发生未处理的异常时重试。如果是这样,如何识别函数的当前执行是“重试”执行还是“正常”执行,即下一次批处理执行?

kb5ga3dv

kb5ga3dv1#

我尝试了下面的代码来识别Azure函数重试逻辑与***ExponentialBackoffRetry***方法,并获取当前上下文:-

我的Function1.cs:-

using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace FunctionApp6
{
    public static class Function1
    {
        [Function("Function1")]
        [ExponentialBackoffRetry(5, "00:00:10", "00:10:00")]
        public static async Task Run([EventHubTrigger("siliconeventhub", Connection = "EventHubConnection")] EventData[] events, RetryContext retryContext, ILogger log)
        {
            if (retryContext != null && retryContext.RetryCount > 0)
            {
                log.LogInformation($"Retry #{retryContext.RetryCount}");
            }
            else
            {
                log.LogInformation("Normal execution");
            }

            foreach (EventData eventData in events)
            {
                try
                {
                    byte[] bytes = eventData.Body.ToArray();
                    string messageBody = Encoding.UTF8.GetString(bytes);
                    log.LogInformation($"Eventhub got a message: {messageBody}");
                }
                catch (Exception e)
                {
                    log.LogError($"Error processing message: {e.Message}");
                    throw;
                }
            }
        }
    }
}

具有完全限定扩展名的替代代码:-

using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace FunctionApp6
{
    public static class Function1
    {
        [Function("Function1")]
        [Microsoft.Azure.Functions.Worker.ExponentialBackoffRetry(5, "00:00:10", "00:10:00")]
        public static async Task Run([Microsoft.Azure.Functions.Worker.EventHubTrigger("siliconeventhub", Connection = "EventHubConnection")] EventData[] events, RetryContext retryContext, ILogger log)
        {
            if (retryContext != null && retryContext.RetryCount > 0)
            {
                log.LogInformation($"Retry #{retryContext.RetryCount}");
            }
            else
            {
                log.LogInformation("Normal execution");
            }

            foreach (EventData eventData in events)
            {
                try
                {
                    byte[] bytes = eventData.Body.ToArray();
                    string messageBody = Encoding.UTF8.GetString(bytes);
                    log.LogInformation($"C# Event Hub got a  message: {messageBody}");
                }
                catch (Exception e)
                {
                    log.LogError($"Error processing message: {e.Message}");
                    throw;
                }
            }
        }
    }
}

这里,如果重试上下文小于零或NULL,则视为正常执行,大于零或NULL则视为重试执行。

输出:-

相关问题