The following is an example that demonstrates how I was able to successfully schedule jobs.
Start by implementing the IJob interface to create a job that can be managed by Quartz.Net.
1 2 3 4 5 6 7 |
public class DailyJob : IJob { public void Execute(IJobExecutionContext context) { //Do your daily work here } } |
Then in the RoleEntryPoint OnStart() method create and configure the scheduler with a trigger and the daily job. This is the part that was greatly lacking in documentation. To get this working I had to poke around the available methods and objects
using IntelliSense. Once you have created the scheduler, be sure to keep a reference to it in your Worker Role instance. This will ensure that your jobs execute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
public class WorkerRole : RoleEntryPoint { public override void Run() { //default project template implementation while (true) { Thread.Sleep(10000); Trace.WriteLine("Working", "Information"); } } public override bool OnStart() { ServicePointManager.DefaultConnectionLimit = 12; ConfigureScheduler(); return base.OnStart(); } public override void OnStop() { sched.Shutdown(false); sched = null; base.OnStop(); } IScheduler sched; private void ConfigureScheduler() { var schedFact = new StdSchedulerFactory(); sched = schedFact.GetScheduler(); var job = new JobDetailImpl("DailyJob", null, typeof(DailyJob)); var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); var cronScheduleBuilder = CronScheduleBuilder.DailyAtHourAndMinute(17, 15) .InTimeZone(timeZoneInfo); var trigger = TriggerBuilder.Create() .StartNow() .WithSchedule(cronScheduleBuilder) .Build(); sched.ScheduleJob(job, trigger); sched.Start(); } } |
When configuring a time based job, its quite important to keep in mind that all Windows Azure servers utilize UTC time, and ‘en-US’ locale settings. It’s imperative that you make sure that your job is scheduled to execute in the right time zone. Use TimeZoneInfos to get the correct time zone info object.
1 2 3 4 5 6 7 8 |
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); var cronScheduleBuilder = CronScheduleBuilder.DailyAtHourAndMinute(17, 15) .InTimeZone(timeZoneInfo); var trigger = TriggerBuilder.Create() .StartNow() .WithSchedule(cronScheduleBuilder) .Build(); |