简体   繁体   English

检测 azure 函数超时以将消息重新路由到应用程序服务计划

[英]Detect timeout on azure function to reroute message to app service plan

I've got an Azure function unziping archives on a consumption plan.我有一个 Azure 函数可以解压缩消费计划中的存档。 Some of them happens to take more than 10min to unzip and can timeout.其中一些恰好需要 10 分钟以上的时间来解压缩并且可能会超时。 I'm thinking of having a separate app service plan where I would reroute the extraction when timing out on consumption plan.我正在考虑制定一个单独的应用程序服务计划,在消费计划超时时我会重新路由提取。

How would you do that?你会怎么做? a timer in the function?功能中的计时器? a catch timeout exception?捕获超时异常? Do you have a better suggestion?你有更好的建议吗?

Thanks谢谢

For those interested I ended adding my own timeout (few seconds earlier than Azure one) to the extract function, then rerouting to another queue handled by a service app plan than don't timeout. 对于那些感兴趣的人,我结束了我自己的超时(比Azure一早几秒)添加到提取功能,然后重新路由到服务应用计划处理的另一个队列,而不是超时。

Code : 代码:

using (var timeoutCts = new CancellationTokenSource())
{
    try
    {
        // task completed within timeout
        timeoutCts.CancelAfter(590000);
        var counter = await ExtractArchiveAsync(archiveFullName, myBlob, timeoutCts.Token);
        log.Info($"Extracted : { counter.GetCurrentCount() }");
    }
    catch (OperationCanceledException)
    {
        // timeout logic
        log.Info($"Function timedout, redirected to long queue");
        var queue = StorageService.GetCloudQueueReference("ArchiveToExtractQueueTimedOut");
        await queue.AddMessageAsync(new CloudQueueMessage(archiveFullName));
    }
}

The accepted answer requires ExtractArchiveAsync() to throw the OperationCanceledException.接受的答案要求 ExtractArchiveAsync() 抛出 OperationCanceledException。 While this can work, I had a different usecase that might be useful to others finding this thread:虽然这可行,但我有一个不同的用例,可能对其他人找到这个线程有用:

private static async Task ExecuteWithTimeout(Func<Task> action, int timeout)
{
      Task task = action();
      if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
      {
          await task;
      }
      else
      {
          throw new TimeoutException("Timeout after " + timeout + " milliseconds");
      }
}

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

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