【发布时间】:2018-04-27 15:07:35
【问题描述】:
我正在显示当前在我的页面上运行的 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 服务器不会自动删除旧的服务器数据?
我将不胜感激任何帮助:)
【问题讨论】:
-
您可以创建一个从数据库中删除数据的脚本。查看这个答案stackoverflow.com/a/39977006 并修改drop 以删除查询。然后,您可以在控制台应用程序中从启动运行该脚本
-
@MarcusHöglund 我不想删除作业数据。我只想删除服务器数据。实际上我正在使用控制台应用程序测试长时间运行的作业,所以每次运行控制台应用程序时都会发生这种情况,它会创建新条目删除现有的。此外,我不能盲目地删除或截断hangfire服务器表,因为如果我将有超过1台服务器运行该作业,那么它将产生问题。因此我正在寻找一种像hangfire服务器api这样的方式将帮助我删除关闭时的特定服务器
标签: c# hangfire hangfire-console