如何防止石英作业立即启动

how to prevent quartz job start immediately

我有一个 .net windows 服务可以创建和安排 quartz 作业,该作业计划 运行 从 windows 当天凌晨 4 点开始每 4 小时执行一次 SUN-SAT服务启动。不幸的是,当我开始我的 windows 服务工作时,它比计划提前了很多,并且重复了几次以上的工作,后来它只能按照计划工作。如何在启动我的 windows 服务后立即阻止作业 运行,并且仅在预定时间 运行。我正在使用 crons 调度程序。我正在使用 Oracle DB 来维护我的工作计划。

 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();

评论后更新: 是的,您需要使用 StartAt 方法,但是:

  • 如果服务在凌晨 4 点之前开始,您需要使用 StartAt 中的今天日期,否则使用明天日期:

    var startAtDateTime = DateTime.Today.AddHours(4);
    if (startAtDateTime < DateTime.UtcNow)
    {
       startAtDateTime = startAtDateTime.AddDays(1);
    }
    
    and then use .StartAt(new DateTimeOffset(startAtDateTime))
    
  • 注意 StartAt 需要 UTC 格式的时间,而不是本地时间,所以如果您需要 运行 在本地时区凌晨 4 点工作,请不要忘记关于时区偏移。

此外,'SUN-SAT' 表示每天,您可以将 cron 表达式简化为 0 0 0/4 ? * *