Run a single while(true) on Azure

Viewed 112

I'm new to Azure and I need to DoSomeWork() in Azure. This DoSomeWork() should be running periodically, every N minutes or so. However, DoSomeWork() can't be executed twice at the same time. In other words, any DoSomeWork() execution can't start before a prior DoSomeWork() execution finished.

I've been taken a look at Azure Web Jobs, particularly Continuous Azure Web Jobs. This seems the way to go but it's not clear on how to start, especially with the starting code that you get in VS:

static void Main()
{
    var config = new JobHostConfiguration();

    if (config.IsDevelopment)
    {
        config.UseDevelopmentSettings();
    }

    var host = new JobHost(config);
    // The following code ensures that the WebJob will be running continuously
    host.RunAndBlock();
}

There is also Functions class that takes an input a QueueTrigger decorated parameter, but I don't want the code to be triggered by any queue message or so.

How can I get a simple Console.WriteLine("hello world") running e.g. every minute but without overlapping? If Azure Web Jobs is not the way to go, what should I use instead (should be Azure-based)?

2 Answers

As mentioned in the comment, azure webjobs supports TimerTrigger feature(scheduled WebJob) which can be ran every xxx minutes as per your need.

It's quite simple when using azure webjob. For example, in visual studio, just create a console project -> Add this line of code: Console.WriteLine("hello world") -> then build the project -> then zip all the necessary files including the .exe into a .zip file -> at last, upload the .zip file into your webjob, and set the schedule(like execute the code every 5 minutes).

Please refer to this doc for more details about creating a scheduled WebJob.

You can also consider using other azure services which supports timerTrigger feature, like azure function.

WebJobs can be complicated to set up and maintain. Especially when dealing with a CI/CD pipeline. I have a few running right now and am getting ready to move them into one of the following more dependable and maintainable solutions:

The way we set up scheduled work is to use an Azure Function that runs via CRON schedule. It's super dependable and durable since it's managed by Azure. You just set it up, throw your code up and the rest is up to Azure to make sure it fires off when you configured it to.

If you want to do this in your own application, take a look at running a background service in an ASP.NET Core application. You can run a timer in the background service that will fire off and do some work. Keep in mind that if your app scales horizontally, you will be running two timers, which probably isn't good in your situation.

You could do something fancy like setting up a Azure Function to hit an endpoint on your WebAPI at a scheduled time. Then you could send the work to a BackgroundService which is singleton, so you could block a second request if you are currently running your job.

We tend to go the last route. Azure fires off a timer, the function executes, sends a message to an endpoint, the endpoint places work in the background.

There are tons of options outside of what I mentioned, but these are the only ones I have had the privilege to architect.

Related