简体   繁体   中英

How mark a Service Bus message as processed from an Azure function?

I have two very simple NodeJS based Azure function, one which pushes an event to a Service Bus queue:

import { Context } from '@azure/functions';

export async function run(context: Context, myTimer: any): Promise<void> {
  context.log('Function started!');

  context.bindings.taskQueue2 = [{ message: 'Some task message' }];

  context.log('Function finished!');
}

and an another which reads the message, logs it's content and then exists:

import { Context } from '@azure/functions';

export async function run(context: Context, myQueueItem: any): Promise<void> {
  // https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus#trigger---message-metadata
  context.log('Node.js ServiceBus queue trigger function processed message', myQueueItem);
  context.log('EnqueuedTimeUtc =', context.bindingData.enqueuedTimeUtc);
  context.log('ExpiresAtUtc =', context.bindingData.expiresAtUtc);
  context.log('DeliveryCount =', context.bindingData.deliveryCount);
  context.log('MessageId =', context.bindingData.messageId);
  // I have tried with both calling and not calling context.done
  // context.done();
}

The related parts from my host.json :

{
  "extensions": {
    "serviceBus": {
      "prefetchCount": 100,
      "messageHandlerOptions": {
        "autoComplete": false,
        "maxConcurrentCalls": 512,
        "maxAutoRenewDuration": "00:10:00"
      }
    }
  },
}

What I would expect that calling the first function once would result in:

  • one message is pushed to the service bus queue (works)
  • the second function would read that message (works)
  • the second function would marke it as processed when exiting without error (doesn't work)

However what I see instead is the second function re-receives the message from the queue until DeliveryCount reaches the limit.

My questions are:

  • Why the task is not marked as complete automatically when the task is finished?
  • How can I mark it as complete manually if messages are not marked as finished by default?

According to the host.json you provided, you set the autoComplete as false. If you want to mark the message as complete , we need to manually call the method complete() . For more details, please refer to the docuemnt在此处输入图片说明

But according to my research, at the moment, the autoComplete: false is only useful for C# function. For more details, please refer to the GitHub issue . So I suggest you set the autoComplete as true. It will call Complete on the message if the function finishes successfully, or calls Abandon if the function fails. For more details, please refer to the official document

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM