I notice that when I create my scheduled task for the first time at application startup, it executes immediately even though it was created with a cron expression many hours in the future. Is this normal behavior? Is there a way to prevent that? If i would use the RunAfter with the same datetime of the next cron execution, will it fire twice? Or I could find the next cron execution and then prevent it from running if it is now yet the time?
You would need to set the Scheduled Task’s LastRun in the db, something like:
IBackgroundJobs jobs = feature.Jobs; // or IOC
using var dbJobs = jobs.OpenDb();
dbJobs.UpdateOnly(() => new ScheduledTask {
LastRun = DateTime.UtcNow,
}, where:x => x.Name == taskName);
Hi @mythz , thanks, your trick does it!
Now, there is another little thing…I have some issues WHEN the scheduled tasks runs…
It runs at my expected local time only If I convert the Cron expression to UTC …
Can you explain how the library works with Time Zones, Daylight saving etc…
Right now, in my EST tz (UTC-5), I was expecting 0 7,15,23 * * * to work but I had to use 0 4,12,20 * * *
which is very weird. Now, when we go in DST (UTC-4)… what is going to happen?
Everything is stored and parsed as UTC, so you have to keep using UTC.