public override void Run() { while (true) { // connect to the database DatabaseDataContext db = new DatabaseDataContext(); // fetch all scheduled jobs that are overdue and enabled foreach (Schedule s in db.Schedules.Where(z => z.NextOccurrence <= DateTime.UtcNow && z.IsEnabled == true)) { // execute job Job job = new Job(s); Thread jobThread = new Thread(new ThreadStart(job.Execute)); jobThread.Start(); // schedule next occurrence CrontabSchedule schedule = CrontabSchedule.Parse(s.Occurrence); DateTime nextOccurrence = schedule.GetNextOccurrence(DateTime.UtcNow); // calculate new next occurrence s.NextOccurrence = nextOccurrence; db.SubmitChanges(); } // close connection db.Dispose(); // sleep for fifteen seconds Thread.Sleep(15000); } }
public void Add(Job job) { job.Parent = this; Jobs.Add(job); }