0

I have a function that adds messages to a queue using queueCollector.AddAsync(message).

I would like to set a VisibilityTimeout per message. How can it be done?

Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
Carlos Alves Jorge
  • 1,919
  • 1
  • 13
  • 29

1 Answers1

1

Looks like your function uses IAsyncCollector<T> as output type. With this output binding, we can only output message body and properties like visibilityTimeout are set by Azure Storage Service automatically.

To control the properties of CloudQueueMessage, we could use CloudQueue as output type, i.e. binding to the Queue directly. Note that visibilitytimeouot can't be larger than 7 days.

C# pre-compiled code sample, use queue trigger and output message.

public static async Task Run([QueueTrigger("queue-in")]string message, ILogger log, 
    [Queue("queue-out")]CloudQueue outQueue)
{
    // set 3 minutes visibility timeout after being created in the queue
    await outQueue.AddMessageAsync(new CloudQueueMessage(message), null, TimeSpan.FromMinutes(3), null, null);
}

C# scripts sample for online development.

#r "Microsoft.WindowsAzure.Storage"

using System;
using Microsoft.WindowsAzure.Storage.Queue;

public static async Task Run(string message, ILogger log, CloudQueue outQueue)
{
    log.LogInformation($"C# Queue trigger function processed: {message}");
    await outQueue.AddMessageAsync(new CloudQueueMessage(message), null, TimeSpan.FromMinutes(3), null, null);
}

Its function.json

{
  "bindings": [
    {
      "name": "message",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "queue-in",
      "connection": "MyStorageConnection"
    },
    {
      "type": "queue",
      "name": "outQueue",
      "queueName": "queue-out",
      "connection": "MyStorageConnection",
      "direction": "out"
    }
  ]
}

There's another visibilityTimeout in host.json. If you want to set the time interval between retries when processing of a message fails, have a look at it.

Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
  • 1
    Regarding this issue on github https://github.com/Azure/azure-webjobs-sdk-extensions/issues/480 I would guess using an IAsyncCollector should work too. – Sebastian Achatz Dec 11 '18 at 10:18
  • IAsyncCollector can indeed get a CloudQueueMessage which will do the trick. It will require adding Microsoft.WindowsAzure.Storage.Queue though. – Carlos Alves Jorge Dec 11 '18 at 11:40
  • @CarlosAlvesJorge Could you elaborate more? I noticed CloudQueueMessage before, but I can't set its properties. – Jerry Liu Dec 11 '18 at 12:18
  • @Jerry Liu not ideal but will have the job done. https://stackoverflow.com/questions/42591669/setting-the-visibilitytimeout-for-a-message-added-to-an-azure-queue-via-azure-fu – Carlos Alves Jorge Dec 13 '18 at 11:23
  • @CarlosAlvesJorge Glad you made it, so it turns out you use online csx? My solution uses pre-compiled code and seems similar to the answer while I take advantage of output bindings. I can modify my answer to csx if needed. – Jerry Liu Dec 13 '18 at 11:32
  • @CarlosAlvesJorge Added samples, we have together summarized a concise answer based on the old one, cheers! – Jerry Liu Dec 13 '18 at 15:21
  • @CarlosAlvesJorge If there's no more trouble, could you accept the answer for others to refer? – Jerry Liu Dec 17 '18 at 11:33