Is it possible to have 2 web apps using the same HangFire database? I need to start a RecurringJob “A” in the “A” web application, and a RecurringJob “B” in another web application. But I have only one database. The result is that I have 2 servers listed in the dashboard, but both dashboard shows either “A” and “B” jobs. Can I set a recurring jobs to a specific server instance?
Thanks,
Joe
Joe I think you could do this if you specified Queues on your servers. Configure server A to use QueueA, and default. Then server B to use QueueB, and default then start the specific Jobs in the correct queues
RecurringJob.AddOrUpdate(() => new MyJob(), Cron.Hourly, null, “QueueA”);
http://docs.hangfire.io/en/latest/background-processing/configuring-queues.html
Where can I tell to server A to use Queue A?
I thought using the ServerName property in the Startup.cs, but it’s marked as obsolete
In your start.cs you can use something like
app.UseHangfireServer(new BackgroundJobServerOptions
{
Queues = new[] { "default", "QueueA" }
});
and on the second application you would use
app.UseHangfireServer(new BackgroundJobServerOptions
{
Queues = new[] { "default", "QueueB" }
});
this will set them up so they process those queues, then you can assign each job to a specific queue
Thanks, it works. But there’s nothing in the Recurring Jobs table that highlight the server the job will use… so I waited a bit to see if it worked. Probably a column in the table with the job’s queue will be useful.
To help other people: add a parameter in the web.config (different for each instance or server):
<appSettings>
<add key="queue" value="queue1" />
</appSettings>
then in the startup.cs add the following code:
var queue = System.Configuration.ConfigurationManager.AppSettings["queue"];
app.UseHangfireServer(new BackgroundJobServerOptions
{
Queues = new[] { "default", queue }
});
So it using the
RecurringJob.AddOrUpdate(() => new MyJob(), Cron.Hourly, null, "QueueA");
Doesn’t work? I believe that is supposed to specify which queue the job is to be run in. That way you can specify the server it will run on. Or did I miss something?
Yes, it works. But the job’s queue is not visible in the job list under the Recurring Jobs tab. So, to check if everything was fine, I must waited the jobs to run and check the application logs to verify which instance ran the job
Ah, yes of course, I am not aware of any way to break out queues in the dashboard. Thanks for clarifying, and I am glad it all worked out.