Unable to make a Continuous Azure Webjob a singleton

342 views Asked by At

I have an Azure webjob that reads from a Service Bus Queue. My goal is to process 1 message from a Queue, synchronously, per instance. Is that possible?

What I have observed is that if there are 100 messages in the queue, I see 10~20 instances of the webjob running at the same time, each processing different messages from the queue. I would like to limit this to 1 message. I have tried the following so far:

  1. I have created a settings.job file with {"is_singleton": true} property in it.
  2. I have also tried using [Singleton] attribute on top of the function signature.
  3. I tried setting the properties config.Queues.BatchSize = 1 and config.Queues.NewBatchThreshold = 1.

Here's the signature of my webjob function:

[ServiceBusAccount("servicebusconnstr")]
public static void MigrateDoc([ServiceBusTrigger("myqueue")] string queueItem, TextWriter log)
{
...
}

None of these methods seem to work.

1

There are 1 answers

0
Sun On

Figured it out. The settings I need is ServiceBusConfiguration.MessageOptions.MaxConcurrentCalls = 1. It can be done like so:

var config = new JobHostConfiguration();
var serviceBusConfig = new ServiceBusConfiguration();
serviceBusConfig.MessageOptions.MaxConcurrentCalls = 1;

if (config.IsDevelopment)
{
     config.UseDevelopmentSettings();
}

config.UseServiceBus(serviceBusConfig);

var host = new JobHost(config);
host.RunAndBlock();

Thanks to this post: Azure WebJob concurrency when using ServiceBusTrigger for the example.