如何强制 hangfire 服务器在重启时删除该特定服务器的旧服务器数据?
How to force hangfire server to remove old server data for that particular server on restart?
我正在我的页面上显示当前 运行ning 的 hangfire 服务器列表。
我在控制台应用程序中 运行ning hangfire 服务器,但问题是当我没有我的控制台应用程序时 运行ning 仍然 hangfire api returns hangfire服务器。
此外,当我 运行 我的控制台应用程序多次时,我得到 3-4 个 hangfire 服务器,尽管我在控制台应用程序中只有 1 个 hangfire 服务器 运行ning。
Mvc 应用程序:
IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi();
var servers = monitoringApi.Servers().OrderByDescending(s => s.StartedAt);
控制台Application: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)
如果你想自己看代码,这里有相关的源代码文件:
通过最后的link,也很清楚,您可以自定义您的服务器名称,以便于查找和删除:
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);
按照代码删除同一服务器中的重复项。
//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);
}
我正在我的页面上显示当前 运行ning 的 hangfire 服务器列表。
我在控制台应用程序中 运行ning hangfire 服务器,但问题是当我没有我的控制台应用程序时 运行ning 仍然 hangfire api returns hangfire服务器。
此外,当我 运行 我的控制台应用程序多次时,我得到 3-4 个 hangfire 服务器,尽管我在控制台应用程序中只有 1 个 hangfire 服务器 运行ning。
Mvc 应用程序:
IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi();
var servers = monitoringApi.Servers().OrderByDescending(s => s.StartedAt);
控制台Application: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)
如果你想自己看代码,这里有相关的源代码文件:
通过最后的link,也很清楚,您可以自定义您的服务器名称,以便于查找和删除:
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);
按照代码删除同一服务器中的重复项。
//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);
}