I have a Azure Function that handles SharePoint operations. Due to throttling I only want 10 concurrent functions running at the time and always 10 functions concurrently. I was considering to use functions with an Azure Service Bus Queue. Is there any build in the Azure platform to achieve this? Service Bus is not a requirement, so if other hubs or queues are better for this. I have looked in the Azure UI and havent found anything on the service bus side or function.
Test and observations:
I created a test with no success. I have created and deployed a functions that sleeps for 20 secs and then write a document to Cosmos DB:
[FunctionName("SleepFunction")]
public static void Run([ServiceBusTrigger("provision", AccessRights.Manage, Connection = "AzureWebJobsServiceBus")]string myQueueItem, TraceWriter log)
{
    System.Threading.Thread.Sleep(20000);
    log.Info($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
    string EndpointUrl = "https://kk-db-governance-engine.documents.azure.com:443/";
    string PrimaryKey = "xx";
    var docClient = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
    var result = docClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri("kk-governance", "test-function-concurrency"), new Run());
}
Functions app.settings:

If I add 10 messages to the queue, 10 documents are added to the database concurrently. I would expect that only 3 at the would be added with 20 secs delayed. Setting the WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT in the app settings does not seem seem to work. Any suggestions?
I also tried the maxConcurrentCalls in the host.json file:
{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsServiceBus": "xxx",
    "AzureWebJobsStorage": "xx",
    "AzureWebJobsDashboard": "xx"
  },
  "version": "2.0",
  "extensions": {
    "serviceBus": {
      "maxConcurrentCalls": "3"
    }
  }
} 
As you can see on screenshot below I use consumption plan:

Answer: I got following working:
"serviceBus": {
    "maxConcurrentCalls": 3
  }
An answer can be found here: Add Singleton support for Functions to ensure only one function running at a time
WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT = 1To allow only X concurrent calls in this instance, you can specify this setting in the host.json file:
{
    "version": "2.0",
    "extensions": {
        "serviceBus": {
            "maxConcurrentCalls": 1
        }
    }
}
For those who find a way to this question, the actual host.json structure has changed since the accepted answer.
To allow only X concurrent calls in this instance, you can specify this setting in the host.json is now:
"extensions": {
    "serviceBus": {
      "prefetchCount": 1,
      "batchOptions": {
        "maxMessageCount": 1
      }
    }
  }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With