Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 743
ILoveStackoverflow Avatar asked Apr 27 '18 15:04

ILoveStackoverflow


People also ask

Is HangFire scalable?

The second option for scaling Hangfire is to simply run more Hangfire Servers. Now when running two instances of my hangfire Server, the dashboard shows each server with 30 worker threads. This means our application can process 60 jobs concurrently. Hangfire fully manages dispatching jobs to the appropriate server.

How does HangFire Server work?

Server. Hangfire Server processes background jobs by querying the Storage. Roughly speaking, it's a set of background threads that listen to the Storage for new background jobs, and perform them by de-serializing type, method and arguments.


2 Answers

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:

  • Mapping of db server.Id

  • Background server announcement

  • Delete server from db with id

  • Code to generate server id

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);
like image 94
caesay Avatar answered Oct 12 '22 01:10

caesay


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);
        }
like image 1
Lucas Lima Avatar answered Oct 12 '22 01:10

Lucas Lima