Introduction of Hangfire:

Are you tired of struggling with the limitations of the Windows Task Scheduler when it comes to managing and scheduling background jobs? Look no further – Hangfire is here to revolutionize your job management experience.

In this blog post, we will explore Hangfire, a powerful open-source framework designed to handle background jobs and tasks seamlessly with .NET 6 and MSSQLServer. Say goodbye to the frustrations of handling failed jobs, especially for high-volume operations like sending millions of SMS or emails. Hangfire provides a reliable alternative that ensures smooth execution and takes the burden off your shoulders.

Discover how Hangfire enables asynchronous task execution, freeing up your application’s resources and improving performance. Its fault tolerance mechanism automatically retries failed jobs, ensuring critical operations never slip through the cracks. Plus, with the comprehensive monitoring and management dashboard, you gain real-time insights into job execution and historical data.

Join us on this journey as we delve into the features and benefits of Hangfire, explore its integration possibilities, and witness the remarkable impact it can have on your background job management. It’s time to supercharge your efficiency with Hangfire. Let’s get started!

What Is Hangfire?

In simple words, hangfire is a task scheduler used to schedule and manage jobs. Unlike Windows scheduler, it runs as an independent application and doesn’t need a separate service. Hangfire is a tool that allows us to work easily with background jobs and fire them in the background.

Hangfire is built on a client-server architecture running on the .NET stack, where the Hangfire client creates jobs and the Hangfire server executes them. It is available for both the .NET Framework & .NET Core. Hangfire uses external storage in a database, like SQL Server.

Using this external storage, Hangfire stores the jobs submitted to it and maintains the status of those tasks. Hangfire is available both as an open-source free version as well as a paid version.

Types of Hangfire tasks

There are different tasks that users can execute in Hangfire and the following are the types.

  1. Fire-and-forget
  2. Delayed
  3. Recurring
  4. Continuations
  5. Batches
  6. Batch Continuations

Task Type 1: Fire-and-forget

Fire-and-forget jobs are executed only once and almost immediately after the user creates them. So for instance, if the user wants to fire off a background task to send an email or execute some process, the user can just fire and forget a background task. The syntax to do that:

Task Type 2: Delayed

Unlike fire and forget, delayed tasks are not executed the moment the action is taken but rather, run at a specific time in the background. An example of a delayed task would be when a welcome email or promo code is sent to a user 8 hours after they sign up. The syntax to do that:

Task Type 3: Recurring

Recurring jobs are kind of like CRON. They execute on a schedule. In fact, they use exactly the same syntax as CRON. Recurring jobs fire many times on the specified CRON schedule. The frequency can be every five minutes, every hour, every day, every week, every Wednesday, or any other frequency the user wants. The syntax to do that:

Task Type 4: Continuations

Continuations are executed after a given job has been finished. With continuations, we can have a job, and then after that job is finished, the user can execute another job. This way, the user can have some sort of chain of jobs that run one after another. Here is the syntax:

Task Type 5: Batches

Batches are created atomically and consist of several background jobs. And Batches only work in the paid version. Here is the syntax:

Task Type 6: Batch Continuations

Following the completion of all background jobs in the parent batch, batch continuation is fired. Batch Continuations only work in the paid version. Here is the syntax:

Why Hangfire?

Hangfire allows the user to submit jobs from anywhere dynamically, and Hangfire is not pre-configured like the Windows task scheduler. So, once the user deploys the Hangfire application, the user can submit any number of jobs from anywhere dynamically.

Hangfire also processes the submitted jobs in the background asynchronously on multiple threads to execute more jobs in less time.

Hangfire runs as an independent application, so a user can deploy this on any operating system like Windows, Linux, or Mac.

Hangfire can be used with Dot NET Core. Dot Net Core also supports multiple operating systems, so users can deploy this on multiple operating systems.

Hangfire doesn’t require a separate scheduler to run and Hangfire itself is a task scheduler so it doesn’t require another scheduler to run. Hangfire is an open-source framework that helps users create, process, and manage background jobs free of cost. It supports scalable architecture and multi-threading. This means that if the server is not capable of executing all jobs, the user can add more servers to the hangfire application to execute the jobs.

Hangfire supports external storage including SQLServer, Redis, Oracle, MySql, Firebird, PostgreSQL, and many others. Most importantly, Hangfire provides a Dashboard to view all jobs and their execution status including exceptions. With the help of this dashboard, the user can instantly see what is currently happening in Hangfire.

How does Hangfire work?

Workflow of Hangfire:

Hangfire’s basic architecture can be summed up like this:

