Home > Net >  Run Quartz job at specified time
Run Quartz job at specified time

Time:07-22

I have a job in Quartz Scheduler which deletes rows from database and I want it to run everyday at 00:00. Currently it has intervalInMinutes set to 1440 (24hrs) but it's wrong. How to set this to run everyday at 00:00?

Code snippet:

x.AddTrigger(x => x,
.ForJob(delete)
.StartNow()
.WithSimpleSchedule(s => s
    .WithIntervalInMinutes(1440)
    .RepeatForever()
);

CodePudding user response:

There is a simple configuration for a specific recurring job trigger which is like below :

ITrigger t = TriggerBuilder.Create()
        .WithIdentity("myTrigger")
        .ForJob("myJob")
        .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(0,0)) // execute job daily at 00:00
        .Build();

CodePudding user response:

I currently do not have access to an IDE so I can't check it out, but I have found this on the Quartz documentation, maybe this helps you in the right direction.

trigger = TriggerBuilder.Create()
.WithIdentity("trigger8") // because group is not specified, "trigger8" will be in the default group
.StartAt(DateBuilder.EvenHourDate(null)) // get the next even-hour (minutes and seconds zero ("00:00"))
.WithSimpleSchedule(x => x
    .WithIntervalInHours(2)
    .RepeatForever())
// note that in this example, 'forJob(..)' is not called 
//  - which is valid if the trigger is passed to the scheduler along with the job  
.Build();

Notice how they specify StartAt, maybe you can use that builder to start at 00:00 exactly.

Then with the WithSimpleSchedule set an interval for every 24 hours and repeat forever.

Good luck!

https://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/simpletriggers.html

  • Related