c# - Azure函数: how to implement delay of retrying queue message better

标签 c# azure azure-functions azure-queues

我的Azure函数应该监听队列中的消息,然后获取消息,尝试使用消息内的值调用外部服务,如果外部服务返回“OK”,那么我们必须将消息写入另一个队列(用于下一个Azure函数) ,如果返回“失败”,我们必须返回到当前队列,并在 5 分钟后通过我们的 Azure 函数再次重试。如何实现?我用计时器做到了,但解决方案不喜欢我:

    [FunctionName("FunctionOffice365VerificateDomain_and_AddService_and_GexMxRecord")]
    public async static Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer,
        [Queue("domain-verificate-Office365-add-services-get-mx-record", Connection = "StorageConnectionString")]CloudQueue listenQueue,
        [Queue("domain-add-mx-record-to-registrator", Connection = "StorageConnectionString")]CloudQueue outputQueue,
        ILogger log)
    {
        while (true)
        {
            // do "invisible" message for next 30 sec
            var message = await listenQueue.GetMessageAsync();
            if (message != null)
            {
                DomainForRegistration domainForRegistration = JsonConvert.DeserializeObject<DomainForRegistration>(message.AsString);
                try
                {
                    await _office365DomainService.VerifyDomainAsync(domainForRegistration.DomainName);
                    // remove message
                    await listenQueue.DeleteMessageAsync(message);

                    await _office365DomainService.UpdateIndicateSupportedServicesDomainAsync(domainForRegistration.DomainName);

                    var mxRecord = await _office365DomainService.GetMxRecordForDomainAsync(domainForRegistration.DomainName);
                }
                catch (DomainVerificationRecordNotFoundException)
                {
                     // thrown when VerifyDomainAsync failed
                }
            }
            else
                break;
        }
    }

如何更仔细地做到这一点,没有这些 while(true),但在验证失败后超时?

最佳答案

同意@DavidG,尝试使用队列触发器来实现你的目标。 W可以信赖host setting的队列。

visibilityTimeout is The time interval between retries when processing of a message fails maxDequeueCount is The number of times to try processing a message before moving it to the poison queue.

{
    "version": "2.0",
    "extensions": {
        "queues": {
            "visibilityTimeout" : "00:05:00",
            "maxDequeueCount": 2,
        }
    }
}

这样,函数应该看起来像这样

public static async Task Run(
    [QueueTrigger("domain-verificate-Office365-add-services-get-mx-record")]string myQueueItem, ILogger log,
    [Queue("domain-add-mx-record-to-registrator", Connection = "StorageConnectionString")]IAsyncCollector<string> outputQueue
)
{
    // do stuff then output message
    await outputQueue.AddAsync(myQueueItem);
}

如果你不想向主机抛出异常,我们可以求助initialVisibilityDelay CloudQueue 方法的。

specifying the interval of time from now during which the message will be invisible

    public static async Task Run(
        [QueueTrigger("domain-verificate-Office365-add-services-get-mx-record")]string myQueueItem, ILogger log,
        [Queue("domain-add-mx-record-to-registrator", Connection = "StorageConnectionString")]IAsyncCollector<string> outputQueue,
        [Queue("domain-verificate-Office365-add-services-get-mx-record", Connection = "StorageConnectionString")]CloudQueue listenQueue
    )
    {

        try 
        {
            // do stuff then output message
            await outputQueue.AddAsync(myQueueItem);
        }
        catch(DomainVerificationRecordNotFoundException)
        {
            // add the message in current queue and can only be visible after 5 minutes
            await listenQueue.AddMessageAsync(new CloudQueueMessage(myQueueItem), null, TimeSpan.FromMinutes(5), null, null);
        }
    }

关于c# - Azure函数: how to implement delay of retrying queue message better,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54282850/

相关文章:

azure - 从 Azure LogicApp 调用 Azure Function

c# - 表达式树的问题

c# - 转换日期时间?串入 dd-MM-yyyy

azure - 我需要 docker 在 linux ubuntu 上运行 azure data studio 吗?

azure - 在 Azure Function App 中使用 PowerShell 连接到不同的 Azure AD 租户

python - Azure Functions Blob 部署

C#,获取用户值并重置它

c# - 当通过 WCF 发送对象时,如何处理 LINQ to SQL DataContext 中的关系成员?

azure - ASP.NET Core API - 在 Azure 应用服务上获取 404,但在本地主机上工作正常

json - Azure 不接受 IoT 中心路由的 json 格式