azure - 为通过 Azure 函数输出绑定(bind)添加到 Azure 队列的消息设置 VisibilityTimeout

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

我有一个 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/

相关文章:

azure - 如何在powershell/azure cli中动态循环键值对

node.js - 无法查看上传到 azure blob 存储的图像

Android:更改所有 ListView 行中项目的可见性

c# - 无法使用 View 模型使控件在 WPF 的窗口中可见和隐藏?

node.js - 尝试使用 SAS 访问 Azure blob 时收到 "Signature did not match. String to sign used was..."

azure - PSCustomObject 数据收集问题

android - 如何通过外部按钮在 ListView 中设置可见性?

azure - 使用 Postman 对 Azure Functions 执行 Azure AD 身份验证

azure - 使用事件中心捕获的优点

Azure Functions 隔离 .Net 6.0 + SignalR