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?
0
votes
Why not leave your web job where it is? You can move your web application to a WebRole if you want and still run your web job in the web app.
– Rick Rainey
That can be done, but wont that will be like keeping the web app running just for the sake of Web Job? We could have retired the web app completely if we can move the web job logic in worker role. Is it a good practice to keep both the web app and web role running?
– Pratik Bhattacharya
Yes, you would keep the web app to support the web job. This is perfectly fine to do while still moving your web front end to a WebRole.
– Rick Rainey
Thanks Rick.....will do as you advised.
– Pratik Bhattacharya
1 Answers
2
votes
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();