如何在发送推送通知期间关闭计算机时处理 Quartz 触发器

How to handle Quartz trigger when Computer is turned off during sending push notification

为了每天以 24 小时为间隔发送推送通知,我使用了 quartz 库。但问题是当我的电脑关闭或我的网络断开连接时,这种和平代码也会被触发,但我的推送通知不会发送。我的问题是当我的电脑关闭或网络断开时如何管理触发器? 我想在我的电脑开机或连接到互联网后发送推送通知。

 ITrigger trigger = TriggerBuilder.Create()
                .WithDailyTimeIntervalSchedule
                  (s =>
                     s.WithIntervalInHours(24)
                    .OnEveryDay()
                    .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(11, 00))
                  )
                .Build();

 scheduler.ScheduleJob(job, trigger);

您需要Misfire Instructions, a JobExecutionException and the DisallowConcurrentExecutionAttribute

的组合
  1. 在你的作业上设置DisallowConcurrentExecutionAttribute,这将防止你的作业同时执行多次。

    [DisallowConcurrentExecution]
    public class MyJob : IJob
    {
    ....
    }
    
  2. Misfire Instruction 设置为您的触发器,这将告诉您的触发器在错过执行时立即触发。

    ITrigger trigger = TriggerBuilder.Create()
            .WithDailyTimeIntervalSchedule
              (s =>
                 s.WithIntervalInHours(24)
                .OnEveryDay()
                .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(11, 00))
                .WithMisfireHandlingInstructionFireAndProceed()
              )
            .Build();
    
  3. 捕获作业执行期间的任何异常,例如当无法访问互联网时。然后抛出一个 JobExecutionException 并告诉 Quartz 它应该再次触发触发器。

    public class MyJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                // connect to internet or whatever.....
            }
            catch(Exception)
            {
                throw new JobExecutionException(true);
            }
        }
    }
    
  4. 下次先读documentation ;)