How to force hangfire server to remove old server data for that particular server on restart?

7.6k views Asked by At

I am showing list of hangfire servers currently running on my page.

I am running hangfire server in console application but the problem is when I don't have my console application running still hangfire api returns hangfire servers.

Moreover when I run my console application multiple times I get 3-4 hangfire servers though I have only 1 hangfire server running in console application.

Mvc application :

IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi();
var servers = monitoringApi.Servers().OrderByDescending(s => s.StartedAt);

Console Application:Hangfire server

public static void Main(string[] args)
{
    var sqlServerPolling = new SqlServerStorageOptions
    {
        QueuePollInterval = TimeSpan.FromSeconds(20) // Default value
    };
    GlobalConfiguration.Configuration.UseSqlServerStorage("ConnectionString", sqlServerPolling);

    // Set automatic retry attempt
    GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0 });

    // Set worker count
    var options = new BackgroundJobServerOptions
    {
        WorkerCount = 1,
    };
    using (var server = new BackgroundJobServer(options))
    {
        Console.WriteLine("Hangfire Server1 started. Press any key to exit...");
        Console.ReadKey();
    }
}

Hangfire server doenst automatically remove old server data whenever I run my console application again for that particular server?

I will appreciate any help :)

2

There are 2 answers

0
caesay On BEST ANSWER

I dug through the source code to find:

IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi();
var serverToRemove = monitoringApi.Servers().First(); //<-- adjust query as needed
JobStorage.Current.GetConnection().RemoveServer(serverToRemove.Name)

If you want to see the code yourself, here are the related source code files:

Via the last link, it's also clear that you can customize your server name to make it easier to find and remove:

var options = new BackgroundJobServerOptions
{
    WorkerCount = 1,
    ServerName = "removeMe",
};
// ....
IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi();
var serverToRemove = monitoringApi.Servers().First(svr => srv.Name.Contains("removeMe"));
JobStorage.Current.GetConnection().RemoveServer(serverToRemove.Name);
0
Lucas Lima On

Follow the code to remove duplicate in the same server.

        //Start Hangfire Server
        var varJobOptions = new BackgroundJobServerOptions();
        varJobOptions.ServerName = "job.fiscal.io";
        varJobOptions.WorkerCount = Environment.ProcessorCount * 10;
        app.UseHangfireServer(varJobOptions);
        app.UseHangfireDashboard("/jobs", new DashboardOptions {
            Authorization = new[] { new clsHangFireAuthFilter() }
        });
        //Remove Duplicte HangFire Server
        var varMonitoringApi = JobStorage.Current.GetMonitoringApi();
        var varServerList = varMonitoringApi.Servers().Where(r => r.Name.Contains("job.fiscal.io"));
        foreach( var varServerItem in varServerList) {
            JobStorage.Current.GetConnection().RemoveServer(varServerItem.Name);
        }