Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable Hangfire #18

Closed

Conversation

stevetemple
Copy link

@stevetemple stevetemple commented Mar 28, 2023

Just thinking about an option to disable on certain environments, for example local dev, or front end servers in a load balanced environment

@nul800sebastiaan
Copy link
Owner

Nice one @stevetemple !! I'm on holiday for a few weeks but will have a look when I'm back.

@nul800sebastiaan
Copy link
Owner

Just did a quick test for my Job Scheduler and it looks like this:

public class JobsComposer : IComposer
{
    private static string GetTiming(string timing, bool disabled)
    {
        return disabled ? Cron.Never() : timing;
    }
    
    public class HangfireSettings
    {   
        public bool? Disabled { get; set; }
    }

    public void Compose(IUmbracoBuilder builder)
    {
        var settings = builder.Config.GetSection("Hangfire").Get<HangfireSettings>();
        var hangfireDisabled = settings.Disabled.GetValueOrDefault(false);

        RecurringJob.AddOrUpdate<ITestService>($"This is a test job", x =>
            x.RunTestJob(null), GetTiming(Cron.Daily(), hangfireDisabled));
    }
}

The most important part is the GetTiming(Cron.Daily(), hangfireDisabled) call when scheduling the job - if Hangfire is disabled in the appSettings.json which looks like so:

image

If Hangfire is disabled, jobs are still scheduled but will never run, but can be ran manually.

To not schedule any jobs, you could write it differently like this and then GetTiming is not needed.

public void Compose(IUmbracoBuilder builder)
{
    var settings = builder.Config.GetSection("Hangfire").Get<HangfireSettings>();
    var hangfireDisabled = settings.Disabled.GetValueOrDefault(false);

    if (!hangfireDisabled)
    {
        RecurringJob.AddOrUpdate<ITestService>($"This is a test job", x =>
            x.RunTestJob(null), Cron.Daily());
    }
}

@stevetemple
Copy link
Author

I thought you were on holiday 🤨

I'd just tweaked it, realised that it wouldn't let jobs be added, just want the server side of it turned off but all servers could schedule jobs just won't run them.

Meant to open this as a draft as I was just pitching the idea

@stevetemple
Copy link
Author

Running this on a production website, where the front end servers are disabled, but backoffice server enabled, works well

I would probably change the setting name to Hangfire:Server:Disabled as it's the server part that is disabled rather than anything else. The front end servers can still add tasks etc but they'll only be run on the backoffice server

@nul800sebastiaan
Copy link
Owner

nul800sebastiaan commented Sep 18, 2023

Sorry for the delay @stevetemple
I couldn't contrib back to your branch, but I took your suggestion and made it into:

  "Hangfire": {
    "Server": {
      "Disabled": true
    }
  }

image

Additionally, you were hiding the dashboard on instances that were not a server, which turned into an infinite spinner, I think it's fine to show the dashboard even if the current machine is not a server.

Merged in 622d88f and will be released in v3.1.0 in a few minutes.

@stevetemple
Copy link
Author

Cool, thanks

Yeah we disable backoffice on front-end so that might just be our way of working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants