Azure関数の出力バインディングを介してAzureキューに追加されたメッセージのVisibilityTimeoutを設定する

ゴッドマインダー

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メッセージを設定させる方法はありますか?

ゴッドマインダー

Azure Functions Storage Queueの出力バインディングは、CloudQueueMessageへのアクセスのみを提供し、メッセージのVisibilityTimeoutを設定することはできません。

コードを書き直してAzureStorage Queueに接続し、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.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);
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