Great work on the recent update. I have a quick question about setting
a job to run immediately with a CronTrigger. I have something like
this:
JobDetail job = new JobDetail("job", "grp", typeof(SimpleJob));
CronTrigger trigger = new CronTrigger("trig", "grp", "job1", "grp",
DateTime.Now, null, "0 0 0 * * ?");
sched.ScheduleJob(job, trigger);
What I'm aiming for is the job to start immediate when the scheduler
has started and then fire off repeatedly at 12AM each day. I used the
example that came with the Quartz.NET 0.5 source but the result is not
what I desired. It job still waits until the first fire time of the
cron expression.
Is there another way to handle this or am I being dense?
Thanks in advance,
Kelsen
JobDetail job = new JobDetail("job", "grp", typeof(SimpleJob));
CronTrigger trigger = new CronTrigger("trig", "grp", "job", "grp",
DateTime.Now, null, "0 0 0 * * ?");
sched.ScheduleJob(job, trigger);
This is just the nature of CronTrigger. It has the advice about
running, it checks whether the rule is true for given time searching
for next fire time _after_ the time it started. And with your rule,
the next fire time will occur next midnight.
If you want to make sure your job is run immediately you can set start
time to one day before DateTime.Now, so you change your code to:
CronTrigger trigger = new CronTrigger("trig", "grp", "job", "grp",
DateTime.Now.AddDays(-1), null, "0 0 0 * * ?");
Quartz will see that it missed on run and will fire the job
immediately. After that, jobs will run in the given interval. Under
the hood, this actually is caused by
Trigger.MISFIRE_INSTRUCTION_SMART_POLICY, which is translated in
CronTrigger to CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW. You can
also set this value to CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING
which suppresses the fire until the next real fire time. These fields
are private in 0.5 version accidentally but will be public in next
release. You can always set these with integers (ugly):
/// <summary>
/// Instructs the <see cref="IScheduler" /> that upon a mis-fire
/// situation, the <see cref="CronTrigger" /> wants to be fired now
/// by <see cref="IScheduler" />.
/// </summary>
public const int MISFIRE_INSTRUCTION_FIRE_ONCE_NOW = 1;
/// <summary>
/// Instructs the <see cref="IScheduler" /> that upon a mis-fire
/// situation, the <see cref="CronTrigger" /> wants to have it's
/// next-fire-time updated to the next time in the schedule after the
/// current time (taking into account any associated <see cref="ICalendar" />,
/// but it does not want to be fired now.
/// </summary>
public const int MISFIRE_INSTRUCTION_DO_NOTHING = 2;
And from the documentation: "Be careful when setting fire times
between mid-night and 1:00 AM - "daylight savings" can cause a skip or
a repeat depending on whether the time moves back or jumps forward."
-Marko
Kelsen