azure-webjobs-sdk icon indicating copy to clipboard operation
azure-webjobs-sdk copied to clipboard

Singleton doesn't work & TimerTrigger marked with Singleton is repeated on multiple instances for the same time interval

Open AAATechGuy opened this issue 2 years ago • 3 comments

Please provide a succinct description of the issue.

A. Singleton doesn't appear to work. Same function is executed on two different machines at the same time.

B. TimerTrigger marked with Singleton is repeated on multiple instances for the same time interval If TimerTrigger was configured with interval=1minute, then, I expected 08:00:00 to be executed only on a single machine, and not twice on two different machines (there are only two machines). Similarly, for the next interval 08:01:00, 08:02:00 and so on.

Repro steps

Provide the steps required to reproduce the problem

  1. Using following libraries.
    <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.31" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />

Create function.

        [Singleton]
        public void RunOnTimerTrigger2(
            [TimerTrigger("0 */1 * * * *", UseMonitor = true)] TimerInfo myTimer,
            ILogger log)
        {
            // code for scenario A
            // Trace.WriteLine($"[{DateTime.UtcNow.ToString("o")}] {nameof(RunOnTimerTrigger2)} 1");
            // Thread.Sleep(10000);
            // Trace.WriteLine($"[{DateTime.UtcNow.ToString("o")}] {nameof(RunOnTimerTrigger2)} 2");

            // code for scenario B
            Trace.WriteLine($"[{DateTime.UtcNow.ToString("o")}] {nameof(RunOnTimerTrigger2)}");
        }

Initialized as follows.

            var _storageConn = "<actual Azure connection string>";
            var builder = new HostBuilder();
            builder.ConfigureAppConfiguration((builderContext, cb) =>
            {
                cb.AddInMemoryCollection(new Dictionary<string, string>()
                {
                    { "AzureWebJobsDashboard", _storageConn },
                    { "AzureWebJobsStorage", _storageConn }
                });
            });

            builder.ConfigureWebJobs(b =>
            {
                b.AddAzureStorageCoreServices();
                b.UseHostId(Environment.MachineName.ToLower());
                b.AddTimers();
            });

            var host = builder.Build();

            using (host)
            {
                var jobHost = host.Services.GetService(typeof(IJobHost)) as JobHost;

                await host.StartAsync();

                Trace.WriteLine("waiting for newline to exit");
                Console.ReadLine();

                await host.StopAsync();
            }
  1. Run the compiled binaries in two separate machines with the same Azure Storage Connection string.

Expected behavior

ScenarioA: when Singleton is annotated, atleast the function should be executed exactly once across machines.

ScenarioB: for a particular interval, I should have seen execution only on one machine.

Actual behavior

ScenarioA: In fact, I added Thread.Sleep(10000) inside the trigger. Singleton itself doesn't seem to be honored.

Specifically, I did not expect 07:56:00-07:56:10 (10 second interval) to be executed only on one machine, which is the functionality of Singleton.

On machine 1, image

On machine 2, image

You can notice the overlapping intervals.

ScenarioB: for a particular interval, I do see two executions across two machines.

Specifically, I did not expect 07:42:00 (etc.) to be executed on both machines 1 and 2.

On machine 1, image

On machine 2, image

Known workarounds

Provide a description of any known workarounds.

Related information

Provide any related information

  • Package version
  • Links to source

AAATechGuy avatar Mar 11 '22 07:03 AAATechGuy

Hi @AAATechGuy Thank you for the feedback. We will discuss this issue internally and let you know about the findings!

Ved2806 avatar Mar 22 '22 14:03 Ved2806

Hey folks, did you figure out anything about this? We experienced the same issues as we scaled a WebJobs SDK project deployed into Azure to multiple App Services instances.

robertmclaws avatar May 10 '22 17:05 robertmclaws

Hi @fabiocav Need your help with this.

Ved2806 avatar Nov 03 '22 13:11 Ved2806