I will use Hangfire in loadbalanced environment with single database. I need a job to be run on each of the server instances.
I tried to create a job with QueueAttribute
, to which I would assign Environment.MachineName
value.
The issue is that I need to pass constant values in attributes, which Environment.MachineName
isn't.
How to configure Hangfire to run a job on all server instances independetly?
For every server that hangfire creates it uses machine name as default name of the server itself. But you can manipulate the name of queue when creating the server , for instance :
services.AddHangfireServer(a =>
{
a.WorkerCount = 10,
a.Queues = new[] { "default", Environment.MachineName };
});
This way every server would have unique queue for your needs. After that you can enqueue your job like this :
IBackgroundJobClient client = new BackgroundJobClient();
IState state = new EnqueuedState
{
Queue = Environment.MachineName
};
client.Create(() => Console.WriteLine(), state);
NOTE: I haven't tested this - this is basic idea.