Cron 表达式无法正常工作
Cron Expression not working correctly
我有 .net windows 服务来安排 Quartz 作业,我有以下代码每天凌晨 4 点开始我的工作,每 12 小时重复一次。下面的开始时间将因每个工作而异。这里的问题是我说的不是凌晨4点开始,而是每天只有运行中午12点和中午12点。我怎样才能确保它在我要求 StartAtTime 的时间准确开始?如果我将 cron 表达式中的值减少到 1/2,并将开始时间更改为下午 2 点或下午 3 点左右,它会按预期工作。
var startAtTime = DateTime.Today.AddHours(localTime[Key]);
if (startAtTime.ToUniversalTime() < DateTime.UtcNow)
{
startAtTime = startAtTime.AddDays(1);
}
ITrigger objESLJobTrigger = TriggerBuilder.Create()
.WithIdentity("ESLTrigger-", AuditType.ESL.ToString())
.StartAt(new DateTimeOffset(startAtTime))
.WithCronSchedule("0 0 0/12 ? * SUN-SAT", x => x.WithMisfireHandlingInstructionIgnoreMisfires())
.Build();
一切正常。参见
.StartAt()
方法定义何时启动触发器,而不是作业。
- 而您的 cron 表达式
0 0 0/12 ? * SUN-SAT
恰好表示“运行 每天中午 12 点和中午 12 点工作”(您可以使用 cronmaker 检查)。
Cron Scheduler 对您的情况没有帮助,因为它以您期望的 different way 间隔工作:
/ - used to specify increments. For example, “0/15” in the seconds field means “the seconds 0, 15, 30, and 45”. And “5/15” in the seconds field means “the seconds 5, 20, 35, and 50”. You can also specify ‘/’ after the ‘’ character - in this case ‘’ is equivalent to having ‘0’ before the ‘/’. ‘1/3’ in the day-of-month field means “fire every 3 days starting on the first day of the month”.
所以像 0 0 4/12 ? * SUN-SAT
这样的表达式将 运行 每 12 小时在凌晨 4 点和下午 4 点。 0 0 5/12 ? * SUN-SAT
将在早上 5 点和下午 5 点每 12 小时 运行。而 StartAt() 方法仅定义何时触发第一个作业(凌晨 4 点或下午 4 点),它不会对 cron 表达式进行偏移。
只需改用 SimpleSchedule:
ITrigger objESLJobTrigger = TriggerBuilder.Create()
.WithIdentity("ESLTrigger-", AuditType.ESL.ToString())
.StartAt(new DateTimeOffset(startAtTime))
.WithSimpleSchedule(x => x
.WithIntervalInHours(12)
.RepeatForever())
.Build();
我有 .net windows 服务来安排 Quartz 作业,我有以下代码每天凌晨 4 点开始我的工作,每 12 小时重复一次。下面的开始时间将因每个工作而异。这里的问题是我说的不是凌晨4点开始,而是每天只有运行中午12点和中午12点。我怎样才能确保它在我要求 StartAtTime 的时间准确开始?如果我将 cron 表达式中的值减少到 1/2,并将开始时间更改为下午 2 点或下午 3 点左右,它会按预期工作。
var startAtTime = DateTime.Today.AddHours(localTime[Key]);
if (startAtTime.ToUniversalTime() < DateTime.UtcNow)
{
startAtTime = startAtTime.AddDays(1);
}
ITrigger objESLJobTrigger = TriggerBuilder.Create()
.WithIdentity("ESLTrigger-", AuditType.ESL.ToString())
.StartAt(new DateTimeOffset(startAtTime))
.WithCronSchedule("0 0 0/12 ? * SUN-SAT", x => x.WithMisfireHandlingInstructionIgnoreMisfires())
.Build();
一切正常。参见
.StartAt()
方法定义何时启动触发器,而不是作业。- 而您的 cron 表达式
0 0 0/12 ? * SUN-SAT
恰好表示“运行 每天中午 12 点和中午 12 点工作”(您可以使用 cronmaker 检查)。
Cron Scheduler 对您的情况没有帮助,因为它以您期望的 different way 间隔工作:
/ - used to specify increments. For example, “0/15” in the seconds field means “the seconds 0, 15, 30, and 45”. And “5/15” in the seconds field means “the seconds 5, 20, 35, and 50”. You can also specify ‘/’ after the ‘’ character - in this case ‘’ is equivalent to having ‘0’ before the ‘/’. ‘1/3’ in the day-of-month field means “fire every 3 days starting on the first day of the month”.
所以像 0 0 4/12 ? * SUN-SAT
这样的表达式将 运行 每 12 小时在凌晨 4 点和下午 4 点。 0 0 5/12 ? * SUN-SAT
将在早上 5 点和下午 5 点每 12 小时 运行。而 StartAt() 方法仅定义何时触发第一个作业(凌晨 4 点或下午 4 点),它不会对 cron 表达式进行偏移。
只需改用 SimpleSchedule:
ITrigger objESLJobTrigger = TriggerBuilder.Create()
.WithIdentity("ESLTrigger-", AuditType.ESL.ToString())
.StartAt(new DateTimeOffset(startAtTime))
.WithSimpleSchedule(x => x
.WithIntervalInHours(12)
.RepeatForever())
.Build();