0

I have a webjob which process the message only once by using the condition (DevliverCount = 1). Because I don't want other instance to process it if the locktime expired by first webjob. As other webjob try to process the message after locktime expired, the condition (DevliverCount = 1) will not met and comes out of the method which deletes the message from the queue automatically. The problem over here is if the message state went to never finished (other than success) I wont have message in queue to process. How to handle this situation?

Bhanu Reddy
  • 205
  • 3
  • 12

1 Answers1

0

I think part of the problem is that you're trying to use the MaxDeliveryCount property to prevent concurrent message processing:

MaxDeliveryCount

The max delivery count setting is not used to prevent multiple consumers from processing a message at the same time, it's used to prevent "poison messages" where any consumer attempts to process a message whose contents prevent successful processing, and therefore the message would otherwise be processed forever.

I recommend you determine exactly what it is you're trying to accomplish. If you want a simple competing consumers scenario where multiple webjobs consume messages from a single queue, then there are standard ways to accomplish that:

good description of competing consumers

competing consumers with Service Bus queues

You can use MaxDeliveryCount in conjunction with competing consumers... if you want to prevent poison messages you can set MaxDeliveryCount to something larger than 1 and still give other consumers a chance to process messages whose locks expire.

Azure Service Bus supports dead-lettering of poison messages that exceed max delivery count, so you're able to examine such messages offline... they aren't simply deleted forever.

You might also need to add code in your webjobs to renew locks prior to their expiration... otherwise service bus can't differentiate between "valid messages that are taking a long time to process" and "poison messages that can't be processed". Without lock renewal your long-running valid messages will be dead-lettered the same as poison messages, which is almost certainly not what you want.

Good luck!

JoshL
  • 1,706
  • 1
  • 12
  • 13
  • DeliveryCount is of BrokeredMessage object property which will be incremented when it is processing by multiple webjobs. – Bhanu Reddy May 12 '16 at 05:34
  • Yes but why do you insist on a DeliveryCount of no more than 1? What is your goal in specifying that limit? – JoshL May 12 '16 at 13:13
  • The messages that are processed in webjob can take more than 10-15 minutes for each message. The max lock time on message is only 5 minutes. After that it expires and other webjob instance can read the message and try to reprocess which I dont want. So I am basically restricting so that the same message is not processed multiple times by different instances of the same webjob. – Bhanu Reddy May 14 '16 at 04:12
  • Ah, I see now. I think what you really need is to monitor the progress of your webjob as it processes a message and renew the lock via BrokeredMessage.RenewLock(). You *must* do this before the five minute expiration expires. You can renew multiple times, as needed. Make sure you remove the check for DeliverCount == 1, if you use lock renewal you shouldn't need it. – JoshL May 14 '16 at 13:07
  • The code in job makes a network call which takes of upto 10-15 minutes as I mentioned so I can't just Renew until the newtwork call is completed. – Bhanu Reddy May 16 '16 at 08:37
  • Actually, you can do that... spin up a secondary thread that renews the lock every, say, 4 minutes and 30 seconds. When the network call returns on the main thread, kill the secondary thread so that it stops lock renewal. – JoshL May 16 '16 at 11:57
  • Thanks for suggestion. – Bhanu Reddy May 19 '16 at 07:28