0

I am new to Quartz and want to use it to schedule scripts in SQLServer using a service for clients using SQL Server Express.

I need a user to be able to schedule a task to run, say every second week starting from a particular date. eg: StartDate = 1/Jan/2013 12:00PM (Tuesday) and repeat every 14 days.

In the setup for the job, there is a StartTime (datetime), IntervalUnit (second, minute, hour, day, week, year) and RepeatsEvery (the number of intervals to fire after the start date).

eg: a setup of StartDate = "1/JAN/2013 12:00PM" and IntervalUnit = "DAY" and RepeatsEvery= 14, would fire the job every 2nd Tuesday at 12:00PM. If the StartDate is already passed, I need the job to fire on the next start date, eg: If I started the service today (18/JAN/2013), the next startdate would be 29/JAN/2013 12:00PM

I can't figure out how to make this work in Quartz, using the Calendar Trigger. Is this something that is built in to Quartz or do I need to calculate a new StartTime on startup?

Assuming I have to calculate a new StartTime, is there any built in function or neat shortcut for this, or do I need a function similar to this for each IntervalUnit:

'Days
Dim diff As Integer
diff = Now.Subtract(.StartTime).TotalDays
Dim offset As Integer
offset = .RepeatsEvery - (diff Mod .RepeatsEvery)
offset = Now.AddDays(offset).Subtract(.StartTime).TotalDays 'Get the days from Start t preserve time of day
Start = .StartTime.AddDays(offset)
Molloch
  • 2,261
  • 4
  • 29
  • 48

1 Answers1

1

You can create a trigger and then call trigger.GetFireTimeAfter(DateTime.Now) to calculate the new start date and then build and new trigger with the new StartDate.

The following is C#, but hopefully gives you the idea

var trigger = TriggerBuilder.Create()
                   .StartAt( new DateTimeOffset(new DateTime(2013,1,1)))
                   .WithSimpleSchedule(x=>x.RepeatForever()
                                          .WithInterval(new TimeSpan(14,0,0,0)))
                    .Build();                


var newStartingPoint = trigger.GetFireTimeAfter(DateTime.Now);

if (newStartingPoint.HasValue)  
{ 
     trigger = ((ISimpleTrigger) trigger).GetTriggerBuilder()
                                        .StartAt(newStartingPoint.Value)
                                        .Build();    
}

.GetTriggerBuilder() isn't available on ITrigger, which is why you have to cast the trigger to ISimpleTrigger, but the same principle works with other triggers.

sgmoore
  • 15,694
  • 5
  • 43
  • 67