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

2

18

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 :)

Goldeneye answered 27/4, 2018 at 15:7 Comment(2)
You could create a script which deletes data from the db. Look at this answer stackoverflow.com/a/39977006 and modify the drop to delete queries. You can then run that script from the startup in your console appIcebreaker
@MarcusHöglund I dont want to delete job data.I just want to delete server data.Actually i am testing long running job using console application so what happens is everytime i run console app it creates new entry instead of delete existing one.Moreover i cant blindly fire delete or truncate on hangfire server table because if i will have more than 1 server running the job then it will create a problem.Hence i am looking a way like hangfire server api will help me delete perticular server if shutdownGoldeneye
I
17

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);
Iota answered 2/5, 2018 at 18:11 Comment(0)
C
1

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);
        }
Carilla answered 30/12, 2018 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.