6

We have an Azure function that is supposed to be handling several service bus triggers at the same time and what I assume is happening is that it is being split across several instances which is causing some concurrency problems on our end.

We need our function to act as a singleton so we can process requests one at a time without any collisions. From what we looked into in this article (https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#singleton-attribute) we should be able to accomplish this.

Our function looks like this:

[Microsoft.Azure.WebJobs.Singleton(Mode = SingletonMode.Listener)]
[FunctionName("AccountCreatedSubscriber")]
public static void Run([ServiceBusTrigger("accountscontacts-account-created", "license-keys", Connection = "FBISEventBus")]BrokeredMessage message, ILogger log)
{
    log.LogInformation($"{{ Message received from accountscontacts-account-created topic }}");

    // Do Work
    log.LogInformation($"{{ Message sent to account creation handler }}");
}

And for backup we also have this in our host.json file,

{
  "serviceBus": { "maxConcurrentCalls": 1 }
}

But for whatever reason our functions are still running parallel. Any ideas?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tokyo0709
  • 1,897
  • 4
  • 28
  • 49

2 Answers2

14
  1. If your function is on Consumption plan, set WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT to 1 in Application settings.

  2. Check your Azure Function runtime version in portal(Platform features> Function app settings). If it's ~2, we need to modify service bus setting in host.json as below.

    {
        "version": "2.0",
        "extensions": {
            "serviceBus": {
                "messageHandlerOptions": {
                   "maxConcurrentCalls": 1
                }
            }
        }
    }
    
Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
  • Sorry, we had tried this at first and I thought it didn't do anything for us but it was a different issue I realized and this did seem to help with some of the concurrency we were dealing with thank you. – tokyo0709 Jan 08 '19 at 03:59
0

Testing I get the following to work:

  1. If your function is on Consumption plan, set WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT to 1 in Application settings.
  2. [Microsoft.Azure.WebJobs.Singleton(Mode = SingletonMode.Function)]

Also since we only have one instance running (thanks to WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT = 1) the running instances are sharing the same static variables.

Gil Roitto
  • 325
  • 2
  • 8