为通过 Azure 函数输出绑定添加到 Azure 队列的消息设置 VisibilityTimeout [英] Setting the VisibilityTimeout for a Message added to an Azure Queue via Azure Function Output Binding

查看:21
本文介绍了为通过 Azure 函数输出绑定添加到 Azure 队列的消息设置 VisibilityTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TimerTrigger 函数,输出绑定是一个 Azure 队列.

I have a TimerTrigger function and the Output binding is a Azure Queue.

这个想法是每 10 分钟计时器将运行一次,它会查看我的数据库中的一个视图并遍历返回的任何行,将它们作为消息添加到队列中.

The idea is that every 10 minutes the timer will run, it will look at a view in my database and iterate through any rows returned adding them to the queue as messages.

下面是我的示例 TimerTrigger.将消息添加到队列中效果很好.

Below is my sample TimerTrigger. It worked fine adding messages to the Queue.

但是,在我的真实世界场景中,有些行需要立即执行,而另一些行会延迟几分钟(每行有所不同).我计划通过对消息使用 VisibilityTimeout 来处理延迟.

However in my real world scenario some of the rows will require immediate execution while others will have a delay of some minutes (varies per row). I plan on handling the delay by using the VisibilityTimeout for the message.

不幸的是,通过字符串绑定不会让我设置值.CloudQueueMessage.VisiblityTimeout(在下面使用)是只读的.

Unfortunately the binding via a string wouldn't let me set the value. CloudQueueMessage.VisiblityTimeout (used below) is readonly.

#r "Microsoft.WindowsAzure.Storage"

using System;
using Microsoft.WindowsAzure.Storage.Queue;

public static void Run(TimerInfo myTimer,  ICollector<CloudQueueMessage> outputQueueItem, TraceWriter log)
{
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");    


    //- Add a message to be processed now.
    CloudQueueMessage msg = new CloudQueueMessage("Now");
    outputQueueItem.Add(msg);

    //- Add a message to be processed later.
    //- this code below won't work because NextVisibleTime is readonly.
    //- is there some way to set the VisibilityTimeout property before queueing?
    msg = new CloudQueueMessage("Later");
    DateTime otherDate = DateTime.Now.AddMinutes(3);

    msg.NextVisibleTime = otherDate;
    outputQueueItem.Add(msg);

}   

有没有办法让绑定将消息添加到队列中,让我根据需要逐条设置 VisibilityTimeout 消息?

Is there any way to have the binding add messages to the queue and let me set the VisibilityTimeout message by message as appropriate?

推荐答案

Azure Functions Storage Queue 的输出绑定只允许我们访问 CloudQueueMessage,它不允许我们为消息设置 VisibilityTimeout.

Azure Functions Storage Queue’s output binding only gives us access to the CloudQueueMessage which doesn’t let us set the VisibilityTimeout for a message.

我重写了我的代码以连接到 Azure 存储队列并将消息手动发布到队列中,而不是通过 Azure 函数输出绑定.

I rewrote my code to connect to the Azure Storage Queue and post the messages onto the queue manually rather than through Azure Function output binding.

见下文...

#r "Microsoft.WindowsAzure.Storage" 

using System;
using System.Configuration;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;

public static void Run(TimerInfo myTimer, TraceWriter log)
{    
    log.Info($"Queue Notifications: {DateTime.Now}, {myTimer.Schedule}, {myTimer.ScheduleStatus}, {myTimer.IsPastDue}");

    //* Setup the connection to q-notifications, create it if it doesn't exist.
    var connectionString = ConfigurationManager.AppSettings["AzureWebJobsStorage"]; 
    var storageAccount = CloudStorageAccount.Parse(connectionString);
    var queueClient = storageAccount.CreateCloudQueueClient();
    var queue = queueClient.GetQueueReference("q-notifications");
    queue.CreateIfNotExistsAsync();

    //* Eventually this will come from iterating through a SQL Database View of messages that need queueing.
    //* For testing just show we can add two messages with different Visibilty times.
    CloudQueueMessage message;
    TimeSpan delay;

    //* Queue Message for Immediate Processing.
    message = new CloudQueueMessage("Now Message");
    queue.AddMessageAsync(message, null, null, null, null);

    //* Queue Message for Later Processing.
    delay = DateTime.UtcNow.AddMinutes(3) - DateTime.UtcNow;
    message = new CloudQueueMessage("Later Message");
    queue.AddMessageAsync(message, null, delay, null, null);

    //* Queue Message for Even Later Processing.
    delay = DateTime.UtcNow.AddMinutes(12) - DateTime.UtcNow;
    message = new CloudQueueMessage("Even Later Message");
    queue.AddMessageAsync(message, null, delay, null, null);
}

这篇关于为通过 Azure 函数输出绑定添加到 Azure 队列的消息设置 VisibilityTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