【发布时间】:2017-07-24 07:38:40
【问题描述】:
我有一个 TimerTrigger 函数,并且输出绑定是一个 Azure 队列。
这个想法是每 10 分钟计时器将运行一次,它将查看我的数据库中的一个视图并遍历返回的任何行,将它们作为消息添加到队列中。
下面是我的示例 TimerTrigger。将消息添加到队列中效果很好。
但是,在我的真实世界场景中,有些行需要立即执行,而另一些行会延迟几分钟(每行有所不同)。我计划通过对消息使用 VisibilityTimeout 来处理延迟。
不幸的是,通过字符串绑定不会让我设置值。 CloudQueueMessage.VisiblityTimeout(在下面使用)是只读的。
#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 消息?
【问题讨论】:
-
在Sound Code 找到以下内容。 “不幸的是,内置的 Azure Functions Storage Queue 的输出绑定只允许我们访问 CloudQueueMessage,这不能让我们安排时间。所以我选择简单地自己编写代码来连接到队列并延迟发送它。”我将使用文章中提出的方法进行重写,并在此处发回示例。
标签: azure visibility azure-functions azure-storage-queues