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

352 views Asked by At

To send push notification daily with the interval of 24 hour, i've use quartz library. But the issue is when my pc is turned off or my network is disconnected, this peace of code is also triggered but my push notification is not send. My question is how can i manage trigger when my pc is turned off or network disconnected? I want to send push notification after my pc is turned on or connected to the internet.

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

 scheduler.ScheduleJob(job, trigger);
1

There are 1 answers

0
Rabban On BEST ANSWER

You need a combination of Misfire Instructions, a JobExecutionException and the DisallowConcurrentExecutionAttribute

  1. Set the DisallowConcurrentExecutionAttribute on your Job, this will prevent you from executing multiple instances of the same jobs at the same time.

    [DisallowConcurrentExecution]
    public class MyJob : IJob
    {
    ....
    }
    
  2. Set the Misfire Instruction to your Trigger, this will tell your trigger to fire immediately in case it missed an execution.

    ITrigger trigger = TriggerBuilder.Create()
            .WithDailyTimeIntervalSchedule
              (s =>
                 s.WithIntervalInHours(24)
                .OnEveryDay()
                .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(11, 00))
                .WithMisfireHandlingInstructionFireAndProceed()
              )
            .Build();
    
  3. Catch any exception during the execution of your job e.g. when the internet is not reachable and throw a JobExecutionException telling Quartz that the trigger should be fired again.

    public class MyJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                // connect to internet or whatever.....
            }
            catch(Exception)
            {
                throw new JobExecutionException(true);
            }
        }
    }
    
  4. Next time, read the documentation first ;)