Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure WebJobs and Thread Safety

I'm new to Azure WebJobs and I have a basic question. I have a console app deployed as a WebJob, by nature console apps use static a lot, and I have some local static variables as shown below, would I run into trouble with multiple threads updating these variables simultaneously by the WebJob?

class Program
{
    private static MyService _service;
    private static int _counter;

    static void Main()
    {
      ...
    }

    private static void Method1() { .... }
}
like image 684
Ray Avatar asked Dec 26 '22 03:12

Ray


2 Answers

Since you tagged the question with azure-webjobs-sdk and I remember that you had another question a few days ago about the SDK, I will assume your webjob uses the SDK and the multithreading is caused by the fact that we running triggered functions in parallel.

The first part of my answer is not necessarily web jobs related:

class Program
{
    private static MyService _service;
    private static int _counter;

    private static readonly object _lock = new object();

    // The 3 blocks of code are thread safe by themselves
    // but put together, there is no guarantee. Avoid
    // multiple locks if you can
    private static void Method1() 
    {
        // 1. You can use a lock 
        lock(_lock)
        {
            // All the code here will be executed by a single thread. 
            // If another thread tries to get the lock, it will have
            // to wait until the lock is released

            _service.DoSomething();
        }

        // 2. For atomic operations you can use Interlocked
        Interlocked.Increment(ref _counter);

        // 3. For conditional locking
        if (_service.SomeBooleanProperty)
        {
            lock(_lock)
            {
                // Check again to see the condition still holds
                // because it might have changed between the
                // execution of the first if and the lock
                if (_service.SomeBooleanProperty)
                {
                    // Execute the non thread safe code
                }
            }
        }
    }
}

Reference to the Interlocked class

Now I'm talking WebJobs SDK:

If you want to control the concurrency for processing, you can set the Queue.BatchSize property. By default is 16. If you set it to 1, everything runs sequential. See the reference here. If you are having concurrency issues with multi instance concurrency (multiple instances of the same job) then blob leases are the way to go.

like image 130
Victor Hurdugaci Avatar answered Jan 13 '23 23:01

Victor Hurdugaci


No, not unless you specifically write multi-threaded code in your console application. When the WebJob is invoked in Azure, it will get it's own process. For example, here is a screen capture from process explorer for a WebJob called ConsoleTestApp.exe. Notice that it's PID is separate from the W3WP process your Website runs in.

enter image description here

like image 21
Rick Rainey Avatar answered Jan 13 '23 22:01

Rick Rainey