Hello everyone,
I need to find out if there is a solution with Hangfire to schedule a job every N days and every N hours and N minutes.
The client (frontend) has three input boxes (days, hours and minutes) and a datepicker (datetime). Someone among my colleagues suggested as a solution to schedule several jobs and add the values of the three input boxes to the start date via TimeStamp. and then start the second scheduling job.
The technologies I am using are .Net Core 6 Web Api, Hangfire 1.8.12 and Sqlite.
Can you help me understand what is the possible solution?
Thank you
There seems to be an API for a reoccurring job manager
Likely config the job manager for DI then send it off
var cronExpression = Cron.MinuteInterval((int)interval.TotalMinutes);
_recurringJobManager.AddOrUpdate("my-recurring-job", () => ExecuteJob(), cronExpression, TimeZoneInfo.Local, input.StartDate);
Thank you for your help.
But most probably I have explained myself wrongly.
I have to add a CronExpression that includes N days, hours and minutes.
This means that I have to run the scheduler every N days and hours and minutes, and I do not mean the specific day of the month or the specific day of the week.
CronExpression does not support every N days but only a specific day of the week or a specific day of the month.
I hope I have explained myself better.
Thank you.
Have you looked at the background methods documentation
https://docs.hangfire.io/en/latest/background-methods/performing-recurrent-tasks.html
https://docs.hangfire.io/en/latest/background-methods/calling-methods-with-delay.html
It seems like one of these should fit your specifics.
something like
var jobScheduler = new JobScheduler(days: N, hours: X, minutes: Y);
jobScheduler.StartInitialJob();
public void YourJobMethod()
{
// Your job logic
Console.WriteLine("Job executed at: " + DateTime.Now);
// Your specific delay
TimeSpan delay = new TimeSpan(days: _days, hours: _hours, minutes: _minutes, seconds: 0);
// Schedule the job to run again in N days, X hours, and Y minutes
BackgroundJob.Schedule(() => YourJobMethod(), delay);
}
// Initially schedule the job
public void StartInitialJob()
{
YourJobMethod();
}
1 Like