Azure WebJobs & JobHostConfiguration

In the previous post we experienced:

  • Messages in the queue seems not to be processed in the same order they are placed in the queue. Check Step20 in the prev. post. Message 3 seems to be processed after message 4 and 5.
  • When a message arrived to the queue, it is not triggering WebJob method immediately. It is processed after few seconds (check step 14 in the prev. post)
  • When you have several messages waiting in the queue, some of them processed concurrently.
  • After 5th unsuccessful message processing attempt, it is moved into another queue called Poison.

Is there a way to control this behavior? Yes. Just add the following lines into your WebJob's Main method and update the values as desired.

static
void Main()

{

JobHostConfiguration config = new
JobHostConfiguration();

config.Queues.BatchSize = 8;

config.Queues.MaxDequeueCount = 4;

config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(15);

 

var host = new
JobHost(config);

// The following code ensures that the WebJob will be running continuously

host.RunAndBlock();

}

 

In the above code, lines 5-7 are the default code generated by VisualStudio. We added lines 1-4 and here is their description.

 

BatchSize: This parameter defines the number of messages to be processed concurrently. If you set it to 1 in the previous blog post scenario, you will see that the messages in the queue will be processed one by one in the queue order. If you have powerful WebApp instance in Azure, you can increase this number to process multiple messages at the same time without so much performance drop in your WebApp.

 

MaxDequeueCount: In the prev. blog post step 20, we saw that after 5th unsuccessful message processing attempt, message is moved into Poison queue. This parameter sets how many trials needed before putting a message into Poison queue. Important: Unless there exist other messages waiting in the queue that are not in concurrent processing stage, there is no time interval between trials! So if there exists enough resources, retry occurs immediately. In your case, if you need other processes to be completed before the next trial, just use thread.Sleep inside the trigger method.

 

MaxPollingInterval: In the prev. blog post step 14, we observed that the message arrived to the queue are not processed immediately. Because WebJob checks the queue every MaxPollingInterval time span to see if there is a new message arrived. You can adjust this frequency by MaxPollingInterval parameter.