0

I have an web job that consumes message from Azure Service Bus Topic by registering a OnMessage callback . The message lock duration was set to 30 seconds and the lock renew timeout to 60 seconds. As such jobs taking more than 30 seconds to process service bus message were getting lock expired exception.

Now,I have set the message lock duration to more than lock renew time out. But somehow it still throws same exception. I also restarted my webjob, but still no luck.

I tried running same webjob consuming messages from different topic with later settings and it works fine. Is this behaviour expected, and after how much time does this setting change normally reflect.

Any help will be great

imswapy
  • 112
  • 2
  • 7

2 Answers2

3

I have set the message lock duration to more than lock renew time out. But somehow it still throws same exception.

The max value of lock duration is 5 min. If you need less than 5 min to process the job, you could increase the lock duration of your message to meet your requirement.

If you need more than 5 min to process your job, you need to set the AutoRenewTimeout property of OnMessageOptions. It will renew the lock if the lock expired before it reached the AutoRenewTimeout. For example, if you set lock duration to 1 min and set AutoRenewTimeout to 5 min. The message will keep in locked for up to 5 min if you don't release the lock.

Here are the sample code I used to test the lock duration and AutoRenewTimeout on my side. If the job spent more time than lock duration and AutoRenewTimeout, it will throw a exception when we complete the message(it means timeout happened). I also modified the lock duration on portal and the configuration will be applied immediately when I receive a message.

SubscriptionClient Client = SubscriptionClient.CreateFromConnectionString(connectionString, "topic name", "subscription name");

// Configure the callback options.
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.AutoRenewTimeout = TimeSpan.FromSeconds(60);

Client.OnMessage((message) =>
{
    try
    {
        //process the message here, I used following code to simulation a long time spent job
        for (int i = 0; i < 30; i++)
        {
            Thread.Sleep(3000);
        }
        // Remove message from subscription.
        message.Complete();
    }
    catch (Exception ex)
    {
        // Indicates a problem, unlock message in subscription.
        message.Abandon();
    }
}, options);

For your issue, please check how much time will be spent on your job and choose a right way to set lock duration and AutoRenewTimeout.

Amor
  • 8,325
  • 2
  • 19
  • 21
  • The only issue I have with `OnMessageOptions.AutoRenewTimeout` is that is is not a guaranteed operation and documentation doesn't state so. It's a client side operation, meaning it can fail and lock renewal will not take place. https://weblogs.asp.net/sfeldman/azure-service-bus-autorenewtimeout – Sean Feldman Jun 19 '17 at 15:04
  • If you want to perform more control over the lock renew action, you could invoke the RenewLock method which mentioned in the blog. – Amor Jun 19 '17 at 16:20
  • Correct. It's just then you lose the convenience of OnMessage API. Either way, my comment was just to mention that reliance on AutoRenewTimeout should be handled with care. Thanks. – Sean Feldman Jun 19 '17 at 16:40
  • @Amor-MSFT , increasing the AutoRenewTimeout works for me. I did some testing and strangely it's working fine when AutoRenewTimeout is set <= 30 mins. But beyond that lock expires exactly at 30 min mark. Is there any max limit for AutoRenewTimeout ? – imswapy Jul 10 '17 at 09:37
  • Where do we set the lock duration and the auto lock renew options? I can see the AutoRenewTimeout in the MessageHandlerOptions but cant see the LockDuration property anywhere in this or in the SubscriptionClient. I want to set the lock timeout and auto lock renewal time. (Im using the aspnet core lib "Microsoft.Azure.ServiceBus") – Rob Aug 31 '18 at 08:06
1

The settings should be reflected almost immediately. Also lock renewal should probably be more than the lock duration or disabled.

Lock renewal feature is ASB client feature and it doesn't override lock duration set on entities. If you can reproduce this issue and share the repro, raise a support issue with Microsoft.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80