I have been working recentelly with Hangfire I have created RecurringJobs,because a ReccuringJob only runs with a cron expression like:
/// <summary>
/// Schedule a job at a given time frequently
/// </summary>
/// <typeparam name="T"> Type of the function< </typeparam>
/// <param name="jobId"> the job Id </param>
/// <param name="methodToTrigger">he method that will be triggered at a given time</param>
/// <param name="cronExpression">Expression that will define the frequency of trigger for the job</param>
/// <returns></returns>
public Task SchedulReccurentJob<T>
(string jobId, Expression<Func<T, Task>> methodToTrigger, string cronExpression)
{
RecurringJob.AddOrUpdate<T>(jobId, methodToTrigger, cronExpression, TimeZoneInfo.Local);
return Task.CompletedTask;
}
I get the cron expressions via these expressions (they work just fine):
/// <summary>
/// Creates an expression for launching job every n minutes
/// </summary>
/// <param name="minutes"></param>
/// <returns></returns>
public static string EveryMinutes(int minutes)
{
return $"0 */{minutes} * ? * *"; // cant set minutes > 59
}
/// <summary>
/// Creates an expression for launching job every n hours
/// </summary>
/// <param name="hours"></param>
/// <returns></returns>
public static string EveryHours(int hours)
{
return $"0 0 0/{hours} 1/1 * ? *"; // cant set hours > 23
}
/// <summary>
/// Creates an expression for launching job every days at given hour and minute
/// </summary>
/// <param name="days"></param>
/// <param name="hour"></param>
/// <param name="minute"></param>
/// <returns></returns>
public static string EveryDaysAtHourAndMinute(int days, int hour, int minute)
{
return $"0 {minute} {hour} */{days} * ?"; // cant set days > 31
}
My problem is that I want to run a specific job every given minutes , or every given hours, or maybe every given days but with the same hour/minute.
I took a look on the cron expression it's apprentelly not possible:
Do u have any ideas ? Thanks for your help.