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?
Asked
Active
Viewed 275 times
0

Rick Rainey
- 11,096
- 4
- 30
- 48

Pratik Bhattacharya
- 3,596
- 2
- 32
- 60
-
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 May 27 '15 at 13:28
-
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 May 27 '15 at 13:56
-
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 May 27 '15 at 14:03
-
Thanks Rick.....will do as you advised. – Pratik Bhattacharya May 27 '15 at 14:17
1 Answers
2
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();

mathewc
- 13,312
- 2
- 45
- 53
-
One another problem that I was facing is that there were 2 instances of the Worker Role, both the instances were trying to trigger the job. That was causing some unwanted issues, is there way how it can be configured that only 1 instance triggers the job. – Pratik Bhattacharya Dec 09 '15 at 16:07
-
Timer trigger does Singleton locking behind the scenes (using Blob leases). This guarantees that only a single instance of your scheduled function is running across multiple instances. – mathewc Dec 09 '15 at 18:13