Pergunta

I need to use network-validated time when scheduling jobs.

Solution?

Quartz .NET Details - where I got stuck in my investigation

I cannot find how the property StartTimeUtc of ITrigger is used (set with the StartAt method below)?

var sleepTrigger = (ISimpleTrigger)TriggerBuilder.Create()
            .WithIdentity("SleepTimeTrigger")
            .StartAt(sleepRunTime)
            .WithSimpleSchedule(x => x.WithIntervalInHours(24).RepeatForever())
            .Build();

i.e. I need to check the specific implementation that uses the StartTimeUtc timestamp in order to change the scheduling source code / add an option to schedule independently of local system time and use network time instead.

Foi útil?

Solução

There is a static class under Quartz namespace which has two methods receiving a Func<DateTimeOffset> that you can use to return your NTP tunned date/time. It is the "official" Quartz date/time source, and is implemented to allow easy unit testing, but could also be used to customize the date/time. Here is a a sample usage:

SystemTime.Now = () => {
    //return your custom datetime here
    /*
    var ntpTime = new NtpTime(server);
    return ntpTime.NowDateTimeOffset;
    */
};

SystemTime.UtcNow  = () => {
    //return your custom datetime here
    /*
    var ntpTime = new NtpTime(server);
    return ntpTime.UtcNowDateTimeOffset;
    */
};

Notice the code above will not compile, you need to implement your own method of getting the current DateTimeOffset for both Now and UtcNow. There are many ways to get the time from a NTP, you can find some approaches here and here. I suggest that your implementation caches the current datetime and increment it locally instead of asking the ntp server on every call, for performance reasons.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top