Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a recurring job in Hangfire every 8 days

Tags:

c#

cron

hangfire

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.

like image 206
Yogi Avatar asked Feb 03 '16 04:02

Yogi


People also ask

Where does Hangfire store recurring jobs?

Persistent. Background jobs are created in a persistent storage – SQL Server and Redis supported officially, and a lot of other community-driven storages. You can safely restart your application and use Hangfire with ASP.NET without worrying about application pool recycles.

What Hangfire object and method is used to create a recurring job?

The call to AddOrUpdate method will create a new recurring job or update existing job with the same identifier.

Is Hangfire multithreaded?

Hangfire is a multi-threaded and scalable task scheduler built on client-server architecture on . NET stack. In this article, we are going to setup Hangfire and write some code to schedule an initial job in the ASP.NET Core project. Hangfire is an open source library to schedule and execute background jobs in .

Is Hangfire Russian?

Starting from Mar 8, 2022 Hangfire is owned by Hangfire OÜ (private limited), an Estonian company.


2 Answers

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

like image 180
Yogi Avatar answered Sep 22 '22 20:09

Yogi


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

For your case it will be

RecurringJob.AddOrUpdate("MyJob",() => ScheduledJob(), Cron.DayInterval(8));
like image 42
Athul Dilip Avatar answered Sep 21 '22 20:09

Athul Dilip