Pratik Bhattacharya
Pratik Bhattacharya

Reputation: 3746

Mimicking Schedule function in Webjob in Worker Role

I have an Azure web app that includes some web jobs to run some background tasks. These tasks need to run on a schedule (every 6 hours). Using a WebJob it was pretty easy to achieve the goal. However, recently we decided to use Web Roles instead of the Web App. For running the background tasks I have been looking at using Worker Roles in-place of WebJobs. However I am facing issues in scheduling the tasks.
How do I schedule the tasks in the worker role? Moreover, since I am using multiple instances of the Cloud Service, do I need to take some extra precautions to ensure that only a single instance of the worker role run the tasks at one point of time?

Upvotes: 0

Views: 284

Answers (1)

mathewc
mathewc

Reputation: 13558

You can use the Azure WebJobs SDK in a Worker Role to schedule tasks. The SDK includes a TimerTrigger extension (details here) that can be used run functions on schedule. For example you can simply write a function:

// Runs once every 6 hours
public static void TimerJob([TimerTrigger("06:00:00")] TimerInfo timer)
{
    Console.WriteLine("Timer job fired!");
}

Your startup code would look like:

JobHostConfiguration config = new JobHostConfiguration();
config.UseTimers();

JobHost host = new JobHost(config);
host.RunAndBlock();

Upvotes: 2

Related Questions