How to get started every day at the same hour in Quartz.net?

I have to do the work every day at midnight Pacific Time. I am using MVC3 with the Quartz.NET library.

Here is my code:

public static void ConfigureQuartzJobs() { ISchedulerFactory schedFact = new StdSchedulerFactory(); IScheduler sched = schedFact.GetScheduler(); DateTime dateInDestinationTimeZone = System.TimeZoneInfo .ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, System.TimeZoneInfo.Utc.Id, "Pacific Standard Time").Date; IJobDetail job = JobBuilder.Create<TimeJob>() .WithIdentity("job1", "group1") .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartAt(dateInDestinationTimeZone) .WithSimpleSchedule(x => x.WithIntervalInHours(24).RepeatForever()) .Build(); sched.ScheduleJob(job, trigger); sched.Start(); } 

This code runs this work only once at midnight (Pacific time). I installed .WithSimpleSchedule(x => x.WithIntervalInHours(24).RepeatForever()) , but it does not work - the work does not repeat every day.

What can I do to make it work every day?

Any help is much appreciated!

+6
source share
2 answers

Are your scheduled tasks hosted in a web application? If so, you may experience such problems. Web applications are not suitable for running scheduled tasks. You better create a Windows service that hosts your scheduled tasks.

But there are some things you can check:

  • Try using a shorter period of time (for example, check if this works if you set the interval to 1 minute).
  • Try CronTrigger - I use it in a windows service and it works great.

There are several articles that explain what are the pros and cons of placing scheduled tasks in a web application, i.e. this is: http://www.foliotek.com/devblog/running-a-scheduled-task/ .

+4
source

This answer was asked 7 years ago, and the answer has already been accepted. But I think that over the 7 years there have been small changes, so I would suggest this solution through CronScheduleBuilder .

  //Constructing job trigger and build it. ITrigger trigger = TriggerBuilder.Create() .WithIdentity("Test") .StartAt(startingDate) .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(16,40)).WithSimpleSchedule(x=>x.WithIntervalInMinutes(number).WithRepeatCount(number) .Build(); 

This code starts work every day at a specific time, in this case 16:40. Interval times and repeat counts times

0
source

Source: https://habr.com/ru/post/914499/


All Articles