I have included Quartz.net library in my C#.Net website to send emails automatically based on some conditions. I have started the job to with RepeatForever(). I am getting the settings for the scheduler from database. Now when the job has already started is there anyway to check if the settings for the scheduler in the database have changed so i should also update them in my method so that scheduler runs with the new settings dynamically? So far I have done this:
public void StartJob()
{
var reminderSettings = GetReminderSettings(); //getting these settings from database
var count = GetUnpaidInvoicesListCount();
var intervalBtwnReminders = 0;
if (reminderSettings?.RemindersGap != null)
{
intervalBtwnReminders = (int)(reminderSettings.RemindersGap);
}
ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler sched = schedFact.GetScheduler();
sched.Start();
// create job
IJobDetail job = JobBuilder.Create<EmailJob>()
.WithIdentity("job1", "group1")
.Build();
// create trigger
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.WithSimpleSchedule(x =>
{
x.WithIntervalInMinutes(intervalBtwnReminders ).RepeatForever();
})
.Build();
sched.ScheduleJob(job, trigger);
}
I wrote a set of methods to make it more easy. You can't edit an existing trigger with your new interval, but you can replace your existing trigger with a new one. The
CreateTrigger
method creates a new Trigger based on a old one and transfers theTimesTriggered
count.How to use it
In this example, the Scheduler is already running.