What is the difference between SingletonScope. Function & Host in Azure WebJob - azure

What is the difference between SingletonScope? Function & Host in Azure WebJob?
I have Time trigger hosted in two regions in AKS. I want to ensure that only one instance runs every time.
All my instances are sharing the same storage account. Can I use one of the above settings to configure my scenario?

Difference between WebJob host and Azure function:
The host is a runtime container for functions. The Host listens for triggers and calls functions.
In version 3.x, the host is an implementation of IHost.
In version 2.x, you use the JobHost object.
You create a host instance in your code and write code to customize its behavior.
This is a key difference between using the WebJobs SDK directly and using it indirectly through Azure Functions.
In Azure Functions, the service controls the host, and you can't customize the host by writing code.
Azure Functions lets you customize host behavior through settings in the host.json file.
For more information, see Compare the WebJobs SDK and Azure Functions
What is Singleton?
The Singleton attribute ensures that only one instance of a function runs, even when there are multiple instances of the host web app. The Singleton attribute uses distributed locking to ensure that one instance runs.
As in below example, only a single instance of the ProcessImage function runs at any given time:
[Singleton]
public static async Task ProcessImage([BlobTrigger("images")] Stream image)
{
// Process the image.
To learn more about how the SingletonMode.Function works see: https://github.com/Azure/azure-webjobs-sdk/blob/master/src/Microsoft.Azure.WebJobs/SingletonMode.cs
Further you can specify a scope expression/value on a singleton. The expression/value ensures that all executions of the function at a specific scope will be serialized.
Check this link for example: https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#scope-values

Related

Azure functions ActivityTrigger and HttpTrigger lock

Can I safely use lock{} inside Azure AcitityTrigger and HttpTrigger functions or I have to work it around?
While I don't see why it shouldn't work, note that this would work only in the context of a single instance and won't hold when your app scales out.
Across function app instances, you will have to go for a distributed lock like using storage blob leases.
Also, it's better to leverage a queue (like Service Bus) with appropriate concurrency config to ensure not hitting timeouts.

One Azure Function in one repo deployed in multiple Azure App Services

Can I deploy one Azure time trigger function from one repo to multiple App Services?
Example:
Currently I have a repo with one Azure function in it (name Function1, runs every few mins).
I have 5 customers, I have a database for each customer and therefore I have 5 connection strings. Each customer requires me to host the function in isolated environment independent from the other customers.
The function "Function1" does the same logic for each of my customers. It just accesses a different database for each using the different connection string.
Therefore, I created 5 App Services: Function1-Customer1, Function1-Customer2, ... to satisfy the "independent environment requirement".
Each App Service has the unique db connection string assigned in the App Settings.
I tried to deploy the "Function1" to all these 5 App Services. However, when then going to see the Log Stream for any of the App Services it seems that only one instance of that function is running, depending on which App Service deployed last.
So for example, if Function1-Customer1 deployed last and I go to Function1-Customer2 or Function1-Customer3 to see the Log Streams, both outputs a conn string of Function1-Customer1. If Function1-Customer2 deployed last then I would see its conn string in all other App Services.
Is it possible to deploy the Function1 to serve all these 5 App Services? Or do I need a different architecture here?
The functions coordinate by obtaining leases in the underlying blob storage. If two function apps end up fighting over the same lease, they will block each other even though they are supposed to do different things. You can explore this by looking at the blobs in the underlying storage account an check the "lease" status.
Based on our discussion in the comments, I would recommend to use a dedicated storage account for each function app. I would not recommend AzureFunctionsWebHost__hostid or similar solutions, since it adds more complexity.
For each trigger Azure function manages it's own queue in Azure Queue Storage. You can use single function app and trigger 5 different tasks for each customer or you can create separate Azure storage account for each function app.

Do Azure Function from Same App Service Run in Same Instance

I have a scenario where one of my Class has a static member whose value I can set from a Function App. Suppose another Function App belonging to same App Service Plan also uses this same class and also sets/relies on the value of the static member. Now, if the two function apps never run simultaneously, we have no issue. Also, if they don't run under same instance, then we also have no issue as each running instance will have their own definition of the class.
My question is, do each Function App from same App Service run on different instance?
I understand redesigning will eliminate any possible issues, but I am just curious on the interaction between Function Apps in same App Service.
I think it will be better for you to understand the process boundaries.
You have an Azure Function App (FuncApp1.azurewebsites.net) that may have one or more functions hosted on an App Service Plan. You're mentioning that you have another Azure Function App (FuncApp2.azurewebsites.net) with one or more of it's own set of functions hosted on the same App Service Plan. Is this correct?
FuncApp1 will have it's own process id, memory/cpu consumption, and threads on some machine in the cloud and the same for FuncApp2. The static member basically exists independently in two separate programs. In the case where your function app may scale out, each new instance will most likely have it's own process id, etc.
So NO your function apps will not step on each other. Your only concern should be if the static member is thread safe within the same process id. In what scenario are you relying on a static member that may be altered per request? Some incrementing ID?
My question is, do each Function App from same App Service run on
different instance?
I don't think this is possible, just as others have answered. All function apps are run on all the instances provisioned by your App Service Plan. But.. having said that, using per app scaling it is possible to limit the number of instance that a particular app service can be run on during scale out. I have not used this for Function Apps backed by App Service Plan; but an interesting read [https://learn.microsoft.com/en-us/azure/app-service/manage-scale-per-app]
From MS docs
When using App Service, you can scale your apps by scaling the App
Service plan they run on. When multiple apps are run in the same App
Service plan, each scaled-out instance runs all the apps in the plan.
Per-app scaling can be enabled at the App Service plan level to allow
for scaling an app independently from the App Service plan that hosts
it. This way, an App Service plan can be scaled to 10 instances, but
an app can be set to use only five.
Further, to your query on my comment; I think there are two cases here
a) Multiple Function Apps using the same App Service Plan using shared static class with static member
b) Multiple functions within the same Function App using shared static class with static member.
In both cases your static members are not shared. They are per function scope only. Just to be sure, I created two functions, FunctionA and FunctionB under a Function App (FunctionApp1). In both the functions, I refer to a static class Static1. I noticed that changes I make to the static members from FunctionA is not visible to FunctionB.
However, the static member state is preserved between multiple invocations of the same function under the Function App
I found a somewhat related question here https://stackoverflow.com/a/44971720/5344880
one another case would be different Function Apps using the same shared static class and I am reasonably sure that in this case the static class's static member's state is not shared.
Each function app from same App Service plan will run on every instances.
For example, if you have an app service plan which has 3 instances, your function app will run on each instances.
So for your question
do each Function App from same App Service run on different instance?
The answer is no, function apps from same App Service will run on the same instance.

