Reputation: 1185
I've a .net windows service that creates and schedules quartz jobs which is scheduled to run every 4 hours SUN-SAT starting at 4 AM the day the windows service is started. Unfortunately, the moment I start my windows service jobs are getting triggered much ahead of the schedule and is repeating the jobs for more than couple of times and later it is working only as per the schedule. How can I prevent the jobs run immediately after starting my windows service and only run at scheduled time. I'm using crons scheduler. I'm using Oracle DB to maintain my job schedules.
ITrigger objESLJobTrigger = TriggerBuilder.Create()
.WithIdentity("Trigger-" + Key.ToString(), xxx.ToString())
.StartAt(DateTimeOffset.Parse(DateTime.Today.AddHours(4).ToString()).LocalDateTime)
.WithCronSchedule("0 0 0/4 ? * SUN-SAT", x => x.WithMisfireHandlingInstructionIgnoreMisfires())
.Build();
Upvotes: 1
Views: 4019
Reputation: 49779
Updated after comment:
yes, you need to use StartAt
method, but:
you need to use today date in StartAt
if service starts before 4 AM, otherwise use tomorrow date:
var startAtDateTime = DateTime.Today.AddHours(4);
if (startAtDateTime < DateTime.UtcNow)
{
startAtDateTime = startAtDateTime.AddDays(1);
}
and then use .StartAt(new DateTimeOffset(startAtDateTime))
pay attention that StartAt
expects time in UTC format, not in local, so if you need to run job at at 4AM in local timezone, don't forget about timezone offset.
Also as 'SUN-SAT' means every day, you can simplify your cron expression to 0 0 0/4 ? * *
Upvotes: 3