Search code examples
azureazure-webjobsazure-servicebus-queuesazure-webjobssdk

How do I execute same webjob method multiple time only on one trigger message?


I have one azure webjob MyTestWebJob

inside Function.cs

 public static void ProcessQueueMessage([QueueTrigger("Testqueue")] string message, TextWriter log)
        {
            log.WriteLine(message);
        }

I want to execute ProcessQueueMessage multiple time because I want to make my application multi tenant so for every tenantId it wil execute it when Testqueue has message in.

basically how can I achieve multi tenancy using azure webjob?

Can anyone give me hint or customer trigger code ?


Solution

  • If you want to parallelize the processing for each tenant there are a number of techniques.

    1. Multi-threading a single webjob.

    Similar to the code that @viperguynaz provided but just put the logic that processes for each tenant into its own thread. There are tons of different techniques for doing this. The downside to this is because each thread will be executing async you will lose some of the niceties of the web job dashboard.

    1. Create a webjob chain

    For verbs that need to be executed once for every tenant you can create a job whose job is to take a single queue item and explode it as a new queue item for each tenant. You would have one web job responsible for exploding the action across all tenants and then another web job that processes the action for each tenant. The benefit of this approach is that its super clean (your logic for splitting the tenant actions is separate from the actual logic being performed) and processing can be executed synchronously allowing for the web job dashboard to work as expected. A downside to this approach is you will get a little bit of latency in the kicking off of the 'exploded' tasks but that shouldn't matter too much right? After all, its a background process anyway right? :)

    By default, a web job pulls a batch of 16 items off the queue to run them in parallel. You can tweak those settings. Here is a URL for more details on that:

    https://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-webjobs-sdk-storage-queues-how-to/#config