我有一个 TimerTrigger 函数,输出绑定(bind)是一个 Azure 队列。
这个想法是,计时器每 10 分钟运行一次,它将查看我的数据库中的 View ,并迭代返回的任何行,将它们作为消息添加到队列中。
下面是我的示例 TimerTrigger。将消息添加到队列中效果很好。
但是,在我的现实场景中,某些行需要立即执行,而其他行则需要延迟几分钟(每行有所不同)。我计划通过使用消息的 VisibilityTimeout 来处理延迟。
不幸的是,通过字符串的绑定(bind)不允许我设置该值。 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);
}
有没有办法让绑定(bind)将消息添加到队列中,并让我根据需要逐条消息设置 VisibilityTimeout 消息?
最佳答案
Azure Functions 存储队列的输出绑定(bind)仅允许我们访问 CloudQueueMessage,而不允许我们设置消息的 VisibilityTimeout。
我重写了代码以连接到 Azure 存储队列并手动将消息发布到队列中,而不是通过 Azure Function 输出绑定(bind)。
见下文。 。 .
#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 函数输出绑定(bind)添加到 Azure 队列的消息设置 VisibilityTimeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42591669/