[Solved] Execute a recurring job in Hangfire every 8 days

Is it possible to create a recurring job in Hangfire that executes after a given number of days, say 8.

The nearest I found was to execute a job once in a week –

RecurringJob.AddOrUpdate("MyJob",() => ScheduledJob(), Cron.Weekly());

Understanding that Hangfire also accepts standard CronExpression, I’ve tried exploring cron expression for this frequency but couldn’t found one for it-

One ugly solution could be to create 3 or 4 jobs that executes once in month at some dates accordingly, but I don’t want to do that.

Any suggestions please.

Enquirer: Yogi


Solution #1:

Finally I have used CronExpression like this to schedule a recurring job with frequency of every 8 days or for any number of days for that matter.

string cronExp = "* * */8 * *";
RecurringJob.AddOrUpdate("MyJob",() => ScheduledJob(), cronExp);

The third segment in CronExpression represents day of month.

The respective segments are as follows – (Ref: https://en.wikipedia.org/wiki/Cron)

enter image description here

Respondent: Yogi

Solution #2:

A more cleaner Solution will be to use Cron.DayInterval(interval).

For your case it will be

RecurringJob.AddOrUpdate("MyJob",() => ScheduledJob(), Cron.DayInterval(8));
Respondent: Athul Dilip

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.