【问题标题】:Hangfire Multiple serversHangfire 多台服务器
【发布时间】:2018-07-24 01:54:43
【问题描述】:

我正在构建一个 .net 核心 Web 应用程序,在服务器端我正在为计划任务和长期运行任务添加 hangfire。在我添加的 Startup.cs 文件中:

services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection")));

在配置函数中我添加了这个:

app.UseHangfireServer();
app.UseHangfireDashboard();

现在的问题是,每当我运行应用程序时,仪表板中都会显示一个新的服务器实例。

有没有办法确保只有一个服务器在运行?或者我可以在停止应用程序 (IIS) 时关闭服务器并在运行应用程序时重新启动它

【问题讨论】:

  • 稍等片刻后旧实例应该被移除。
  • 真的...感谢您的回答

标签: c# .net-core hangfire


【解决方案1】:

这似乎解决了一些问题。它应该清除过去 15 秒内未激活的所有服务器。我目前有这是我的应用程序startup.cs。感觉不是正确的解决方案,但受到他们提供的东西的限制。

        // Clean up Servers List
        Hangfire.Storage.IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi();
        JobStorage.Current.GetConnection().RemoveTimedOutServers(new TimeSpan(0, 0, 15));

        app.UseHangfireServer(new BackgroundJobServerOptions
        {
            WorkerCount = 1,
            Queues = new[] { "jobqueue" },
            ServerName = "HangfireJobServer",
        });

        RecurringJob.AddOrUpdate("ProcessJob", () => YourMethod(), AppSettings.JobCron, TimeZoneInfo.Local, "jobqueue");

【讨论】:

    【解决方案2】:

    根据Hangfire ASP.NET Core Applications的当前版本 在public void Configure() 内调用app.UseHangfireServer() 是不必要的(至少文档不包括它)

    改为public void ConfigureServices(IServiceCollection services) 使用

    services.AddHangfire
    services.AddHangfireServer
    

    这应该消除第二个服务器实例

    【讨论】:

      【解决方案3】:

      您应该为每台服务器创建一个单独的队列。请看这个页面:Single Dashboard, Single Physical Server, Multiple Jobs

      【讨论】:

        猜你喜欢
        • 2022-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        • 2014-04-20
        • 2022-01-16
        • 2014-10-15
        • 2016-03-12
        相关资源
        最近更新 更多