I am using Hangfire to upload data of excel file(small-large) to database table(Insert\update) as background process in Asp.Net MVC web application.
We have team of 6-7 developers and our project (.Net) is configured in local IIS (on each developers’s machine) for development phase. So we do not use built in .Net web server in development and we run project from local IIS.
Howerver, Our database is at one server (Dev DB server) and we all connect to that database during development.
The hangfire database is on that Dev DB server and we all connect to that server for hangfire related activities.As we all devs run our own instance of IIS application,we all will have our own separate instance of hangfire service.
Now all the hangfire services on different IIS points the same database for Jobs. In this case when my service is running in can pick up any scheduled task from hangfire database and try to execute that,which can occur in error if same resources are being used.
As One solution ( which we dont want to follow if any other is there), In development environment may be we can have own hangfire database on our own local DB instance instead of Dev DB server but when this will go to production where we have load balancers,I am not sure how it will behave and what is the best solution for this.
Seems like using local DBs is probably the best answer, but if you want to use the shared DB and you always want the machine enqueuing the job to be the one to execute it, you could use the machine name as the queue name.
You can control the queue name at runtime by using the alternative enqueue method - it looks something like this (grabbed from this thread Dynamic queues?):
var state = new EnqueuedState(Environment.MachineName);
BackgroundJobClient.Create(() => Console.WriteLine("Nobody else execute me!"), state);
Note: you’d need to make sure that the server setup method also listens on the MachineName queue.
I referenced below code and made changes to my implementation :
IBackgroundJobClient client = new BackgroundJobClient();
IState state = new EnqueuedState
Queue = “my-queue”
client.Create(() => Console.WriteLine(), state);
Very first time, then jobs were not getting to processing state but was stuck on Enqueued state. So I searched about it and found this facility is available in 1.3.3 version ( I was using 1.3.1) so I updated my Hangfire. But still my jobs gets stuck in Enqueued state.
So you’re seeing it in the Dashboard? If you hit the “Servers” page, do you see a server handling “my-queue” or just “default”? If you just see default, you’ll need to modify the “UseHangfire” line of your server setup to be something like this: