How would I scale a timer triggered webjob on an Azure App service to multiple app service instances?

Gareth Terrace 0 Reputation points
2024-05-30T11:19:10.9866667+00:00

Basically we've got a scheduled job that currently runs as a webjob on a single instance. We have two app service instances and this job is starting to take a while to run.

I'd like to run it on both app service instances on the same timer trigger (we can make it split up the work ourselves so nothing gets duplicated, that's not an issue). If I remove the [Singleton] attribute and run webjobs on both app services, I still only see one instance run.

Is there a way to do this with continous and timer triggered webjobs, or does the presence of the timer trigger force it to run as a singleton?

Thanks

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,008 questions
{count} votes

1 answer

Sort by: Most helpful
  1. ajkuma 27,261 Reputation points Microsoft Employee
    2024-05-31T06:18:59.4066667+00:00

    @Gareth Terrace, Based on my understanding of your scenario, you have removed the [Singleton] attribute.

    Firstly, just to highlight, providing some background:

    • By default, timer triggered webjobs in Azure App Service run as singletons, meaning that only one instance of the job will run at a time. This is to prevent multiple instances of the job from running simultaneously and potentially causing conflicts or other issues.
    • Continuous WebJobs run on all instances that the web app runs on, and you can optionally restrict the WebJob to a single instance. Triggered WebJobs, on the other hand, run on a single instance selected for load balancing by Azure.
    • Typically, once you have disabled the singleton behavior, you may run the webjob on multiple instances of your App Service. However, you will need to ensure that the job is split up correctly so that each instance is processing a different set of data or tasks.

    I understand you have mentioned this implementation, it's worth noting that disabling the singleton behavior can lead to issues if the job is not designed to run on multiple instances. For example, if the job is not designed to handle concurrent access to shared resources, you may run into issues when running the job on multiple instances.

    1. -To ensure that a function runs as a singleton on a single instance when the web app scales out to multiple instances, you can also apply a listener-level singleton lock on the function ([Singleton(Mode = SingletonMode.Listener)]).
    2. A web app can time out after 20 minutes of inactivity, and only requests to the actual web app can reset the timer.
      -If you haven't done, enable the Always on setting on your web app's Azure Configuration page.
    3. Settings usable only on Triggered WebJobs: schedule: run the WebJob on a CRON scheduleSettings usable only on Continuous WebJobs.

    Behind the scenes, TimerTrigger
    uses the Singleton feature of the WebJobs SDK to ensure that only a single instance of your triggered function is running at any given time. When the JobHost starts up, for each of your TimerTrigger functions a blob lease (the Singleton Lock) is taken.

    • Alternatively, based on your requirement, Azure Functions provides another way to run programs and scripts, which might offer more flexibility in scaling and distribution - which provides more granular control over scaling and distribution.

    Refer these docs for more info:

    Run background tasks with WebJobs in Azure App Service
    Leverage WebJobs API
    How to use the Azure WebJobs SDK for event-driven background processing

    Apologies for the long post, tried to highlight some background info, if you have more questions, please let us know, I'll be more than happy to follow-up further.


    If the answer helped (pointed you in the right direction) > please click Accept Answer - it will benefit users in finding the right answer quickly.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.