Azure Function : When Function AutoScale, 函数被多次调用

标签 azure .net-core async-await azure-functions

在Azure队列中为队列项添加单个条目,但对同一队列项多次调用函数,无法理解出了什么问题,我们在函数中使用了异步/等待,代码也是异步的,下面是Azure函数设置, enter image description here enter image description here

下面是函数代码,

public class CreateTempVMTempTablesFunction
{
    private static Container container;
    private static IProcessingFunctionService _processingFunctionService; 
    private static IAzureFunctionFailuresRepository _azureFunctionFailuresRepository;
    private static ITrackLogEventsService _trackLogEventsService;

    [FunctionName("CreateTempVMTempTablesFunction")]
    public static async Task Run([QueueTrigger("%environment-plan%" + AzureFunctionConstants.CreateTempTableQueue, Connection = "AzureWebJobsStorage")]string myQueueItem, ILogger log)
    {
        string ErrorMessage = string.Empty;
        var start = DateTime.Now;
        FunctionStatusEnum IsSuccess = FunctionStatusEnum.Success;
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem} - {start}");

        Guid tempid = new Guid(myQueueItem);
        TempVM tempVM = new TempVM();
        try
        {
            container = BusinessLogic.Helpers.SimpleInjectorWebApiInitializer.InitializeSingleton();;
           
            _processingFunctionService = container.GetInstance<IProcessingFunctionService>();
            _azureFunctionFailuresRepository = container.GetInstance<IAzureFunctionFailuresRepository>();
            _trackLogEventsService = container.GetInstance<ITrackLogEventsService>();

            tempVM = await _processingFunctionService.GetById(tempid);
            if (tempVM != null)
            {
                FunctionStatusEnum IsAlreadyPerformed = await _azureFunctionFailuresRepository.GetAzureFunctionFailureStatus(AzureFunctionConstants.CreateTempVMTempTablesFunction, tempVM.Id);

                if (IsAlreadyPerformed != FunctionStatusEnum.Success)
                {
                        ResponseData response = await _processingFunctionService.CreateTempVMTempTables(tempid);
                }
                else
                {
                    ErrorMessage = AzureFunctionConstants.FunctionAlreadyProcessed;
                }
            }
            else
            {
                ErrorMessage = AzureFunctionConstants.TempVMNotFound;
            }
        }
        catch (Exception ex)
        {
            IsSuccess = FunctionStatusEnum.Failed;
            ErrorMessage = ex.ToString();
        }
        finally
        {
            AzureFunctionFailures azureFunctionFailures = new AzureFunctionFailures()
            {
                Id = Guid.NewGuid(),
                FunctionName = AzureFunctionConstants.CreateTempVMTempTablesFunction,
                QueueItem = myQueueItem,
                ErrorMessage = ErrorMessage,
                StartTime = start,
                EndTime = DateTime.Now,
                FailureTypeId = tempid,
                FunctionStatus = IsSuccess,
                ProcessTime = (DateTime.Now - start).TotalMilliseconds,
            };
            await _azureFunctionFailuresRepository.Add(azureFunctionFailures);
        }
        log.LogInformation($"End Time : {DateTime.Now} - QueueItem {myQueueItem}");
        log.LogInformation($"Total Time : {DateTime.Now - start} - QueueItem {myQueueItem}");
    }
}

我已经检查了在队列中添加条目的代码,但只为一个队列项目添加了一个条目。 当在同一队列中为不同的队列项添加多个条目(即负载测试,我仅添加了 24 个请求)时,会发生此问题,当运行单个队列时,不会发生这种情况,我们的功能位于具有自动缩放功能的应用服务计划中

最佳答案

正如评论中提到的。只需在函数的应用程序设置中将 WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT 的值设置为 1 即可。

关于Azure Function : When Function AutoScale, 函数被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64248007/

相关文章:

c# - UseSpaPrerendering 从 .net core 3.0 开始被弃用,替代方案是什么?

javascript - Angular 和异步/等待

azure - 为什么Azure函数类是静态的?

asp.net-core - 迁移到 .Net Core 3 后 JsonPatchDocument 为空

azure - 您可以从 Visual Studio Mac 创建 Azure Functions 吗?

c# - 无法在 azure 上使用框架 4.6.1 执行 dotnet core 站点

c# - 通过任务取消窗口关闭。如何检测任务是否同步返回?

c# - WPF 4.5 中的 INotifyDataErrorInfo 和异步数据验证

powershell - 无法通过 Powershell 或 Visual Studio 连接到安全的 Azure Service Fabric 群集

azure - 要导入以运行此 Powershell 命令的模块的名称