我正在显示当前在我的页面上运行的 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/