Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hangfire recurring job on every server

Tags:

c#

hangfire

I have a situation where I need a recurring job registered with hangfire to run on every server in the cluster.

(The job is to copy some files locally so needs to run on every server regularly)

So far I have tried registering the same job with an id of the server name resulting in n job for n servers:

RecurringJob.AddOrUpdate(Environment.MachineName, () => CopyFiles(Environment.MachineName), Cron.MinuteInterval(_delay));

and the job itself checks if it is the correct server and only does something if it is:

 public static void CopyFiles(string taskId)
 {
        if (string.IsNullOrWhiteSpace(taskId) || !taskId.Equals(Environment.MachineName))
        {
            return;
        }

        // do stuff here if it matches our taskname
}

The problem with this is that all jobs executes on the first server to come along, is marked as complete and as a result is not executed by the other servers.

Is there any way to ensure that the job runs on all servers?

or is there a way to ensure that only one server can process a given job? i.e. target the job at the server that created it

like image 732
MikeW Avatar asked Feb 13 '17 10:02

MikeW


People also ask

How do I stop a recurring job on Hangfire?

You can remove an existing recurring job by calling the RemoveIfExists method. It does not throw an exception when there is no such recurring job.

Is Hangfire Russian?

Starting from Mar 8, 2022 Hangfire is owned by Hangfire OÜ (private limited), an Estonian company.

How does Hangfire Server work?

Server. Hangfire Server processes background jobs by querying the Storage. Roughly speaking, it's a set of background threads that listen to the Storage for new background jobs, and perform them by de-serializing type, method and arguments.

What is a recurring job?

A recurring job is a batch job that is scheduled to run on a regular basis.


1 Answers

Found an answer using this link.

Simply assign the job to a queue that is specific to the server you want it processing on.

So I changed my enqueue to:

RecurringJob.AddOrUpdate(Environment.MachineName, 
  () => CopyFiles(Environment.MachineName),
  Cron.MinuteInterval(_delay), 
  queue: Environment.MachineName.ToLower(CultureInfo.CurrentCulture));

And when I start my server I do this:

_backgroundJobServer = new BackgroundJobServer(new BackgroundJobServerOptions 
                           {
                               Queues = new[] { Environment.MachineName.ToLower() } 
                           });
like image 172
MikeW Avatar answered Nov 14 '22 21:11

MikeW