What is Hangfire?
Hangfire is an open-source library for .NET that provides a simple way to perform background processing in your application. It allows you to run jobs (methods or functions) asynchronously and schedule them to run at a specific time or regularly.
Main components of Hangfire architecture
- Client: This is the code in your application that enqueues background jobs and schedules them to run at a specific time or on a recurring basis. The client API provides a simple way to create and manage background jobs from within your code.
- Server: This is a background process that runs continuously and is responsible for executing scheduled and queued jobs. It listens to the job storage and picks up new jobs as they become available.
- Storage: Hangfire provides a database to store all the information related to background jobs, including their definitions, execution status, and more. Hangfire creates a couple of designated tables in the database to manage this information.
By default, it uses SQL Server, but any other supported option is also easy to configure.
Hangfire workflow
Setting Up Hangfire
NuGet Package
You need to install the Hangfire package.
Hangfire.SqlServer and Hangfire.The core is installed by default.
Setup Storage
- Hangfire has the ability to use a SQL Server database by default for storing job definitions and statuses. Additionally, you have the option to choose other alternatives. For our example project, I have decided to use MSSQL local storage for simplicity.
- It is essential to have a database definition, regardless of whether you use a local or remote database. Hangfire is capable of creating the necessary tables for job storage, but it cannot create a database. Therefore, you must provide a database.
Once you have set up our local database, you must update the appsettings.json file.
Startup.cs setup
- You need to add Hangfire to the Services collection.
- You need to provide the connection string
- You also add the Hangfire server with the AddHangfireServer() method.
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddHangfire(x =>
x.UseSqlServerStorage("Data Source=data source; Initial Catalog=Hangfire-DB;Integrated Security=true;Max Pool Size = 200;"));
services.AddHangfireServer();
}
- Lastly, you can add Hangfire Dashboard for easily monitoring servers and jobs.By calling the UseHangfireDashboard() method you are adding it to pipeline.
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
app.UseHangfireDashboard(); // adding hangfire dashboard
}
Persistence:
- Hangfire will automatically check your storage for the necessary tables, and if they don’t exist, it will create them for you:
Hangfire Dashboard
- This is a web-based UI that allows you to monitor the state of your background jobs.
- You can use the dashboard to view the status of jobs, retry failed jobs, and cancel running jobs if necessary.
- To check the dashboard, you need to browse on /hangfire page.
Background jobs
There are several types of background jobs that you can perform using Hangfire:
Fire-and-forget
These are jobs that are executed once and then forgotten. They are used for tasks that need to be performed in the background, but the result doesn't need to be returned to the application immediately.
Delayed jobs
These are jobs that are scheduled to run at a specific time in the future. You can use them to schedule tasks that need to be performed at a specific time, such as sending a reminder email to a user.
Recurring jobs
These are jobs that are scheduled to run repeatedly at a specified interval. You can use them to automate tasks that need to be performed on a regular basis, such as generating reports or performing database cleanup.
Continuations
These are jobs that are executed after a previous job has been completed. You can use them to create a sequence of jobs that need to be performed in a specific order.
Batch jobs
These are jobs that are grouped together and executed as a single unit. You can use them to perform a set of related tasks that need to be executed together.
Executing jobs
1. Fire and Forget jobs
2. Delayed jobs
3. Continuation jobs
4. Reccuring jobs
- A recurring job is a task that needs to be performed repeatedly on a schedule. For example, sending a weekly email newsletter or performing a daily database backup.
- Using Hangfire's AddOrUpdate method, you can define the schedule for a recurring job using a cron expression, which is a string that specifies the frequency and timing of the job.
- Here's how the AddOrUpdate method works: you first need to specify a unique identifier for the job, which will be used to identify it later. Then, you provide a lambda expression that defines the method to be executed as the job. Finally, you specify the cron expression that defines the job's schedule.
Let's create another endpoint:
[HttpGet("RecurringJob")]
public IActionResult recurringJob()
{
RecurringJob.AddOrUpdate("myrecurringjob", () => Console.WriteLine("hello testing hangfire RecurringJob"), Cron.Minutely());
//sendmail();
return Ok("recurring job is done");
}
5. Batch jobs