Hangfire: Reusing existing server

Created on 18 May 2017  路  1Comment  路  Source: HangfireIO/Hangfire

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();
            }
        }
question

Most helpful comment

Hi, @oodboo!

  1. Two servers are created because both 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.
  2. You don't need to register 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();
}
  1. New Hangfire server is created after each start of an app. If an app restarts previous server lives in servers list for a while. But previous server does nothing and is removed in BackgroundJobServerOptions.ServerTimeout interval by internal ServerWatchdog process.

>All comments

Hi, @oodboo!

  1. Two servers are created because both 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.
  2. You don't need to register 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();
}
  1. New Hangfire server is created after each start of an app. If an app restarts previous server lives in servers list for a while. But previous server does nothing and is removed in BackgroundJobServerOptions.ServerTimeout interval by internal ServerWatchdog process.
Was this page helpful?
0 / 5 - 0 ratings