Controlling whether a job gets dequeued in Hangfire to a particular server....


I want to run multiple servers S1, S2, S3 with a single Q1. I want to deal with the fact that the servers may be at different versions of a common code base which contains the job classes.

So say I have a job:

public class SomeJob
   public static int VERSION = 1;

    public void ExecuteJob(int aVersion)
       //Check we are are getting a job that we can handle
       if (aVersion == VERSION)
           //Do some useful work

    public static void CreateJob(long aBrideId, DateTime aForDateUtc)
        BackgroundJob.Enqueue<SomeJob>(aX => aX.ExecuteJob(VERSION));

When I spin up S1 the VERSION is 1. A few days later I realise I need to make a change to the job so change:


and spin up S2 with the new code base. Now as both S1 & S2 are servicing the same Q1 is there any way to “tell HF” that I only want jobs that pass a particular criteria on a server by server basis for the same queue name?

Or put another way, how do I tell HF to conditionally dequeue a job to a particular server?

You cannot do it, not without effort at least.

You can probably make an ElectStateFilter which will refuse the job changing to Processing state unless it is picked by the “right” server. But it would obvioulsy introduce some processing delays (particularly when the “right” server is quite busy).

Any other solution would likely require changes in Hanfire code.