c# - 如何强制 hangfire 服务器在重启时删除该特定服务器的旧服务器数据?

标签 c# hangfire hangfire-console

我正在显示当前在我的页面上运行的 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 服务器是否会自动删除旧的服务器数据?

我将不胜感激:)

最佳答案

我翻遍了源代码发现:

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

关于c# - 如何强制 hangfire 服务器在重启时删除该特定服务器的旧服务器数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50065324/

相关文章:

c# - 如何在 C# 中获取公共(public)桌面和开始菜单目录的路径?

c# - Hangfire 1.5.3 System.MissingMethodException 在我们所有的工作上

c# - 并发运行两次时如何取消循环作业?

asp.net-web-api2 - Hangfire 作业成功或失败后,如何执行新作业?

asp.net-mvc - Hangfire (v1.8.4) 图标未显示

c# - 以编程方式设置打印机以绕过 Windows 后台处理程序

c# - Ghostscript.NET pdf 到图像在 Windows Azure 上无法工作

c# - 是否可以为 bool 值切换时制作类似事件处理程序的东西?

hangfire - 从数据库脚本触发重复的 Hangfire 作业