Gullu
Gullu

Reputation: 3529

how to get the next scheduled trigger fire time in Quartz.net

This is my first Quartz.net project. I have done my basic homework and all my cron triggers fire correctly and life is good. However Iam having a hard time finding a property in the api doc. I know its there , just cannot find it. How do I get the exact time a trigger is scheduled to fire ? If I have a trigger say at 8:00 AM every day where in the trigger class is this 8:00 AM stored in ?

_quartzScheduler.ScheduleJob(job, trigger);
Program.Log.InfoFormat
 ("Job {0} will trigger next time at: {1}", job.FullName, trigger.WhatShouldIPutHere?);

So far I have tried

GetNextFireTimeUtc(), StartTimeUTC and return value of _quartzScheduler.ScheduleJob() shown above. Nothing else on http://quartznet.sourceforge.net/apidoc/topic645.html

The triggers fire at their scheduled times correctly. Just the cosmetics. thank you

Upvotes: 4

Views: 12601

Answers (3)

t3chb0t
t3chb0t

Reputation: 18675

Without scheduling a job you can get it directly via CronExpression which ScheduleJob uses internally:

var next = new CronExpression("0 0 8 1/1 * ? *").GetTimeAfter(DateTimeOffset.UtcNow);

If you want more timestamps then run it in a loop passing the previous timestamp to the next function:


public static class Generator
{
    public static IEnumerable<TItem> Generate<TSource, TItem>(this TSource source, Func<TSource, TItem> first, Func<TSource, TItem, TItem> next)
    {
        var previous = first(source);
        yield return previous;

        while (true)
        {
            var current = next(source, previous);
            yield return current;
            previous = current;
        }
    }
}

var timestamps = new CronExpression("0 0 8 1/1 * ? *").Generate
(
    first: cron => cron.GetTimeAfter(DateTimeOffset.UtcNow),
    next: (cron, previous) => previous.HasValue ? cron.GetTimeAfter(previous.Value) : default
);
var results = timestamps.Where(x => x.HasValue).Take(10);

Upvotes: 0

LeftyX
LeftyX

Reputation: 35587

As jhouse said ScheduleJob returns the next schedule. I am using Quartz.net 1.0.3. and everything works fine.

Remember that Quartz.net uses UTC date/time format.

I've used this cron expression: "0 0 8 1/1 * ? *".

DateTime ft = sched.ScheduleJob(job, trigger);

If I print ft.ToString("dd/MM/yyyy HH:mm") I get this 09/07/2011 07.00
which is not right cause I've scheduled my trigger to fire every day at 8AM (I am in London).

If I print ft.ToLocalTime().ToString("dd/MM/yyyy HH:mm") I get what I expect 09/07/2011 08.00

Upvotes: 2

jhouse
jhouse

Reputation: 2692

You should get what you're after from getNextFireTime (the value from that method should be accurate after having called ScheduleJob()). Also the ScheduleJob() method should return the date of the first fire time.

Upvotes: 0

Related Questions