通过Azure功能输出绑定为添加到Azure队列的消息设置VisibilityTimeout

时间:2017-03-04 03:17:58

标签: azure visibility azure-functions azure-storage-queues

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

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

以下是我的示例TimerTrigger。它可以很好地向队列添加消息。

然而,在我的真实场景中,某些行需要立即执行,而其他行将延迟几分钟(每行不同)。我计划通过使用VisibilityTimeout来处理延迟。

不幸的是,通过字符串绑定不会让我设置值。 CloudQueueMessage.VisiblityTimeout(在下面使用)是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消息?

1 个答案:

答案 0 :(得分:2)

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

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

见下文。 。

#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.CreateIfNotExists();

    //* 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.AddMessage(message, null, null, null, null);

    //* Queue Message for Later Processing.
    delay = DateTime.UtcNow.AddMinutes(3) - DateTime.UtcNow;
    message = new CloudQueueMessage("Later Message");
    queue.AddMessage(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.AddMessage(message, null, delay, null, null);
}