Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a job every day at the same hour in Quartz.net?

I have to execute job every day at midnight Pacific Time. I am using MVC3 with 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 makes this job run only once at first midnight(in Pacific Time). I have set there .WithSimpleSchedule(x => x.WithIntervalInHours(24).RepeatForever()) but it is not working - job is not repeating every day.

What can I do to make it work every day?

like image 995
Marta Avatar asked Apr 30 '12 08:04

Marta


2 Answers

Are your scheduled tasks hosted by web application? If so, you may experience such problems. Web applications are not suitable for running scheduled tasks. You should rather create windows service that hosts scheduled tasks.

But there are also some things you may check:

  1. Try using shorter period of time (i.e. check if this works if you set interval to 1 minute).
  2. Try CronTrigger - i'm using it in windows service and it works fine.

There are some articles that explain what are pros and cons of hosting scheduled tasks in web application, ie. this one: http://www.foliotek.com/devblog/running-a-scheduled-task/.

like image 163
empi Avatar answered Oct 15 '22 22:10

empi


This question has been asked 7 years ago and there is already accepted reply. But i think throughout 7 years there has been a little bit changes so i would suggest this solution via CronScheduleBuilder.

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

This code trigger job every day at particular time in this case 16:40. With interval number times and repeat count with number times

like image 23
So_oP Avatar answered Oct 15 '22 23:10

So_oP