Forwarding Jobs From One Server To Another

Hopefully this is a quick one - my setup is multiple web servers running in AWS, talking to an RDS SQL Server database, and the servers are running Hangfire. I then have a Windows service running internally that is also running a Hangfire server, and pulling down and executing jobs from the RDS database. My default queue jobs can be executed by any server, but the ones that go to a specific queue right now can only be executed internally, so my Windows service looks at those queues as well as the default queue. What I’d like to do is setup a second server instance that will run in that service that would then use MSMQ, which means that I’ll need to forward or transfer jobs from one server to another, so I’m wondering what the best way to do that would be. Theoretically I supposed I’d want to dequeue directly from the SQL Server storage into the MSMQ storage, but I’m not sure if there’s a hook for accomplishing that goal. My initial experimentation was centered around a custom JobActivator that does the forwarding, but I wasn’t sure how to signal that I was finished with a particular job, or return a job that would get executed as a no-op effectively after I sent the incoming job to the other server instance.


For anyone interested, my solution was to create a custom JobActivator that performs the forwarding from one server to another, where the activator takes as a dependency an IBackgroundJobClient instance configured to use MSMQ storage, which it then uses to create a new job, passing in an EnqueuedState the desired queue name so it winds up in the right MSMQ queue. The trick was then to make the original job into a no-op so that Hangfire could run it successfully without side effects and mark it complete. That was done by setting the values JobActivatorContext.BackgroundJob.Job.Type and Method to a class / method that did nothing, effectively. This has been running in production for several weeks now without issue.