5

I am building a .net core web application, in the server side I am adding hangfire for scheduled tasks and for long run tasks. in the Startup.cs file I added:

services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection")));

and in the configure function I added this:

app.UseHangfireServer();
app.UseHangfireDashboard();

Now the problem is whenever I run the application, a new instance of the server shows up in the dashboard. enter image description here

is there a way to make sure that only one server is running? or if I can turn off the server when I stop the application (IIS) and start it again when I run the application

Mhand7
  • 527
  • 1
  • 3
  • 21

3 Answers3

3

This seems to address some of the issues. It should clear up any servers that haven't been active in the last 15 seconds. I currently have this is my application startup.cs. Doesn't feel like the correct solution, but limited by what they offer.

        // Clean up Servers List
        Hangfire.Storage.IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi();
        JobStorage.Current.GetConnection().RemoveTimedOutServers(new TimeSpan(0, 0, 15));

        app.UseHangfireServer(new BackgroundJobServerOptions
        {
            WorkerCount = 1,
            Queues = new[] { "jobqueue" },
            ServerName = "HangfireJobServer",
        });

        RecurringJob.AddOrUpdate("ProcessJob", () => YourMethod(), AppSettings.JobCron, TimeZoneInfo.Local, "jobqueue");
Netferret
  • 604
  • 4
  • 15
  • Running multiple server instances like you suggest here are obsolete after Hangfire 1.5. Server identifiers are now generated using GUIDs, so all the instance names are unique. https://docs.hangfire.io/en/latest/background-processing/running-multiple-server-instances.html – Cvassend Jan 04 '23 at 08:40
1

According to current version of Hangfire ASP.NET Core Applications the call to app.UseHangfireServer() inside public void Configure() is not neccessary (at least the documentation does not include it)

instead, in public void ConfigureServices(IServiceCollection services) use

services.AddHangfire
services.AddHangfireServer

This should eliminate the second server instance

0

You should create a separate queue for each server. Please look this page: Single Dashboard, Single Physical Server, Multiple Jobs

MRP
  • 499
  • 5
  • 24