MUltiple Timer Triggers are executing same code

Sagar Rallapalli 1 Reputation point
2021-05-13T06:37:48.117+00:00

As same code is getting executed by multiple timer triggers ( Azure Functions), how can i make sure only one code function will executed ?as we have to hit a rest api,we should allow only one function to get executed at any time

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,679 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JayaC-MSFT 5,526 Reputation points
    2021-05-25T12:14:53.733+00:00

    Hello @Sagar Rallapalli , You may consider using WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT and limit it to the minimum. However, remember this will be applied at the function app level.

    You can check system.threading.semaphoreslim or system.threading.mutex and try to handle it in code.

    There are couple of related discussions :
    https://github.com/Azure/azure-functions-host/issues/5972
    https://github.com/Azure/azure-functions-host/issues/1054
    https://stackoverflow.com/questions/52836451/azure-function-app-run-single-azure-function-locally-to-debug

    --------------------------------------------------------------------------------

    UPDATE

    [I have checked this scenario at my end and discussed with the core product team. Sharing the suggestion based on that]

    This is a unique scenario where you want to specify the limit at function level.
    First, we cannot use any Host.json settings as those are host level, not function specific.
    Second, Any batch setting wouldn't be applied to timers.
    Third, w.r.t locking, if the app is scaled out to several instances, different timers/functions may be running on different instances, so it is not advisable to use in memory locking to synchronize.
    Hence, one workaround could be redefining the schedules as per https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=csharp#ncrontab-expressions so that the schedules don't overlap.
    e.g. : The first function runs on the 20 and 40 minute marks of every hour, the second on the 30 minute mark, and the last at the top of the hour.
    crontab.guru

    You may check durable-functions-singletons too.

    Please let me know if this helps!
    If yes , please "Accept the answer" and "Up-vote" so that it helps others in the community.

    0 comments No comments