1

Currently I'm tring to implement my service queue bus on web job. The process that i'm perform with each message is taking about 5 - 30 seconds. While I'm not getting many messages in same time it's running ok, without any exceptions. Otherwise I'm getting this error: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue.

I'm read something about time that I should use to avoid of this error, but it doesn't help me (I'm still getting this error) and I dont' know why it's happen? Maybebe someone stack on similiar problem and solve it with other solution that i use (I'm change MaxAutoRenewDuration to 5 minutes).

Maybe is something wrong with my web job implementation ? Here's my code:

        static void Main(string[] args)
    {
        MainAsync().GetAwaiter().GetResult();
    }

    static async Task MainAsync()
    {
        JobHostConfiguration config = new JobHostConfiguration();
        config.Tracing.ConsoleLevel = System.Diagnostics.TraceLevel.Error;
        queueClient = new QueueClient(ServiceBusConnectionString, QueueName);
        RegisterOnMessageHandlerAndReceiveMessages();

        JobHost host = new JobHost(config);
        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();
        }
        host.RunAndBlock();

    }

    static void RegisterOnMessageHandlerAndReceiveMessages()
    {
        var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
        {
            MaxConcurrentCalls = 1,
            MaxAutoRenewDuration = TimeSpan.FromMinutes(5),
            AutoComplete = false
        };
        queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
    }

    static async Task ProcessMessagesAsync(Message message, CancellationToken token)
    {
        var watch = System.Diagnostics.Stopwatch.StartNew();
        Console.WriteLine("----------------------------------------------------");
        try
        {
            Thread.Sleep(15000); // average time of actions that i perform

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            var results = true;
        }
        catch (Exception ex)
        {
        }
        Console.WriteLine("----------------------------------------------------");
        await queueClient.CompleteAsync(message.SystemProperties.LockToken);
    }
dawcza94
  • 327
  • 2
  • 10

1 Answers1

0

MessageHandlerOptions has a ExceptionReceivedHandler callback you could use to get more details about the failure.

Losing lock can take place, especially if client fails to communicate back to the server on time or there are intermittent failures Azure Service Bus retries itself, but takes time. Normal LockDuration time is 60 seconds, so your sample code should have worked. It could be that you're experiencing connectiving issues that are retried by the client and by then lock is expired. Another option, clock skew between your local machine and the server, which speeds up lock expiration. You could sync the clock to eliminate that.

Note that MaxAutoRenewDuration is not as effective as LockDuration. It's better to set the LockDuration to the maximum that rely on MaxAutoRenewDuration.

In case this code is not what you've used to repro the issue, please share the details.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80
  • Actually when I using this code I'm getting exception too. If I look on details in exception (ExceptionReceiveHandler) i'm getting two kinds of executing actions: 1) UserCallback 2) RenewLock It always say that : ,,The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. " What about LockDuration, is it a properties that i can send the same way as MaxAutoRenewDuration ? – dawcza94 Feb 04 '18 at 09:51
  • "`UserCallback`" means exception was thrown in your code. What's the inner exception you're getting in your callback code? "`RenewLock`" means your processing took longer that the `LockDuration`. I'd suggest to measure and log processing time. – Sean Feldman Feb 04 '18 at 20:39