简体   繁体   中英

Making a Webjob a singleton using multiple deployments using Web Apps in Azure

With Azure WebApps I know I can make a WebJob a singleton by adding the "is_singleton": true to the WebJob settings.job file, and this works great if I have for example, 3 instances in a single WebApp deployment.

However - how can I publish to two WebApps (use case, two different regions) and make the WebJob run as a singleton and only on one of the deployments.


Example of desired behavior:

Sydney Deployment

  • My Singleton Webjob (Running)
  • My other WebJobs that process a queue (Running)

Singapore Deployment

  • My Singleton Webjob (Not running, or perhaps not even deployed?)
  • My other WebJobs that process a queue (Running)

I know I can log in to the azure portal and disable the web job in one of the deployments, but is there a transform or change I can make to my publish profiles in visual studio to achieve the desired behavior automatically?

I'm currently deploying with Visual Studio 2015.

Actually there's a pretty handy Singleton attribute which may be useful in your use-case, take a look at this , excerpts below;

The WebJobs SDK facilitates common distributed locking scenarios via its SingletonAttribute. You can simply apply SingletonAttribute to a job function to ensure that all invocations of that function will be serialized, even across scaled out instances.

[Singleton]
public static async Task ProcessImage([BlobTrigger("images")] Stream image)
{
     // Process the image
}

There's also an example of how you can scope the Singleton down to a region:

You can specify a scope expression/value on the Singleton which will ensure that all executions of the function at that scope will be serialized.

[Singleton("{Region}")]
public static async Task ProcessWorkItem([QueueTrigger("workitems")] WorkItem workItem)
{
     // Process the work item
}

From the point of view of the sites, those are two completely independent sites and so the webjobs in them will be independent of each other. If you want to make them a singleton you'll have to add some custom logic.

Options include:

  1. Set an app setting in one of the two sites and tell the webjob to only execute if that setting is set
  2. Use Azure blob storage to create a lock file, and the webjob that acquires the lock is the only one that can run (this let's your webjob fail over to a second region the first one fails)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM