0
votes

I have two Service Bus Queues and Azure Service Bus Queue Trigger.

The function reads 1000 messages from the queue and forwards them to another (for test) When publishing to Azure (plan consumption) I get 1030+ function hits even though I only have 1000 messages in the queue. I think this has to do with the number of function instances.

Q: How to process 1 unique message only once for 1 function instance?

Queue message count image

[FunctionName("Function1")]
        public static async Task Run([ServiceBusTrigger("export-queue",
        Connection = "Connection")]
        string myQueueItem, ILogger log)
        {
            log.LogInformation($"Message: {myQueueItem}:{DateTime.Now}");
            Thread.Sleep(2000);
            queueClient = new QueueClient(ServiceBusConnectionString, QueueName);

            var message = new Message(Encoding.UTF8.GetBytes(myQueueItem));
            await queueClient.SendAsync(message);
            await queueClient.CloseAsync();
        }
2

2 Answers

0
votes

By default, ServiceBusTrigger is using PeekLock mode. This technically means a message can be processed more than once if the message lock has been locked. This would explain why for 1,000 incoming messages you get more than 1,000 invocations.

Another angle would be the duration of your function execution combined with the prefetch size. If the time it takes to execute is long enough to lose the lock on some of the prefetched messages, decrease the prefetch.

Also, consider designing your solution in a way where you know a message is processed at-least-once, not exactly-once.

0
votes

It works for me.

Add extensions for your host.json :

"extensions": {

    "serviceBus": {
      "prefetchCount": 1,
      "messageHandlerOptions": {
        "autoComplete": true,
        "maxConcurrentCalls": 1,
        "maxAutoRenewDuration": "00:05:00"
      },
      "sessionHandlerOptions": {
        "autoComplete": true,
        "messageWaitTimeout": "00:00:30",
        "maxAutoRenewDuration": "00:55:00",
        "maxConcurrentSessions": 10
      }
    }
  }