public void ScheduleTask(ScheduledTask task) { if (task.ScheduledTaskId == default(long)) { throw new ArgumentException("Scheduling a task requires the task to be saved to the database and have a ScheduledTaskId assigned."); } JobKey jobKey = JobKey.Create(task.TaskName, JobKey.DefaultGroup); IJobDetail job; if (!m_QuartzScheduler.CheckExists(jobKey)) { JobDataMap jobDataMap = new JobDataMap(); jobDataMap.Add("connectionString", m_Controller.ConnectionString); jobDataMap.Add("threadSynchronizer", m_ThreadSynchronizer); jobDataMap.Add("scheduledTaskId", task.ScheduledTaskId); job = JobBuilder.Create <ExecuteSequenceJob>() .WithIdentity(jobKey) .SetJobData(jobDataMap) .StoreDurably() .Build(); m_QuartzScheduler.AddJob(job, false); ExecuteSequenceJobListener jobListener = new ExecuteSequenceJobListener(task.TaskName); jobListener.TaskStart += jobListener_TaskStart; jobListener.TaskComplete += jobListener_TaskComplete; m_QuartzScheduler.ListenerManager.AddJobListener(jobListener, KeyMatcher <JobKey> .KeyEquals(jobKey)); } else { job = m_QuartzScheduler.GetJobDetail(jobKey); m_QuartzScheduler.UnscheduleJobs(m_QuartzScheduler.GetTriggersOfJob(jobKey) .Select(t => t.Key) .ToList()); } if (task.Enabled) { task.NextScheduledRunTimeUtc = DateTime.MaxValue; foreach (TriggerBuilder triggerBuilder in TriggerBuilderFactory.CreateForSchedule(task.TaskName, task.RunSchedule)) { ITrigger trigger = triggerBuilder .ForJob(job) .Build(); DateTimeOffset nextScheduledRunTimeUtc = m_QuartzScheduler.ScheduleJob(trigger); if (nextScheduledRunTimeUtc.DateTime < task.NextScheduledRunTimeUtc.Value) { task.NextScheduledRunTimeUtc = nextScheduledRunTimeUtc.DateTime; } } } else { task.NextScheduledRunTimeUtc = null; } m_Controller.SetScheduledTaskNextScheduledRunTime(task.ScheduledTaskId, task.NextScheduledRunTimeUtc); }
public override void JobToBeExecuted(Quartz.IJobExecutionContext context) { base.JobToBeExecuted(context); m_IsJobRunning = true; long scheduledTaskId = (long)context.MergedJobDataMap["scheduledTaskId"]; ScheduledTaskRunStatistics lastRunStatistics = new ScheduledTaskRunStatistics(); lastRunStatistics.StartTimeUtc = context.FireTimeUtc.HasValue ? context.FireTimeUtc.Value.DateTime : new DateTime?(); lastRunStatistics.EndTimeUtc = null; lastRunStatistics.Status = ScheduledTaskStatus.Running; lastRunStatistics.Error = null; DateTime?nextScheduledRunTimeUtc = context.NextFireTimeUtc.HasValue ? context.NextFireTimeUtc.Value.DateTime : new DateTime?(); string connectionString = (string)context.MergedJobDataMap["connectionString"]; SchedulingController controller = new SchedulingController(connectionString); controller.SetScheduledTaskLastRunStatistics(scheduledTaskId, lastRunStatistics); controller.SetScheduledTaskNextScheduledRunTime(scheduledTaskId, nextScheduledRunTimeUtc); zOnTaskStart(new TaskEventArgs(scheduledTaskId)); }