Introduction
Hangfire is an excellent open source .net framework platform scheduled tasks and .net core platform that can easily integrate easily into your regular tasks of the program, yet powerful. Support for back-end CPU and I / O intensive, long-run and short-run jobs. No need Windows service / Task Scheduler. Provide Redis, SQL Server, SQL Azure and MSMQ persistent support. Colleagues also provides integrated visual interface can be localized elegant task manually manage, and support Chinese interface.
Github Address
https://github.com/HangfireIO/Hangfire
How to install?
.Net applications in C # and .net core, the most convenient we use a third-party database management than Nuget, so you can simply search HangFire, as of posting date, version 1.7.6, as follows:
Or execute the following commands in the package manager, arbitrary two ways
Install-Package Hangfire
Quick Start
The following is a simple piece of code for configuring HangFire, code is an example of SQLServer
public void Configuration(IAppBuilder app)
{
GlobalConfiguration.Configuration.UseSqlServerStorage("<connection string or its name>");
app.UseHangfireServer();
app.UseHangfireDashboard();
}
Features
It does not cover how to how to call in a specific application, the following is a brief introduction to its function, some common features are also mentioned on the official Github.
- That made disappear task
BackgroundJob.Enqueue(() => Console.WriteLine("Simple!"));
- Delay task
Program only after a given period of time to perform background tasks
BackgroundJob.Schedule(() => Console.WriteLine("Reliable!"), TimeSpan.FromDays(7));
- Repeat timing task
Repetitive tasks is as simple as calling the following methods can be any type of repetitive tasks using a CRON expression, due to the support CRON expression, so its support to the second level
RecurringJob.AddOrUpdate(() => Console.WriteLine("Transparent!"), Cron.Daily);
- Multitasking work together
Scheduling a plurality of tasks among each other to allow complete more complex scheduling tasks
var id = BackgroundJob.Enqueue(() => Console.WriteLine("Hello, "));
BackgroundJob.ContinueWith(id, () => Console.WriteLine("world!"));
Web job
You can handle background tasks OWIN in any compatible application framework, including ASP.NET MVC, ASP.NET Web API, FubuMvc, Nancy and so on.
app.UseHangfireServer();
In the console application, Windows service, Azure Worker Role like:
using (new BackgroundJobServer())
{
Console.WriteLine("Hangfire Server started. Press ENTER to exit...");
Console.ReadLine();
}
Usage
The following screenshot is an example of some of the author during the configuration of the project, its management panel is simple to have an understanding
- Glance dashboard implementation of their mandates
- Job Queue List
Task Queue tasks include planning, implementation tasks, completed tasks, failed tasks, delete tasks, as well as the waiting task,
You can also view the implementation of the order to the need to manually retry
- Periodic task list management
The timing of periodic tasks, where it is clear, and can be performed manually, I would have done one thing at present, since the early part of the maintenance manual project data base is very possible, in order to prevent the timeliness of some basic data cache, in after updating the database manually perform the task of rebuilding the cache, a task given the current month, so you do not always have to restart the application.
- Running server
Since HangFire is distributed, then you can see the dashboard server running
to sum up
HangFire is definitely a worthwhile use of the distributed task framework, permanent free Community Edition, even in commercial situations is free, community edition has been good enough in most cases, unless you have a very complex task need to achieve, you can also charge version, supports more powerful! If you have a better recommendation can leave a message in the comments area to share!