Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hangfire recurring job daily on specific time

I am trying to run hangfire recurring job daily on 9.00 AM. Here is what I want to do-

RecurringJob.AddOrUpdate(() => MyMethod(), "* 9 * * *");

Where should I put this line of code?

Sorry if this is a foolish question.

like image 986
s.k.paul Avatar asked Apr 03 '18 10:04

s.k.paul


1 Answers

Assuming you are using .Net Core, where you can find the file startup.cs. In that you can find a Configure() method. Inside the method you can use that piece of line right after app.UseHangfireDashboard() and app.UseHangfireServer() which is for configuring hangfire dashboard and this is optional. Don't forget to Register Hangfire Services in ConfigureServices() method which can be found in the startup.cs itself.

You can Register Hangfire Services inside ConfigureServices() in Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {    
    /*
    other services
    */

    services.AddHangfire(x => x.UseSqlServerStorage("YOUR_HangfireServerConnectionString"));

    /*
    services.AddMvc()
    */
    }

You can Set Hangfire Cron inside Configure() in Startup.cs

public void Configure(IApplicationBuilder app)
{
    app.UseHangfireDashboard();  
    app.UseHangfireServer();
    RecurringJob.AddOrUpdate(() => MyMethod(), "* 9 * * *");
}

for more refer the link

UPDATE

The cron expression * 9 * * * denotes that the job will fires every minutes after 9 (24 hour format) of system time UTC time.

For creating a recurring job at 9.00 AM Daily, the expression should be 0 9 * * * refer here cron expressions

like image 147
Farshan Avatar answered Sep 24 '22 12:09

Farshan