Hangfire: Entry point to call recurring background job in ASP.NET Core

Created on 7 May 2017  路  2Comments  路  Source: HangfireIO/Hangfire

I am trying to implement a recurring background job which will clean up my database after several days.
However, I cannot find any start point in ASP.NET Core to push the background job to the queue for the first time. All I found is that I have to make a request and then create a controller to handle that request. This controller is where I create the background job. I think this is a little "manually" and I have to make a request every times I start the app.
Is there anyway to push the background job to the queue instantly when I start the app for the first time?

Most helpful comment

Startup.cs's Configure method is a perfect place to implement startup logic. Just inject IRecurringJobManager as argument and configure your jobs there:
```c#
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IRecurringJobManager recurringJobs)
{
// ...
// ...
// ...

app.UseMvc();

// configure recurring jobs
recurringJobs.AddOrUpdate("your-job-id", Job.FromExpression(x => x.DoJob(null)), Cron.Weekly());
}
```

All 2 comments

Startup.cs's Configure method is a perfect place to implement startup logic. Just inject IRecurringJobManager as argument and configure your jobs there:
```c#
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IRecurringJobManager recurringJobs)
{
// ...
// ...
// ...

app.UseMvc();

// configure recurring jobs
recurringJobs.AddOrUpdate("your-job-id", Job.FromExpression(x => x.DoJob(null)), Cron.Weekly());
}
```

But what if you want to set cron for reccuring job from config and using reloadOnChange: true? Is there any best practice how to do this?

Decide to use IOptionsMonitor in public void Configure(IApplicationBuilder app, ... - but what if exist better solution?

Was this page helpful?
0 / 5 - 0 ratings