Access Azure WebApp Object From Azure WebJob

We have a Static Class in the WebApp that contains a static dictionary of current sessions and username. We need to have access to the data in the dictionary in the WebJob as we want to update data based on who currently has active sessions. The webJob runs every 5 minutes and needs to have the current list of sessions/users.
I can access the dictionary from the webjob but its always null. We have logging in the webApp that verifies there are entries in the dictionary but when the webjob accesses the dictionary its null.
How can I get that object in the webJob and get its data? Do we need to use Azure Storage (Queue/Table) for this to work?
An "Azure AppService" is hosted on an "AppService Plan", which in turn consists of a number of virtual machines. WebJobs ("your.webjob.exe") and WebApps(usually "w3wp.exe") are completely independent processes on theses systems. They may run on the same machine, but there is no guarantee for it. Either way, communication between them would be difficult and can definitely not be achieved by using a common static variable.
For your use case, you should use a common storage. Azure Storage could work, but Azure Redis Cache or simple SQL might also do the trick. Depends on your framework and requirements.

How can I configure an Azure Function triggered by Service Bus with a custom INameResolver?

I want to be able to control the name of the Service Bus Queue or Subscription that my Azure Function reads from at run-time.
With WebJobs (which Azure Functions are based on) you could do this by implementing and configuring a custom INameResolver, see: How to have a configuration based queue name for web job processing?
However, with Azure functions I have no access to JobHostConfiguration to wire up this custom resolver.
Can I still use an INameResolver, and if so how?
Right now you cannot use a custom INameResolver as there is no mechanism for injecting your own services like this into the host. It's being tracked here: https://github.com/Azure/azure-webjobs-sdk-script/issues/1579

Resources