Search code examples
c#asp.net-corehangfire

HangFire Recurring job at 3 given times


I have set a recurring job to run at this time but i would like to repeat it at 2 more specific times. How can i do it?

app.UseHangfireDashboard();

RecurringJob.AddOrUpdate<TerminaTurnos>(t => t.Termina(),  Cron.Daily(22, 10), TimeZoneInfo.Local);

Solution

  • You can just create 3 reccuring jobs (executing the same job) to execute on different times giving the jobId parameter for each of them:

    RecurringJob.AddOrUpdate<TerminaTurnos>("job1", t => t.Termina(),  Cron.Daily(18, 10), TimeZoneInfo.Local);
    RecurringJob.AddOrUpdate<TerminaTurnos>("job2", t => t.Termina(),  Cron.Daily(20, 10), TimeZoneInfo.Local);
    RecurringJob.AddOrUpdate<TerminaTurnos>("job3", t => t.Termina(),  Cron.Daily(22, 10), TimeZoneInfo.Local);
    

    EDIT:

    But you can also make one reccuring job with cron expression like:

    RecurringJob.AddOrUpdate<TerminaTurnos>(t => t.Termina(), "10 18,20,22 * * *", TimeZoneInfo.Local);
    

    It will execute the job at minute 10 past hour 18, 20, and 22.