简体   繁体   中英

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, 在此处输入图片说明 在此处输入图片说明

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

As mentioned in comments. Just set value of WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT as 1 in application settings of your function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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