Execute a recurring job in Hangfire every 8 days
Asked Answered
H

2

17

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- https://en.wikipedia.org/wiki/Cron

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.

Honor answered 3/2, 2016 at 4:51 Comment(0)
H
33

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

Honor answered 5/2, 2016 at 8:34 Comment(3)
I am in a similar situation. I am trying to execute a method at 23:58 everyday. The method seems to be running but it is running multiple times at 23:58. Following the code. I don't whats going on. Thanks for your insights. RecurringJob.AddOrUpdate(() => new DailyStatistics().Send(), Cron.Daily(3,58));Confab
actually this have to be 0 * */8 * * instead of * * */8 * * In your case it will run on each minute of each 8th day.Rashida
This is completely wrong! this will not run on every 8 day intervals, it will run on at every minute on every 8th day-of-month lolIngratiate
K
15

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

For your case it will be

RecurringJob.AddOrUpdate("MyJob",() => ScheduledJob(), Cron.DayInterval(8));
Kennithkennon answered 21/3, 2018 at 8:53 Comment(2)
Nice! I guess it is added in the newer versions of hangfire. +1Honor
Now obsolete, will be removed in Version 2.0.0Litigable

© 2022 - 2024 — McMap. All rights reserved.