Search code examples
asp.netwebformsdelayed-jobhangfirerecurring

Create Schedule(Delayed) Recurring job using hangfire in asp.net forms


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.


Solution

  • 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);
    }