Pub/sub events with Hangfire

Hello, is it possible for an observer to listen for finished Hangfire jobs? Something like this:

Process A (Web Service): Enqueue a job to execute in 5 minutes
Process B (Windows Service): Be notified of this job’s completion and take some action

Can anyone help? Thank you

You can use continuations:

var jobId = BackgroundJob.Schedule( ... , 5 mins); BackgroundJob.ContinueWith( jobId, continuation );

If you are looking for a generic solution, you can implement IElectStateFilter and within OnStateElection method, you can handle Succeeded or Failed state to take whatever action you need to perform.

Would I be able to use my IElectStateFilter from a completely separate .NET process hosted on another machine?

Yes, I implemented it in a windows service that host the Hangfire Server and it runs on a different machine than the web application that en-queues the job

1 Like

Thank you so much for the suggestion; it sounds very promising. I will give it a shot!

This works great, thank you. I have another question, if you don’t mind.

Do you know of a way to broadcast a job’s state change to multiple servers?

Client App: Enqueue the job
Server1 App: Use filter to become notified
Server2 App: Use filter to become notified

So in essence, both server apps are notified by the same job’s completion?