我正在显示当前在我的页面上运行的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服务器会自动删除旧的服务器数据吗?
我将不胜感激任何帮助:)
答案 0 :(得分:7)
我挖掘了源代码,找到了:
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);
答案 1 :(得分: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);
}