WebJobs不重试失败的队列消息

eri*_*icb 6 azure-web-sites azure-webjobs azure-webjobssdk

我在使用新的0.3.0-beta WebJobs SDK的WebJob中有以下逻辑.当我的代码处理消息失败时,Azure仪表板会显示聚合异常(这是有意义的,因为这是异步).但是,它不会重试处理消息.

我能够找到的文档非常少,表明该消息应在失败后的10分钟内重试.新SDK不是这种情况吗?

public static Task ProcessMyMessageAsync(
    [QueueTrigger(Config.MY_QUEUE)] string msg, 
    int dequeueCount, 
    CancellationToken cancellationToken)
{
    var processor = Config.Container.GetInstance<IMessageProcessor>();
    return processor.HandleJobAsync(msg, dequeueCount, cancellationToken);
}
Run Code Online (Sandbox Code Playgroud)

我得到的异常源于SQL Timeout异常(我的代码中针对SQL Azure的db查询):

System.AggregateException: System.AggregateException: One or more errors occurred. 
    ---> System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing  the command definition. See the inner exception for details. 
        ---> System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. 
        ---> System.ComponentModel.Win32Exception: The wait operation timed out
Run Code Online (Sandbox Code Playgroud)

小智 1

您应该设置 MaxDequeueCount。

JobHostConfiguration jobHostConf = new JobHostConfiguration();
jobHostConf.Queues.MaxDequeueCount = 10;
var host = new JobHost(jobHostConf);
host.RunAndBlock();
Run Code Online (Sandbox Code Playgroud)

在将消息放入死信/坏信队列之前,将重试 10 次。

您还可以在函数中使用自定义重试策略。我建议您查看“瞬态故障处理应用程序块” https://msdn.microsoft.com/en-us/library/hh680934(v=pandp.50).aspx

或者您可以使用 SqlAzureExecutionStrategy 在 EF 中启用重试 https://msdn.microsoft.com/en-us/data/dn456835.aspx