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 void RemoveTask(ScheduledTask task) { IJobListener jobListener = m_QuartzScheduler.ListenerManager.GetJobListener(task.TaskName); if (jobListener != null) { //Following should always be true, but checking anyway in case the use of jobs is expanded if (jobListener is ExecuteSequenceJobListener) { ExecuteSequenceJobListener executeSequenceJobListener = (ExecuteSequenceJobListener)jobListener; executeSequenceJobListener.TaskStart -= jobListener_TaskStart; executeSequenceJobListener.TaskComplete -= jobListener_TaskComplete; } m_QuartzScheduler.ListenerManager.RemoveJobListener(task.TaskName); } JobKey jobKey = JobKey.Create(task.TaskName, JobKey.DefaultGroup); if (m_QuartzScheduler.CheckExists(jobKey)) { m_QuartzScheduler.DeleteJob(jobKey); } }