简体   繁体   English

Azure 函数:当函数自动缩放时,函数被多次调用

[英]Azure Function : When Function AutoScale, function called multiple times

In Azure Queue add single entry for queueitem but function called multiple times for same queueitem, not able to understand what is wrong, We have used Async/Await in function and code is also Async, below is Azure function settings,在Azure Queue中为queueitem添加了单个条目,但为同一个queueitem多次调用了函数,无法理解是什么问题,我们在函数中使用了Async/Await,代码也是Async,下面是Azure函数设置, 在此处输入图片说明 在此处输入图片说明

below is function code,下面是功能代码,

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}");
    }
}

I have check the code where added entry in queue, but only one entry is added for one queueitem.我检查了在队列中添加条目的代码,但只为一个队列项添加了一个条目。 This issue happened when added multiple entry in same queue (ie load testing where I have added 24 request only) for different queue item, when single queue is run then this is not happened, our functions are in App Service Plan with autoscaling当为不同的队列项在同一队列中添加多个条目时发生此问题(即我仅添加了 24 个请求的负载测试),当运行单个队列时,则不会发生这种情况,我们的功能在具有自动缩放功能的应用服务计划中

As mentioned in comments.正如评论中提到的。 Just set value of WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT as 1 in application settings of your function.只需在函数的应用程序设置中将WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT值设置为1

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

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