HangFire - Delayed / Recurring Jobs

Michael Strong
2 min readJan 23, 2018

Recently I did a blog on Quartz.NET and I was not happy with the features. Quartz does not have an easy way to see what's going on. Its lightweight and in a pinch can get the job done. If you are looking for something on a larger scale, move to HangFire. In a business environment, I found the need to see what's scheduled for when. Hangfire has a dashboard that allows for a lot of visibility. You can see what's pending, what's scheduled, what's running and so much more.

At the top of the dashboard you can see Jobs, Retries, and Recurring Jobs. If you open up Jobs, the different types are broken down further into the following:

- Enqueued
- Scheduled
- Processing
- Succeeded
- Failed
- Deleted
- Awaiting

Two NuGet packages are used within this demo, being Hangfire itself and Microsoft's Owin. Owin is used to start the service. Once Owin is installed you will see a Startup.cs file in your solution. To initialize Hangfire I used the following syntax, which included my DBs connection string.

When the app first loads, Hangfire will create many tables within your DB. To load a method as a job you can use the following code.

Basically there are 3 types of jobs you can create:

Fire and Forget — for large tasks that run in the background, they do not hold up your users
Recurring — used for when an event needs to fire over and over
Delayed — one time job for a certain time (ex, 3 hours from now or at 12 midnight)

The 3rd button is code used to cancel the recurring job.

And that’s it really. From the example above you can see that the method that actually does the work is called Send. In this example I have created the Send method within the class called EmailSender.

The ClientScript is just a pop up window that displays to the user what was clicked. A confirmation message.

I have created a video that shows this Demo in action.

--

--

Michael Strong

Living in south west Ontario Canada. Father of two. Working full time as a software engineer at a major transportation software company.