/// <summary> /// Updates job /// </summary> /// <param name="jobName"></param> /// <param name="message"></param> public static void UpdateJob(string jobName, string message, Enumerations.MaintenanceJobType jobType, string jobMailSubject = "", string jobMailBody = "") { log.Info("About to update local job"); log.Info("Getting related trigger"); ICronTrigger realtedTrigger = GetCronTriggerForJob(GetAllJobs().FirstOrDefault(jd => jd.Key.Name == jobName).Key); log.Info("Trigger retrived successfuly"); log.Info("Creating new job instance"); IJobDetail newJob = CreateJobBasedOnItsType(jobName, message, jobType, jobMailSubject, jobMailBody); log.Info("Job instance created"); Quartz.Collection.ISet <ITrigger> trigger = new Quartz.Collection.HashSet <ITrigger>(); try { log.Info("Creating trigger for a newJob"); trigger.Add(TriggerBuilder.Create() .ForJob(newJob) .WithIdentity(realtedTrigger.Key.Name) .WithCronSchedule(realtedTrigger.CronExpressionString) .StartAt(realtedTrigger.StartTimeUtc) .Build()); } catch (Exception ex) { log.Error(ex.Message, ex); } log.Info("Replacing job and trigger"); try { scheduler.ScheduleJob(newJob, trigger, true); } catch (SchedulerException SchEx) { log.Error(SchEx.Message, SchEx); } log.Info("Job and trigger successfuly replaced"); }
private static IJobDetail CreateJobBasedOnItsType(string jobName, string jobMessage, Enumerations.MaintenanceJobType jobType, string jobMailSubject = "", string jobMailBody = "") { log.Info("Checking job type"); if (jobType == Enumerations.MaintenanceJobType.Local) { return(CreateLocalNotifierJob(jobName, jobMessage)); } else if (jobType == Enumerations.MaintenanceJobType.Mailing) { return(CreateMailingNotifierJob(jobName, jobMessage, jobMailSubject, jobMailBody)); } else { return(null); } }
/// <summary> /// Schedules job using assigned cron trigger /// </summary> /// <param name="jobName"></param> /// <param name="jobMessage"></param> /// <param name="triggerName"></param> /// <param name="cronExpression"></param> public static bool ScheduleJobWithCronTrigger(string jobName, string jobMessage, Enumerations.MaintenanceJobType jobType, string triggerName, string cronExpression, DateTimeOffset startDate, string jobMailSubject = "", string jobMailBody = "") { if (jobType == Enumerations.MaintenanceJobType.Local) { log.Info("Prepering to schedule local job..."); try { scheduler.ScheduleJob(CreateLocalNotifierJob(jobName, jobMessage), CreateCronTrigger(triggerName, cronExpression, startDate)); } catch (SchedulerException SchEx) { log.Error(SchEx.Message, SchEx); } log.Info("Local job successfuly scheduled"); return(true); } else if (jobType == Enumerations.MaintenanceJobType.Mailing) { log.Info("Prepering to schedule mailing job..."); try { scheduler.ScheduleJob(CreateMailingNotifierJob(jobName, jobMessage, jobMailSubject, jobMailBody), CreateCronTrigger(triggerName, cronExpression, startDate)); } catch (SchedulerException SchEx) { log.Error(SchEx.Message, SchEx); } log.Info("Mailing job successfuly scheduled"); return(true); } return(false); }