首页 > 解决方案 > Azure 函数有时会在出队时启动多个实例

问题描述

我有一个相当简单的天蓝色函数,设置如下:

{
  "queues": {
    "maxPollingInterval": 2000,
    "visibilityTimeout": "00:10:00",
    "maxDequeueCount": 3,
    "batchSize": 12,
    "newBatchThreshold": 6
  }
}

我注意到有时当函数失败时,它会按预期将消息出队,然后重试再次执行该函数。问题是有时它会在同一条消息上启动两个实例。这是已知的行为吗?我的功能如下所示。在抛出异常之前我应该​​做些什么吗?

[FunctionName("MoveCopyFilesFolders")]
public static void Run([QueueTrigger("xx", Connection = "StorageConnStr")]string jobIdStr, TraceWriter log)
{
    log.Info($"MoveCopyFilesFolders processed: {jobIdStr}");

    Dictionary<string, string> properties = new Dictionary<string, string> { { "service", "xx" }, { "jobid", jobIdStr } };
    KK.Common.Instrumentation.AILogger logger = new KK.Common.Instrumentation.AILogger(configuration.ApplicationInsightsKey, properties);

    try
    {
        logger.WriteTrace($"Start moving/copy files/folders for jobid: {jobIdStr}", SeverityLevel.Verbose); 

        var jobId = Int32.Parse(jobIdStr);

        var moveCopyRepo = new CopyMoveRepository(configuration.DBConnectionString);
        var appRegRepo = new AppRegistrationRepository(configuration.DBConnectionString);

        CopyOrMoveItemsService.Run(jobId, moveCopyRepo, appRegRepo, logger);
        logger.WriteTrace("End move/copy files for site", SeverityLevel.Information);
        logger.Flush();
    }
    catch (Exception ex)
    {
        log.Info(ex.Message);
        logger.WriteException(ex);
        logger.Flush();
        throw ex;
    }          
}

我看到的行为是函数按预期启动,只有一个实例。当失败时,有时会在同一条消息上启动两个实例。我可以看到,因为我添加了 10 条具有不同 id 的消息作为输入,并且在失败后两个实例以相同的 id 运行。

标签: azure-functions

解决方案


推荐阅读