2

I want to create a recurring job with Hangfire but I want it to be delayed and start at a certain date.
For example I will create a job that do a task every week but I want this task to start after 3 days!

After searching I couldn't come to something that can do both delayed task and make it recurring at the same time.

I just started using hangfire today so I don't have much experience using it yet.

Muhammad Nour
  • 2,109
  • 2
  • 17
  • 24

1 Answers1

4

Hangfire offers the possibility to create recurring jobs in this way:

RecurringJob.AddOrUpdate(
    () => myRecurringJob(),
    Cron.Daily);

However as you mentioned this does not allow to postpone the date where the first occurrence will start. To work around this, I suggest to use a scheduled job to create your recurring job at a later time:

BackgroundJob.Schedule(() => myRecurringJobCreation(), 
                       new DateTimeOffset(new DateTime(2017,2,10)));

//...
public void myRecurringJobCreation() {
    RecurringJob.AddOrUpdate(
        () => myRecurringJob(),
        Cron.Daily);
}
GôTô
  • 7,974
  • 3
  • 32
  • 43
  • That was the first thing that came across my mind, but I thought there may be something else using the Api that can directly address this issue! – Muhammad Nour Feb 06 '17 at 10:19
  • @MuhammadNour I think the only other option is to create the recurring job right away and prevent execution directly in `myRecurringJob` but this is quite ugly. I don't know of a nicer way to achieve this in the API – GôTô Feb 06 '17 at 10:29