I am using hangfire in Asp.Net core, and I am experiencing something I think should not be happening.
On IISExpress start, app will create 2 servers. I am not sure why. Whe UseHangfireServer does not use already existing server created in previous line?
Then, despite registering OnShutdown method, by stopping application from Visual Studio, 2 servers will remain in DB, and as soon as I start application 2 new servers will be created.
Is there any way I can do some cleaning background job or use already existing servers?
Code is down:
public void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(config => config.UseStorage(new PostgreSqlStorage(Configuration["ConnectionStrings:DefaultConnection"])));
and in :
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var applicationLifetime = app.ApplicationServices.GetRequiredService<IApplicationLifetime>();
applicationLifetime.ApplicationStopped.Register(OnShutdown);
app.UseHangfireDashboard();
var options = new BackgroundJobServerOptions{ ServerName = "TEST" };
server = new BackgroundJobServer(options);
app.UseHangfireServer(options);
....
private void OnShutdown()
{
if (server != null) {
server.Dispose();
}
}
Hi, @oodboo!
new BackgroundJobServer(options) and app.UseHangfireServer(options) create new server. Remove server = new BackgroundJobServer(options) line and only one server will be created after the application start. OnShutdown callback because UseHangfireServer does it. You should leave the following lines:public void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(config => config.UseStorage(...));
}
public void Configure(IApplicationBuilder app)
{
app.UseHangfireServer();
app.UseHangfireDashboard();
}
BackgroundJobServerOptions.ServerTimeout interval by internal ServerWatchdog process.
Most helpful comment
Hi, @oodboo!
new BackgroundJobServer(options)andapp.UseHangfireServer(options)create new server. Removeserver = new BackgroundJobServer(options)line and only one server will be created after the application start.OnShutdowncallback becauseUseHangfireServerdoes it. You should leave the following lines:BackgroundJobServerOptions.ServerTimeoutinterval by internalServerWatchdogprocess.