Issue
I am trying to create a quartz scheduler for cron jobs in Quartz.NET (version 2.4.1.0). I have one job that fires at its specified interval, and another that fires once in the start and never fires again (it also has a specified interval).
I am a little stumped as the only variation in these jobs is their names and CronSchedules (found in the triggers).
Note: I have researched similar problems to this online (as well as Stack Overflow) and I have found nothing that solves my problem.
My DB connection works perfectly (UID/PWD left blank in this example).
All Cron Expressions have been tested in CronMaker and are valid.
App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<quartz>
<add key="quartz.scheduler.instanceName" value ="MyScheduler"/>
<!--5 is recommended for under 100 jobs-->
<add key="quartz.threadPool.threadCount" value ="5"/>
<add key="quartz.jobStore.useProperties" value =" false"/>
<add key="quartz.jobStore.clustered" value ="false"/>
<add key="quartz.jobStore.misfireThreshold" value ="60000"/>
<add key="quartz.jobStore.type" value ="Quartz.Impl.AdoJobStore.JobStoreTX, Quartz"/>
<add key="quartz.jobStore.driverDelegateType" value ="Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz"/>
<add key="quartz.jobStore.tablePrefix" value ="QRTZ_"/>
<add key="quartz.jobStore.dataSource" value ="myDS"/>
<!--DB CONNECTION-->
<add key="quartz.dataSource.myDS.connectionString" value ="Server=localhost;Database=QUARTZ;Uid=;Pwd="/>
<add key="quartz.dataSource.myDS.provider" value ="SqlServer-20"/>
</quartz>
</configuration>
Program.cs
Note: Job 3 is successfully firing every minute. Job 4 fires once at the start and then stops firing (should be every 30 seconds).
Jobs:
public class Job3 : IJob
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine("Job 3");
}
}
public class Job4 : IJob
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine("Job 4");
}
}
Job Details:
IJobDetail jdJob3 = JobBuilder.Create<Job3>()
.WithIdentity("job3", "group3")
.WithDescription("CRON TRIGGER - Job meant to run constantly at 1min intervals")
.StoreDurably(true)
.Build();
IJobDetail jdJob4 = JobBuilder.Create<Job4>()
.WithIdentity("job4", "group3")
.WithDescription("CRON TRIGGER - Job meant to run constantly at 30sec intervals")
.StoreDurably(true)
.Build();
Triggers:
//Day Of Month - fires every minute
ITrigger tChron2 = TriggerBuilder.Create()
.WithIdentity("myTrigger3", "group3")
.WithCronSchedule("0 0/1 * * * ?", x => x
.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time"))
.WithMisfireHandlingInstructionFireAndProceed()) //handle misfire
.ForJob("job3", "group3")
.Build();
//Day Of Month - fires every 30sec, between 8am and 2pm, on the 26th of every month, any year
ITrigger tChron3 = TriggerBuilder.Create()
.WithIdentity("myTrigger4", "group3")
.WithCronSchedule("0,30 * 8-14 26 * ?", x => x
.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time"))
.WithMisfireHandlingInstructionFireAndProceed()) //handle misfire
.ForJob("job4", "group3")
.Build();
Job Listener:
public class MyJobListener : IJobListener
{
void IJobListener.JobExecutionVetoed(IJobExecutionContext context)
{
throw new NotImplementedException();
}
void IJobListener.JobToBeExecuted(IJobExecutionContext context)
{
Console.WriteLine("\r\nJob is about to be executed...");
}
void IJobListener.JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
{
Console.WriteLine("Job was executed...");
}
public string Name { get; set; }
}
//IMPLEMENTATION
MyJobListener myJobListener = new MyJobListener();
myJobListener.Name = "MyJobListener1";
scheduler.ListenerManager.AddJobListener(myJobListener, GroupMatcher<JobKey>.AnyGroup());
OUTPUT
Any ideas on what could be causing this?
Solution
I was able to pinpoint the issue.
I was not properly disposing my data from the database.
After running the project, I would delete all rows first from the QRTZ_TRIGGERS table, and then from the QRTZ_JOB_DETAILS table. This would allow me to run my project without receiving the error: "Unable to Store Job". What I didn't realize was that the QRTZ_CRON_TRIGGERS table was duplicating its fields for every consecutive run (I'm running different examples below):
This was causing conflicts when I would update the cron expressions as multiple instances of the same schedule existed.
To fix this, I utilized "ScheduleJobs" and set the replace value to true:
//JOB SCHEDULE CREATOR
private static void JSCreator(IScheduler scheduler, IJobDetail jdJob, ITrigger tTrig)
{
var SJ = new Dictionary<IJobDetail, Quartz.Collection.ISet<ITrigger>>();
SJ.Add(jdJob, new Quartz.Collection.HashSet<ITrigger>() { tTrig });
scheduler.ScheduleJobs(SJ, true);
}
//IMPLEMENTATION
JSCreator(scheduler, jdJob1, tSimple1);
JSCreator(scheduler, jdJob2, tCron1);
JSCreator(scheduler, jdJob3, tCron2);
JSCreator(scheduler, jdJob4, tCron3);
JSCreator(scheduler, jdJob5, tCron4);
JSCreator(scheduler, jdJob6, tCron5);
This removed any existing data and replaced it with the data found in the job I was currently running. Now all of my jobs are running appropriately.
Answered By - ITSUUUUUH