Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule task to run everyday at specific time with Quartz.NET

I am using Quartz.NET for doing a task everyday at specific hour and this is my code:

public class TestSchedule : ISchedule
    {
        public void Run()
        {

            DateTimeOffset startTime = DateBuilder.FutureDate(2, IntervalUnit.Second);

            IJobDetail job = JobBuilder.Create<HelloJob>()
                                       .WithIdentity("job1")
                                       .Build();

            ITrigger trigger = TriggerBuilder.Create()
                                             .WithIdentity("trigger1")
                                             .StartAt(startTime)
                                             .WithDailyTimeIntervalSchedule(x => x.OnEveryDay().StartingDailyAt(new TimeOfDay(7, 0)).WithRepeatCount(0))
                                             .Build();

            ISchedulerFactory sf = new StdSchedulerFactory();
            IScheduler sc = sf.GetScheduler();
            sc.ScheduleJob(job, trigger);

            sc.Start();
        }
    }

my code is working, but problem is that works only once(it seems that , it's because WithRepeatCount(0) ) now, how can say that run everyday at 7 o'clock?
PS : I don't want use CronTrigger to do that.

like image 894
Sirwan Afifi Avatar asked Mar 24 '23 10:03

Sirwan Afifi


1 Answers

DailyTimeIntervalTriggerImpl only support repeatCount.

This trigger also supports "repeatCount" feature to end the trigger fire time after a certain number of count is reached. Just as the SimpleTrigger, setting repeatCount=0 means trigger will fire once only! Setting any positive count then the trigger will repeat count + 1 times. Unlike SimpleTrigger, the default value of repeatCount of this trigger is set to REPEAT_INDEFINITELY instead of 0 though.

Cron expressions are beautiful and there's loads of tools which can help you to achieve what you're looking for.

Another alternative would be to use a SimpleTriggerImpl and set the interval every 24 hours:

ITrigger trigger = TriggerBuilder.Create()
        .WithIdentity("trigger1")
        .StartAt(startTime)
        .WithSimpleSchedule(x => x.RepeatForever().WithIntervalInHours(24))
        .Build();
like image 138
LeftyX Avatar answered Apr 27 '23 22:04

LeftyX