Hangfire Background Recurring Jobs — Console Application
Hangfire is an open-source framework that helps you to create, process and manage your background jobs, i.e. operations you don’t want to put in your request processing pipeline:
Hangfire supports all kind of background tasks — short-running and long-running, CPU intensive and I/O intensive, one shot and recurrent. You don’t need to reinvent the wheel — it is ready to use.
Hangfire supports different types of background jobs:
Fire-and-forget, Background Process, Delayed, Recurring, Continuations, Batches, Batch Continuations
In this article, I am going to explain how to configure Hangfire for processing Background Recurring Jobs in Console Application.
Install the Hangfire package using NuGet
PM> Install-Package Hangfire
In the App.config, add the connecting string to get Hangfire Job Storage as:
<add name=”HangfireConnection” connectionString=”Data Source=GHANSHYAMS\SQLEXPRESS;Initial Catalog=Hangfire_Demo;Integrated Security=True;” providerName=”System.Data.SqlClient”/>
Now, install Topshelf package from NuGet and also some more packages like Microsoft.Owin.Host.HttpListener, Microsoft.Owin.Host.SystemWeb & Microsoft.Owin.Hosting
Add the Startup.cs class and configure the Hangfire dashboard.
Recurring Job
The Recurring job structure consists of JobId, Method, Queue, Type, Cron expression
By default Hangfire processes Default queue, but you can also specify your own queue like:
Queue("alpha")]
public void SomeMethod() { }
To begin processing queues, you need to update your BackgroundJobServer configuration.
var options = new BackgroundJobServerOptions
{
Queues = new[] { "alpha", "beta", "default" }
};
app.UseHangfireServer(options);
Methods to Trigger by Hangfire
After all configurations are done, start the console application and open the below URL in browser as:
You will hangfire dashboard accessed with “/hangfire”.
The hangfire dashboard shows various information about your Recurring Jobs, Servers, Retries, Jobs status — succeeded, failed, processing, deleted, awaiting, scheduled, enqueued
Hangfire References
https://www.hangfire.io/overview.html
My GitHub Project Reference
Happy Learning