1
votes

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);
    }
1

1 Answers

0
votes

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.