The Hangfire client is the one that creates the job in the architecture. In addition, there is a job storage area, where jobs are stored. The idea is that the application has some kind of storage, such as a database, repository, or Redis. That’s where the jobs should be stored. Eventually, the Hangfire server can retrieve them.

Since the client is the one who creates the job, it returns to the client immediately after creating a job, so the client does not have to wait for it to be completed. The Hangfire server executes those jobs. Thus, the Hangfire server needs to connect to the database, which in turn, returns pending jobs for the server to execute.

MsSQLServer is used for job storage and DotNet Core id is used for Hangfire client and server.

How To Setup and Configure Hangfire in the existing project?

1. Install the following Nuget Packages:

  • Hangfire
  • Hangfire.Dashboard.Authorization

2. Add the following configurations in the program.cs or startup.cs files

Hangfire Server configuration:

Hangfire Dashboard configuration:

EndPoint Configuration:

Program.cs or Startup.cs file is the place where the job configuration should be done.

Example:

Recurring Job:

IScheduleJobsRepository – the Interface name that contains the implement methods.

HangfireScheduleJobsAsync() – The method name that needs to be called.

Add the following configuration variables in the Appsettings.json file:

Database Configuration:

CRON Expression:

Tables in the MSSQLServer Database:

The tables related to Hangfire are created automatically while running the DotNet Core application.

Hangfire Dashboard

The dashboard of Hangfire is available at the http://hostAddress/hangfire endpoint. And it looks like this:

Benefits of Hangfire

Straightforward

Setting up and using the system is easy. The application doesn’t require a Windows service, a Windows scheduler, or a separate installation.

Persistent

Background jobs can be created using Persistent Storage on SQL Server, Redis, and other community-developed storage options. The user can reboot the application and use Hangfire with ASP.NET without worrying about application pool recycling.

Open Source

Hangfire is free to use for both commercial and non-commercial purposes.

Self-maintainable

It is not necessary to laboriously maintain storage space – Hangfire takes care of that aspect and expunges outdated entries automatically.

Efficient

By default, SQL Server and polling is used to obtain jobs, however, one can use MSMQ or Redis extensions to decrease the delay of processing to the lowest.

Extensible

The user can enhance the background processing in a similar way to ASP.NET MVC action filters by using job filters. All the job storage support is abstracted and the user can implement it for their favorite storage provider. They can also modify the dashboard.

Distributed

Background method calls and their arguments are serialized and may cross process boundaries. Rather than setting up Hangfire on multiple machines to get more processing power, the user can simply use it without configuration – syncing is performed automatically.

Reliable

After creating a background job without any issues, Hangfire takes on the obligation to complete the process with the least amount of chances for an exception. Should there be an unhandled issue or an application termination, the job is attempted again without any extra effort from the user.

Transparent

The integrated web interface provides users with a comprehensive overview of their background procedures, as well as the status of each background job. Additionally, pre-integrated support for popular logging platforms enables users to detect mistakes effortlessly and without any configuration.

Business Benefit Provided by our team to the client:

  1. Throughput: Hangfire’s asynchronous processing and scalability features significantly increased the throughput of the application. The number of requests that can be processed within a given time frame greatly reduced . With Hangfire, the number of requests handled per job increased 100% thus improving their overall performance of the system.
  2. Error Rate: Hangfire’s fault tolerance and retries feature helped the client to reduce the error rate in the application. This metric measures the percentage of failed or erroneous requests or tasks. By setting up the jobs to automatically retry on failing, Hangfire improved the success rate of background operations, minimizing errors and enhancing the overall reliability of your application. Specific scenarios such as restarting the failed job from the point of failure avoided key Customer satisfaction issues such as repeated emails or notifications being shared to the same people multiple times, delay in notifications as the system previously did not have any restart capability where key additions to the application thus bring the error rate to almost zero.
  3. System Downtime: Prior to integrating Hangfire, due to the inability to process concurrently and since the processing time was very high (a job could run upto more than half a day, was reduced to  a minute for upto 5 million records) thus causing heavy load on the servers and ultimately result in bringing the servers down. Hangfire’s fault tolerance and scalability features contributed to reducing system downtime to almost zero.

Conclusion

To summarize, Hangfire offers a robust and flexible solution for background job processing in .NET applications. By offloading time-consuming and resource-intensive tasks to background jobs, Hangfire enables businesses to enhance application performance, improve scalability, and streamline their processes. With features like asynchronous processing, fault tolerance, and scheduled jobs, Hangfire provides the necessary tools to optimize business operations.  It empowers businesses to leverage the full potential of their .NET applications by providing a reliable, scalable, and efficient background job processing framework. By leveraging the capabilities, our client optimized their operations, enhanced performance, and ensured a seamless user experience.

Posted in Technologies