A
A
Andrew2019-05-03 22:23:45
ASP.NET
Andrew, 2019-05-03 22:23:45

How to properly perform regular operations on the site when using slots?

A site (App Service) is used on Azure.
For deployment without interrupting the service, two slots are used - the new version is uploaded to the second slot, and then there is a switch between slots.
Once a day, you need to do some operations, for this, when you start the application, the following code is launched

// В Global.asax
protected void Application_Start(){
   // ...
   var t = new Thread(new SomeClass().SomeMethod);
   t.Start;
   // ...
}
// В SomeClass
public void SomeMethod(){
   while (true) {
      // Проверяем что наступили новые сутки и делаем некую работу
      Thread.Sleep(TimeSpan.FromHours(1);
   }
}

The code works as it should, always on time, there are no problems with it.
But, there is a problem due to the fact that there are two deployment slots, and accordingly two instances of the site work. They fire at the same time, and this work is done twice.
I tried to add the IsProduction parameter to the application's combat slot parameters and check it at the start of the task
if (Environment.GetEnvironmentVariable("IsProduction") == null || Environment.GetEnvironmentVariable("IsProduction") != "true") return;

It works, but sometimes, for some reason, it does not work, I suspect, after switching between slots. Sometimes, after switching between slots, the second slot considers itself to be combat (IsProduction = true), and the code works twice again.
I thought about adding WebJobs to the site, but the problem is that these web jobs are switched along with the slot. Those. by adding a task to one slot after switching, this task will go to the second slot. And since If the slot is not in use, then its operation is not guaranteed, it may be unloaded from memory along with the web job.
What is the correct way to implement these regular operations only once when using slots?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2019-05-04
@kttotto

There are no particular options here, as soon as you use an external one for both applications, but a common store for them: database, just a file on disk, a third-party service, pulling which will check the last time the task was launched.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question