Boosting Productivity with HangFire: Streamlining Background Job Processing
Tags: dotnet, codenewbie, coding, beginners
Published: 2023-07-05 05:58:44

**Introduction**
In today's fast-paced software development world, handling background jobs efficiently is crucial. Hangfire, is a powerful open-source library, simplifies background job processing in .NET applications. In this article, we'll explore the features and benefits of Hangfire and learn how it can revolutionize the way you handle background tasks in .NET Core Apps.

**What is Hangfire?**
Hangfire is an open-source framework for .NET that allows you to perform background processing in a reliable and scalable way. It can be used to run tasks that are not time-sensitive, such as sending emails, processing images, Updating Data, Sending Notifications, or generating reports etc. Hangfire works by decoupling your background tasks from your web requests. This means that your web requests will not be blocked by long-running tasks, and your application will remain responsive. 

**Hangfire Features**

**Job Scheduling**
Hangfire allows you to schedule jobs for execution at specific times or intervals. You can specify cron expressions, time delays, or recurring patterns to determine when the jobs should be executed.

**Persistent Storage** 
Hangfire provides support for various storage options, such as SQL Server, MySQL, PostgreSQL, Redis, and more. This ensures that job data is reliably stored and can survive application restarts or failures.

**Fault-tolerant Processing**
Hangfire ensures reliable job execution by handling exceptions and providing built-in mechanisms for retrying failed jobs. It maintains the state of jobs, allowing you to monitor their progress and handle failures gracefully.

**Dashboard and Monitoring**
Hangfire comes with a web-based dashboard that provides an overview of enqueued, processing, and completed jobs. It allows you to monitor job status, track performance, and manually trigger jobs if needed.

**Extensibility**
Hangfire offers an extensible architecture that allows you to customize its behavior and integrate with other components of your application. You can create custom job filters, implement job cancellation, and extend its functionality as per your requirements.

**Ease of use**
Hangfire is easy to use and integrate with your existing applications and its really easy to set it up and get started.

**Types of background jobs**

**Fire and Forget Jobs**
These are one-time background jobs that are enqueued and executed independently without waiting for a result or feedback. Once enqueued, Hangfire processes them as soon as the required resources are available. 
- Sending Notifications 
- Logging
- Processing Payments


**Delayed jobs**
Delayed jobs allow you to schedule a job for execution at a specific time or after a certain delay. These jobs are enqueued to be executed in the future based on the specified time or delay interval.
- Data Import/Export in Batches after certain time.
- Report Generation 

**Recurring jobs**
Recurring jobs are used to schedule jobs that need to run repeatedly at specified intervals. You can set the recurrence pattern (e.g., cron expression or time interval) to define when the job should be executed.
- Birthday Reminder
- Monthly Subscription Reminder
- Data Backup

**Continuations jobs**
Continuations allow you to define a job that should be executed after the completion of another job. When the parent job finishes, the continuation job is enqueued for execution. Simply we can say that child job is dependent on parent job.
- Image Processing (e.g. after upload extract text or crop images)
- Data Validation(e.g. validating data from 3rd party api)

**Batches jobs** (Pro Version Needed)
Batch jobs enable you to combine multiple jobs into a single logical unit. You can enqueue a batch job containing multiple sub-jobs, and they will be executed atomically as part of the batch.
- Data Retrieval for multiple endpoints or databases.
- Data Processing for multiple endpoints or databases.

**Batch Continuations jobs** (Pro Version Needed)
Batch continuation is fired when all background jobs in a parent batch finished. Instead of processing data in real-time or as events occur, batch jobs allow you to process data in chunks or batches, optimizing resource utilization and enabling efficient processing of large datasets.

Now lets jump into the code and implement Hang fire Background jobs in our applications. Create a new WebApi project in .NET 8.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kajbqk6htbtsa3zf3zi.png)

**Hangfire NuGet Package**
First, we are going to install the Hangfire NuGet package. We can search for Hangfire in the package manager window and other packages too.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lce49u5jemqp2mwsg6t6.png)

**HangFire Storage**
HangFire Create tables for storing job related data in database ensuring that the job information and state are retained even if the application or server restarts. It allows Hangfire to maintain job metadata, track the status of jobs, and resume processing from where it left off in case of interruptions or failures. 
Create Database in SSMS First
```sql
Create Database HangFireDatabase
```
Adding Connection string in appsettings.development.json

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7tdea7y9x492gn0ksa11.png)
Setting up startup class

