18

我正在显示当前在我的页面上运行的 hangfire 服务器列表。

我在控制台应用程序中运行 hangfire 服务器,但问题是当我没有运行控制台应用程序时,hangfire api 仍然返回 hangfire 服务器。

此外,当我多次运行我的控制台应用程序时,我得到了 3-4 个 hangfire 服务器,尽管我只有 1 个 hangfire 服务器在控制台应用程序中运行。

Mvc 应用程序:

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

控制台应用程序:Hangfire 服务器

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 服务器不会自动删除旧的服务器数据?

我将不胜感激任何帮助:)

4

2 回答 2

14

我翻遍了源代码发现:

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

如果你想自己看代码,这里是相关的源代码文件:

通过最后一个链接,很明显您可以自定义您的服务器名称,以便更轻松地查找和删除:

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);
于 2018-05-02T18:11:13.110 回答
1

按照代码删除同一服务器中的重复项。

        //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);
        }
于 2018-12-30T18:14:52.537 回答