Checking Hangfire Health

Hi

I was wondering if it is possible to check that Hangfire is receiving jobs, like a health check. The reason being that always on mode is not configured and shouldn’t need to be (more then 1 job every five minutes).

At the moment i am using something like this, but would it return a null string if the job has not been enqueued?

string testActive = BackgroundJob.Enqueue(() => TestSend());
if(!String.IsNullOrEmpty(testActive))
{}

Thanks

Even some verification that the code in the above post will not return a valid string if the Hangfire service is down would be nice.

Anybody?

That code will not work. You have to remember that there are two sides to Hangfire:

  1. The client which is responsible for queuing jobs (In this case, just setting the state and persisting them in the job storage engine)
  2. The server which is responsible for executing the jobs (There may be multiple servers located on different physical machines)

Your code will only tell you if the job has been queued:

What you probably want to do is to check that at-least one server has sent a heartbeat within the last x period and is therefore seen as active and processing jobs, or even that at least one server is seen as active and processing the queue of interest:

IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi();
IList<ServerDto> servers = monitoringApi .Servers();

bool atLeastOneServerIsActive = servers.Any();

const queueName = "Default";
bool queueIsBeingProcessed = servers.Any(
    s => s.Queues.Contains(queueName, StringComparer.InvariantCultureIgnoreCase)
);

It is worth noting that this is still not a full health-check, for this you should periodically add a job to the queue (Maybe use a recurring job) and either get this job to update a value in the database that you can check to ensure it’s changed, or check the details about this recurring job to see when it last ran and if it was successful. (See the dashboard’s recurring job page for the kind of information available.) The IMonitoringApi interface should be helpful for getting information related to this as this is what the dashboard uses. https://github.com/HangfireIO/Hangfire/blob/3049522fc02a291420f56c7c41c1d735dc186a13/src/Hangfire.Core/Storage/IMonitoringApi.cs

Thank you for the reply.

I have indeed used what you suggested as a health check and it works well for my needs. Thank you for sending the code reference, that has given me confirmation to what i needed to know, which is that the job either has been queued or not.