【问题标题】:Disable HangFire server in specific environment (localhost)在特定环境(本地主机)中禁用 HangFire 服务器
【发布时间】:2021-12-22 02:30:35
【问题描述】:

我们有一个预定的 HangFire 作业,每 4 小时运行一次。但是hangfire 在每个预定的时间间隔都会让控制台充满心跳。当我们在本地运行应用程序以开发新功能(或)调试现有代码时,这会很烦人。 通过增加HearbeatInterval 配置,我能够减少心跳日志。

本地应用程序和托管开发环境应用程序具有相同的数据库。托管的开发 API 已运行重复作业。因此,除非有必要(除非我想调试/测试计划的作业),否则我不想在我们的本地机器上运行它们(使用 API 开发)。

由于这对于开发人员来说是一个很常见的场景,我想了解 HangFire 是否提供了一种标准的方法来实现这一点?

注意:我已经通过 hangfire 标签和 HangFire 文档查看了所有 stackoverflow 问题,看看这对于没有自定义代码的 OOB 解决方案是否可行。

【问题讨论】:

  • 所以...不要将 Hangfire 注入到服务集合中?
  • 我希望 HangFire 成为我代码的一部分,并在 QA、Prod、staging 等其他环境中运行。但不是在本地开发机器上。如果需要在本地,我应该能够通过配置进行控制(如果存在)
  • 这正是我的意思——不要注入它。例如,使用if (environment.IsDevelopment()) 很容易做到
  • 但我们确实有一个开发环境,其中托管 API 用于 UI 本地开发。该环境也被视为ASPNETCORE_ENVIRONMENT => Development 的开发。
  • 嗯...定义一个新环境很容易,不要把我的话看得太明确,发挥你的想象力。只需为附加的调试器添加一个 if 并将环境设置为 Local 并检查 environment.IsEnvironment("local")

标签: c# asp.net-core hangfire


【解决方案1】:

docs 表示您可以直接在appsettings.json 文件中配置日志记录级别。

您可以像这样减少所有 Hangfire 日志的消息数量:

{
  "Logging": {
    "LogLevel": {
      ...
      "Hangfire": "Warning" //Only log warnings from Hangfire
    }
  }
}

但您可能只想排除特定的消息子集。我不确定心跳消息到底是什么命名空间,但你可以这样做:

{
  "Logging": {
    "LogLevel": {
      ...
      "Hangfire": "Information", //Output information and above for all Hangfire logs
      "Hangfire.Server.ServerWatchdog": "Warning" // but only warnings for the 
                                                  // Hangfire.Server.ServerWatchdog namespace
    }
  }
}

【讨论】:

  • 感谢您提供有关日志记录的信息,但我的问题更多是关于如何不在本地运行作业,但它们仍在我的代码中。
【解决方案2】:

HangFire 库中没有任何 OOB 选项。 正如Camilo Terevinto 所提到的,我能够完成以下工作。

public void ConfigureServices(IServiceCollection services)
{
  // other service configuration goes here

  if (IsHangfireJobsAllowed())
  {
    // HangFire configuration
    var storage = HangFireJobs.GetHangFireStorageConnection(
        config["CosmosDBEndpoint"],
        config["CosmosDBAuthKey"],
        config["CosmosDBDatabaseName"]);
    
    // Add HangFire services.
    services.AddHangfire(configuration => configuration
        .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
        .UseSimpleAssemblyNameTypeSerializer()
        //.UseRecommendedSerializerSettings()
        .UseSerializerSettings(new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })
        .UseStorage(storage));

    // Add the processing server as IHostedService
    services.AddHangfireServer();

    GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0 });
  }
  
  // other service configuration goes here
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IConfiguration config)
{
    // Other code goes here

    if (IsHangfireJobsAllowed())
    {
        app.UseHangfireDashboard("/hangfire");
        // Custom code to schedule the recurring jobs 
        HangFireJobScheduler.ScheduleRecurringJobs();
    }
    
    // Other code follows here
}

private bool IsHangfireJobsAllowed()
{
    // For QA and Prod env's add HangFire with out config dependency
    if (!Environment.IsDevelopment())
        return true;
    // For Dev environment adding HangFire conditionally 
    // Ignoring return value since, the result will have 'true' 
    // only if the parsing is success and config has 'true'
    bool.TryParse(Configuration["HangFire:RunJobsOnDev"], out bool result);
    return result;
}

Camilo TerevintoDavidG,谢谢你们俩。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多