Service Bus Queue Lock Token Expired Error in Azure Function App - azure

I'm more used to Service Bus Queue but have challenges when using it with Azure Function App.
We have Azure Function App which reads data from Service Bus Queue through ServiceBugTrigger. Per this link, Azure Function App manage Queue message PeekLock internally (at the queue trigger and function execution end), we do not require to Complete() message at the end of the process.
My queue message lock duration is set to 3min (which is enough for my execution, I would say more than my requirement). I also applied other required parameters to treat message well like,
"serviceBus": {
"maxAutoRenewDuration": "00:05:00",
"maxConcurrentCalls": 10,
"prefetchCount": 0
}
I am getting LOCK DURATION EXPIRED error frequently with this implementation. Really no idea what's happening here, Any clue?
I am used to Service Bus Queue and aware with each parameter function. Also, have configured each parameter per requirement.

This happens when your maxAutoRenewDuration is more than the lock duration at servicebus side.
You should check the lock duration specified at Service bus queue side. Ensure it is greater or equal to maxAutoRenewDuration specified in your azure function
You can update it from portal or service bus explorer

Related

Azure Topic Subscription Configure to Run only 1 Instance at a time

I have created a Azure service bus topic subscription which receives a message form another http trigger function app and insert information into a database.
Every message I receive has an ID and based on the ID, I decide to either add a new record or updating existing one.
Problem is happening when 2 messages with the same ID are received at the same time and ends up creating 2 database records.
Is there any way to configure topic subscription function to run only 1 instance at a time? I don't have control on the function app which send
Is there any way to configure topic subscription function to run only 1 instance at a time?
You can configure everything like number of instances to be running, number of messages to be processing, number of calls and sessions, etc., in host.json file. All the attributes of host.json related to Azure Functions Service bus trigger bindings are available in this MS Doc reference.
For your requirements such as processing 1 message at a time, you can define the following attributes to 1 in the host.json file:
"maxConcurrentSessions": 1,
"maxMessageBatchSize": 1,
"maxConcurrentCalls": 1,
"messageHandlerOptions": {
"maxConcurrentCalls": 1
},
Normally, Azure Functions will process the messages in multiple and in parallel. So, the attributes like maxConcurrentSessions, maxConcurrentCalls plays the major role to define number of sessions and calls to be processed for every instance.
"batchOptions": {
"maxMessageCount": 1
}
Above complete configuration of host.json will do processing of 2nd message only after the execution of 1st message in the Service bus topic subscription azure function.

Azure Function is not triggerd after putting a message to a queue in service bus

I have a queue in a service bus. After putting a message into a queue an azure logic app and an azure functions should betriggered and process the content.
My Azure logic app is triggered but my azure funcction is not triggered. My code for azure function:
[FunctionName("ReadMEssageFromQueue")]
public static void Run([ServiceBusTrigger("messagequeue", Connection = "AzureWebJobsStorage")]string myQueueItem, ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}
host json:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AzureWebJobsStorage": "******" // connection string of my service bus
}
}
should I set something in service bus queue to send message to both ressources?
Azure Service Bus Queue messages are picked up by only one processor. So I think in your case, the logic app is picking up and consuming the message first and the message is not available for the function to process. You can try temporarily disabling the logic app and letting function pick the message to confirm this.
Ref: azure-service-bus-queue-with-multiple-listeners
You can trigger the Azure function from your logic app (not sure if it'll help your use case), or you can use Azure Service Bus topics as topics support the model where multiple consumers can subscribe to a topic.
The former option might be a better approach for you from cost perspective, as you'd need to use Standard tier of service bus in order to use topics feature, which means additional cost for you over your current setup.
Also, you might want to use some other name for service bus connection string as AzureWebJobsStorage is used for storage account connection string

Azure Service Bus - describe logic

I want to have the following logic for Service Bus queue:
only one message in queue (storing datetime)
if we try to add new message never than added to queue - reject it
Is it possible?

What happens if I do not call CloseAsync and CompleteAsync method of IMessageSession in azure service bus?

IMessageSession has CloseAsync and CompleteAsync methods.
If CloseAsync and CompleteAsync are not called (after acquiring a session and processing messages), what are the repercussions to the client and to azure service bus account?
The message is not successfully removed from the queue, the default maximum delivery count (retry) is 10. So your receiver can potentially receive the message 10 times, then it's put in deadletter queue (depending on the configuration) where it will stay for 14days (default setting)
For more details: https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dead-letter-queues
The approved answer is incorrect. TTL is applicable for the Active Queue, not for the Dead-letter Queue.
Secondly, the answer is implementation-specific, one not necessarily needs to use RegisterMessageHandler and can make use of MessageReceiver instead and something else.
Basically, CompleteAsync is used for deleting a locked message from the queue (can be DLQ as well) and CloseAsync is for closing the connection object.

Azure Service Bus - making sure the job is done

I send a message to ASB, some data (a long task) will start being processed, but the processing fails miserably (someone turns off the computer/out of memory/whatever). How should I handle this situation? Like putting the queue back on the bus? Do I need to create my own monitoring/requeueing unit?
Azure ServiceBus has an internal retry policy. If a message fails to deliver, it will be send back to the queue automatically.
When creating a queue or a topic/subscription you can specify the MaxDeliveryCount.
QueueDescription.MaxDeliveryCount
SubscriptionDescription.MaxDeliveryCount
Default value is 10. A message is automatically deadlettered after this number of deliveries.

Resources