Reputation: 93
I've almost finished simple application in c# with quartz which run some SQL queries periodically.
I have a question, is it possible to create cron trigger which start job at specific date and after this date do some jobs periodically.
here is my code:
IJobDetails myJob = new JobDetails(); //This Constructor needs to be parameterless i nic na to nie poradzimy
JobDetailImpl jobDetail = new JobDetailImpl(name, gruoupName, myJob.GetType());
//dodawanie parametru
jobDetail.JobDataMap.Add("addParam", item);
//////
CronTriggerImpl trigger = new CronTriggerImpl();
trigger.Name = triggerName;
trigger.Group = grupa;
trigger.CronExpressionString = "0 10 14-15 8 8 ?";
try
{
_scheduler.ScheduleJob(jobDetail, trigger);
}
catch
{
MessageBox.Show("INVALID TRIGGER. JOB CANCELED");
}
DateTimeOffset? nextFireTime = trigger.GetNextFireTimeUtc().Value.AddHours(2);
Console.WriteLine("Job o jobs_id=" + item.jobs_id + " start:" + nextFireTime.Value);
For example: Today is 1.08.2013 and today i want to create crone trigger which start at 8.08.2013 and fires SQL query every day from 2:00pm to 3:00pm every 10minutes.
At the moment I know how to make crone trigger which start 8.08.2013 and run once, and know how to create second crone trigger which run every day from 2:00pm to 3:00pm every 10minutes but i still can not figure out how to marge this 2 crone triggers and create one: which start at 8.08.2013, run everyday after 8.08.2013 and fires SQL query every day from 2:00pm to 3:00pm for each 10minutes.
I tried use:
trigger.FinalFireTimeUtc(someDate);
trigger.SetNextFireTimeUtc(someDate)
But without any positive result. Job fires as its cronExpressionString says.
Many thanks in advance for help.
Upvotes: 0
Views: 1663
Reputation: 361
The key is when building your Trigger use the 'StartAt' extension method.
/* calculate the next time you want your job to run - in this case top of the next hour */
var hourFromNow = DateTime.UtcNow.AddHours(1);
var topOfNextHour = new DateTime(hourFromNow.Year, hourFromNow.Month, hourFromNow.Day, hourFromNow.Hour, 0, 0);
/* build your trigger and call 'StartAt' */
TriggerBuilder.Create().WithIdentity("Delayed Job").WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever()).StartAt(new DateTimeOffset(topOfNextHour))
Upvotes: 1
Reputation: 93
Thank You lenimall for answer. I check method StatTimeUTC but it returns start datetime value, not setting it. I have found blog which proffs Quartz is not capable of running jobs at the time when you desired. http://nurkiewicz.blogspot.com/2012/04/quartz-scheduler-misfire-instructions.html Anyway, many thanks for help!
Upvotes: 0
Reputation: 451
Not sure what version of Quartz.net you're using, but there is a "StartTimeUtc" property you can set with 2.0. You might want to try that one.
Upvotes: 0