Question

I have an array of hours when a single job needs to be run on the current day, something like this:

["00:05", "01:42", "04:21", "17:57"]

As you can see are arbitrary hours so I cannot use a Cron schedule. I've been searching on how to add multiple hours to a trigger, or how to use multiple triggers on the same job and I haven't found any ways to achieve this.

So, how can I run the same job at the times specified by the array?

Was it helpful?

Solution

Having multiple triggers for the job is the key.

var job = JobBuilder.Create<TheJobType>()
    .StoreDurably(true)
    .WithIdentity("the-job-all-are-going-to-execute")
    .Build();

scheduler.AddJob(job, false);

var trigger1 = TriggerBuilder.Create()
                                    .ForJob(job)
                                    .WithIdentity("trigger1")
                                    .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(0, 5))
                                    .Build();
scheduler.ScheduleJob(trigger1);


var trigger2 = TriggerBuilder.Create()
                                    .ForJob(job)
                                    .WithIdentity("trigger2")
                                    .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(1, 42))
                                    .Build();
scheduler.ScheduleJob(trigger2);

.... etc ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top