Issue
Is there a way to use base time in Quartz trigger to generate next fire times by adding the current time to distribute scheduled jobs across that particular hour instead running every scheduled job on fixed interval.
For example my code below works fine and generate the next fire time as 11:20, 11:30, 11:40 etc when I use the 10 minutes interval. But, If I have 1000 customers and all of them using 10 minute schedule interval, it will result in firing 1000 jobs at the same time.
var trigger = SimpleScheduleBuilder
.RepeatMinutelyForever(10)
.Build();
var fireTime = trigger.GetFireTimeAfter(DateTime.UtcNow);
for (int i = 0; i < 10; i++)
{
if (fireTime == null)
{
break;
}
Console.WriteLine(fireTime.Value.ToLocalTime());
fireTime = trigger.GetFireTimeAfter(fireTime);
}
Output
11/20/2019 11:20:00 +05:30
11/20/2019 11:30:00 +05:30
11/20/2019 11:40:00 +05:30
11/20/2019 11:50:00 +05:30
11/20/2019 12:00:00 +05:30
11/20/2019 12:10:00 +05:30
11/20/2019 12:20:00 +05:30
11/20/2019 12:30:00 +05:30
11/20/2019 12:40:00 +05:30
11/20/2019 12:50:00 +05:30
But If I can use the base time
- Client 1 scheduled the job at 11:05 (Current time of server) using 10 minutes interval - His jobs should fire 11:05, 11:15, 11:25, 11:35 and so on
- Client 2 scheduled the job at 11:08 (Current time of server) using 10 minutes interval - His jobs should fire 11:08, 11:18, 11:28, 11:38 and so on
Any idea, how can I use the base time in Quartz.net (3.07) ?
Solution
Try this to pass specific start time for calculation:
var trigger = TriggerBuilder.Create()
.StartAt(DateTimeOffset.UtcNow)
.WithSchedule(SimpleScheduleBuilder.RepeatMinutelyForever(10))
.Build();
Or with specific fluent interface for simple schedules:
var trigger = TriggerBuilder.Create()
.StartAt(DateTimeOffset.UtcNow)
.WithSimpleSchedule(x => x.WithIntervalInMinutes(10).RepeatForever())
.Build();
You can also consider using less triggers and handle more clients which with each one. Like having group identifier in trigger data map and process all group's data per invocation. Naturally depends on your requirements.
Answered By - Marko Lahma Answer Checked By - Mildred Charles (WPSolving Admin)