Immediate start of CronTrigger job

2,880 views
Skip to first unread message

Kels...@gmail.com

unread,
Jun 22, 2007, 12:51:02 PM6/22/07
to Quartz.NET
Hello -

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

Kels...@gmail.com

unread,
Jun 22, 2007, 12:52:47 PM6/22/07
to Quartz.NET
Yes, there is a typo in the trigger instantiation. Sorry I thought I
has cleaned it up but it should read:

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);

Marko Lahma

unread,
Jun 24, 2007, 3:24:53 AM6/24/07
to quar...@googlegroups.com
Hi,

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

Kels...@gmail.com

unread,
Jun 29, 2007, 10:06:04 AM6/29/07
to Quartz.NET
Thanks a lot Marko for the detailed explanation. It cleared up
everything.

Kelsen

Reply all
Reply to author
Forward
0 new messages