Search code examples
c#asp.net-corehangfire

Disable HangFire server in specific environment (localhost)


We have a scheduled HangFire job that runs every 4 hours. But hangfire floods the console with heart beat at every scheduled interval. That would be annoying when we run the application in local for developing new features (or) debugging existing code. The heartbeat logs I was able to reduce by increasing the HearbeatInterval configuration.

The local application and hosted development environment application have same DB. The hosted development API already runs the recurring jobs. Hence I do not want to run them on our local machines(with API development) until unless it is necessary (Until unless I want to debug/test the scheduled jobs).

Since this is a quite common scenario for developers, I want to understand if HangFire provides a standard a way to achieve this?

Note: I have been though all the stackoverflow questions with hangfire tag and HangFire documentation to see if this is feasible with OOB solution with out custom code.


Solution

  • There wasn't any OOB option from HangFire library. As Camilo Terevinto mentioned, I was able to achieve this following work around.

    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 Terevinto and DavidG, thank you both of you.