Hangfire Schedule to run Recurring jobs with In-Memory Storage.

Abayomi Omosehin
3 min readOct 23, 2023
scheduled Job interface in Hangfire

This article will be more helpful to those who are interested in managing long-running tasks such as sending emails, periodical activities and many other heavy tasks that do not require user interventions. The BackgroundService base class for long-running jobs is a good way to achieve this with not so much complication. There are many approaches one can use to achieve this such as Quartz but my preference for Hangfire rests on its ease of configuration and the UI dashboard.

Hangfire has many methods such as Enqueue, AddOrUpdate, Schedule and others. Each of these has its use cases based on application design. My focus here is what schedule method task can achieve with UseMemoryStorage (lightweight and in-memory storage of background job state information). Let start with a simple configuration of hangfire

Add the following package to your .net core application

dotnet add package Hangfire.Core
dotnet add package Hangfire.AspNetCore
dotnet add package Hangfire.InMemory

After the above installations in done, the code below is all that you require to get along.

This shows how easy the configuration can be. When the app is started it uses an in-memory storage on the server to save scheduled jobs. You can also use any database of your choice to store the data or Redis for enterprise use. But then my example here will work for many instances where a recurring job is needed.

To use the schedule method (Delayed jobs are executed only once too, but not immediately, after a certain time interval). Smart developers can make use of this scheduling method to run recurring tasks. To schedule a task use

This is all that is required to schedule a task to be carried out at a later time conveniently, with no hassle. In yourmethod(payload) you can schedule anything including httpClient, database long-running method, email sending and other possible tasks. The moment a task is posted it gets reflected on the dashboard with many juicy features To access your dashboard ===> https://baseurl:port/hangfire. This can be restricted by authentication.

To make this a recurring task you need to set up a table in your database (not the in-memory storage but any database of your choice) to track your jobs when successful or not. When a job is successful you can reschedule the job for another delay call, if fails you can perform a retry or delete the failed job and schedule another one. Repeatedly one can achieve a simple background job with a schedule method. Since the jobs are not persisted in the storage, little space is being used which can serve some good purposes. This can be managed based on your use cases.

In general, I got to know that any package that has a schedule method can perform a recurring background job seamlessly based on good planning.
I believe you have learnt one or two things here. Please kindly share with your circle of influence.

To learn more about hangfire, it is powerful and simple. check out https://www.hangfire.io/

--

--