Search code examples
azureazure-webjobsazure-queues

singleton in azure queuetrigger not working as expected


My understanding of this is obviously wrong, any clarification would be helpful.

I thought that adding [Singleton] to a web job would force it to run one after another. This does not seem to be the case.

This is my very basic test code (against a queue with about 149 messages)

      [Singleton] //just run one at a time
    public static void ProcessQueueMessage([QueueTrigger("datatrac-stops-to-update")] string message, TextWriter log)
    {
        monitorEntities mDb = new monitorEntities();

        //go get the record
        int recordToGet = Convert.ToInt32(message);
        var record = (from r in mDb.To_Process where r.Id == recordToGet select r).FirstOrDefault();

        record.status = 5;
        mDb.SaveChanges();

        Console.WriteLine($"Finished record {message}");
    }

When it runs I get this on the console: enter image description here

and as I step though it I am getting conflict errors.

What am I not understanding?

RESOLVED - MORE INFO

Here is what I did to address this, like Haitham said in his answer [Singleton] refers to how many instances of the webjob itself is running -- not how many items are processed per instance.

That was addressed by modifying my Main like:

 static void Main(string[] args)
    {
        var config = new JobHostConfiguration();
        config.Queues.BatchSize = 2;

Which when set to 1 only ran 1 at a time. When set to 2 like above then modifying the below code:

 public static void ProcessQueueMessage([QueueTrigger("datatrac-stops-to-update")] string message, TextWriter log)
    {
        var threadID = Thread.CurrentThread.ManagedThreadId;

        Console.WriteLine($"{threadID} : started record {message}");

Produces this behavior (which is what was expected): enter image description here

Link where I found documentation on above:

https://github.com/Azure/azure-webjobs-sdk/wiki/Queues#config


Solution

  • Singleton does not mean it will run it one after another but mainly about instantiation the instance for the web job class.

    If you need to run just one at a time, you can use locks on a static variable to prevent the code to execute more than one time. But I would not recommend that anyway and you have to see why there are conflict errors