```csharp
builder.Services.AddHangfire(hangfire =>
{
    hangfire.UseColouredConsoleLogProvider();
    hangfire.UseSqlServerStorage(
                 builder.Configuration.GetConnectionString("DBConnection"));
});
builder.Services.AddHangfireServer();

//adding hangfire dashboard ui
app.UseHangfireDashboard();
```
Now lets create a controller HangFireJobsController and implement our Hangfire job logics in it.
```csharp
using Hangfire;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace HangFIre.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class HangFireJobsControlle : ControllerBase
    {
        private readonly IBackgroundJobClient _backgroundJobClient;

        public HangFireJobsControlle(IBackgroundJobClient backgroundJobClient)
        {
            _backgroundJobClient = backgroundJobClient;
        }
        [HttpGet]
        [Route("FireForgetJobs")]
        public String FireForget()
        {
            //Fire-and-forget jobs are executed only once and almost immediately after creation.
            var jobId = BackgroundJob.Enqueue(() => Console.WriteLine("Fire And Forget Job"));
            return $"Job ID: {jobId}. Fire And Forget jobs Executed!";
        }

        [HttpGet]
        [Route("DelayedJobs")]
        public String Delayed()
        {
            //Delayed jobs are executed only once too, but not immediately, after a certain time interval.
            var jobId = BackgroundJob.Schedule(() => Console.WriteLine("Delayed Job Executed!"), TimeSpan.FromSeconds(10));
            return $"Job ID: {jobId}.Delayed Jobs Executed!";
        }

        [HttpGet]
        [Route("RecurringJobs")]
        public String Recurring()
        {
            //Recurring jobs fire many times on the specified CRON schedule.
            RecurringJob.AddOrUpdate("Recurring Job Run Every Day", () => Console.Write("Recurring Job Executed"), Cron.Daily);
            return "Recurring Job Executed!";
        }

        [HttpGet]
        [Route("ContinuationsJobs")]
        public async Task<String> Continuations()
        {
            //Parent Job
            var jobId = BackgroundJob.Enqueue(() => Console.WriteLine("Continuations Job Parent is Being Executed!"));
            Console.WriteLine("Delayed For 1 Minute");
            await Task.Delay(TimeSpan.FromSeconds(10));
            Console.WriteLine("1 Minute Has Been Completed Child Job Executed");
            BackgroundJob.ContinueJobWith(jobId, () => Console.WriteLine("Continuations Job Child is Being Executed!"));
            return "Continuations Jobs Executed!";
        }
    }
}

```
We have created 4 types of jobs in our controller.
- Fire-and-Forget Jobs
- Delayed Jobs
- Recurring Jobs
- Continuations
Since we are using free version of Hangfire. The are 2 jobs are available in pro version.
- Batches
- Batch Continuations

you can read about it here [HangFire Documentation](https://www.hangfire.io/)

Lets run our application and see the HangFire Dashboard.
> my application is running on https://localhost:7077/ port so to see my Hangfire dashboard https://localhost:7077/hangfire.

right now we can access the dashboard with username and password. and using the default port with our app.
for adding validation and changing default port you can read the Hangfire Documentation.  [HangFire Using Dashboard](https://docs.hangfire.io/en/latest/configuration/using-dashboard.html).
The dashboard looks like that.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5fuql7abux3rmwxnk8j.png)
we can see the recurring jobs, servers and jobs.
In jobs tab we can  see overall jobs overview that are in process, scheduled, awaiting and Deleted etc.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/slr1ekkpk7g0lkelk871.png)
let see recurring jobs that we have added it is shown there.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ldhejqkb07g6ntv5t8k4.png)
you can explore other tabs also. Now we can test our application for different types of jobs that we have implemented using swagger UI.
Run fire and forget jobs and see the output in console windows that its executed.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qi1b59uahcrriyszw8m.png)
we can also the see the history in our jobs tab with id 18.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ren548trg4v9orehnyq3.png)
Lets Execute Delayed Jobs and see the results. Notice we are delaying jobs for 10 seconds it will print out to console after 10 seconds our execution and in Hangfire job tab we can see its scheduled.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oo3otba4gtyb7ryd8kyv.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ui67flarbk6rkfai3i8.png)

Now run recurring jobs and see nothings happens in console. because its cron job that run daily on **every day at 00:00 UTC**.
but we can also execute this job from dashboard to see if its working fine. go to Recurring jobs Tabs checkbox the Recurring Job Run Every Day and press the trigger now button observe the console now its executed.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ku43p19es6sznndcmpk4.png)
Now we need to test our last job which is Continuation Jobs. which is executed when parent job is finished first. notice we have added  a 10 second delay in our code in order to satisfy our self that parent job is being executed. let run the continuation job now and see the result in console.


![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2y1tiqkcjvhhok0zf6v0.png)
observe that first it prints
> "Continuations Job Parent is Being Executed!"
and after ten second delay its prints 
*Continuations Job Child is Being Executed!*
child has dependency on parent job.

The article concludes by providing code snippets and examples demonstrating how to implement Hangfire background jobs in a .NET Core application. It shows how to install the Hangfire NuGet package, configure storage, set up the Hangfire dashboard, and implement different types of jobs using the Hangfire API.

The article also includes screenshots of the Hangfire dashboard, showcasing job monitoring, execution history, and scheduling features.

Overall, the article serves as a comprehensive introduction to Hangfire, highlighting its features and demonstrating how to leverage its capabilities to efficiently handle background tasks in .NET Core applications.