Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Damien Holley 180 posts 541 karma points
    Nov 03, 2022 @ 00:39
    Damien Holley
    0

    Hangfire to run only on Master server in load balanced environment

    I've been looking for information on making sure hangfire ONLY runs on the master server of a load-balanced environment and am drawing a blank.

    I am getting logs that come from the process hangfire calls, but they are only appearing on the public servers, (some appear on master, but public has different logs come through)

    This is very worrying and I wanted to make sure Hangfire was setup so it would only execute on the master server as it is using EF to write to SQL and is updating cache.

    I am modifying it so it only processes the commands in the Compose function of HangfireJobsComposer so they are only run on the master server.

  • Conor Howe 36 posts 137 karma points
    Nov 08, 2022 @ 21:16
    Conor Howe
    0

    In a load balanced environment you can explicitly set the publisher and subscriber roles, with the publisher role being your master as described here:

    https://our.umbraco.com/Documentation/Fundamentals/Setup/server-setup/load-balancing/flexible-advanced#explicit-schedulingpublisher-server

    You can then conditionally add the hang fire setup if you are on the correct role

  • Damien Holley 180 posts 541 karma points
    Nov 09, 2022 @ 23:38
    Damien Holley
    0

    Yeah that's done, I would like to know the recommended method of forcing hangfire to only use the master server.

  • Conor Howe 36 posts 137 karma points
    Nov 10, 2022 @ 07:24
    Conor Howe
    0

    So now you would use something like this:

    https://our.umbraco.com/documentation/reference/Scheduling/#using-serverroleaccessor

    And only add the hang fire setup on the correct role

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Nov 10, 2022 @ 10:10
    Sebastiaan Janssen
    0

    A hack I've done before is if you know the machine names, and they won't change then just set the timing to never.

    private static string GetTiming(string timing)
    {
        return System.Environment.MachineName == "FLASH" 
            ? Cron.Never() 
            : timing;
    }
    

    And then call it like:

    RecurringJob.AddOrUpdate<IAwesomeService>($"😎 Do great thing", x =>
                x.DoThing("test"), GetTiming(Cron.Daily()));
    
  • Damien Holley 180 posts 541 karma points
    May 10, 2023 @ 01:08
    Damien Holley
    0

    That's a nice solution, if I run my own server I would definitely do that, but it's on Azure (and machine names change on there a lot).

  • Damien Holley 180 posts 541 karma points
    May 10, 2023 @ 01:10
    Damien Holley
    100

    My solution was ito completely rebuild how it was all working, I rejigged hangfire not to run on public at all. The public partials call the same endpoint, it checks to see if it is a master server.

    If not, it hits an endpoint on master that runs the same code and returns the results (whilst updating the cache and using a cacherefresher to tell the public instances to update their cache.

Please Sign in or register to post replies

Write your reply to:

Draft