layout | title | active | sub_active | redirect_from |
---|---|---|---|---|
products |
Overview — Hangfire Core |
overview |
overview |
/core/ |
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:
- mass notifications/newsletter;
- batch import from xml, csv, json;
- creation of archives;
- firing off web hooks;
- deleting users;
- building different graphs;
- image/video processing;
- purge temporary files;
- recurring automated reports;
- database maintenance.
Hangfire supports several kinds 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.
These jobs are executed only once and almost immediately after they are fired.
var jobId = BackgroundJob.Enqueue( () => Console.WriteLine("Fire-and-forget!"));
Recurring jobs are fired many times on the specified CRON schedule.
RecurringJob.AddOrUpdate( "myrecurringjob", () => Console.WriteLine("Recurring!"), Cron.Daily);
Batch is a group of background jobs created atomically.
var batchId = Batch.StartNew(x =>
{
x.Enqueue(() => Console.WriteLine("Job 1"));
x.Enqueue(() => Console.WriteLine("Job 2"));
});
Use them when you need to run background processes continuously throughout the lifetime of your application.
public class CleanTempDirectoryProcess : IBackgroundProcess
{
public void Execute(BackgroundProcessContext context)
{
Directory.CleanUp(Directory.GetTempDirectory());
context.Wait(TimeSpan.FromHours(1));
}
}
Background jobs are very important part of an application and Hangfire ensures that any job is performed at least once. To persist background job information between application restarts, all the information is saved in your favorite persistent storage. Currently the following storages are officially supported:
And support for a lot of other storages are implemented by the community, please see the Extensions page for details.
Storage subsystem is abstracted enough to support RDBMS and NoSQL solutions. If your favorite transactional database system is not supported yet, it's likely possible to implement its support as an extension.
If your background job encounters a problem during its execution, it will be retried automatically after some delay. Hangfire successfully deals with the following problems:
- Exceptions
- Application shutdowns
- Unexpected process terminations
You are also able to retry any background job manually through the programming code or the Dashboard UI:
var jobId = BackgroundJob.Enqueue( () => Console.WriteLine("Hello"));var succeeded = BackgroundJob.Requeue(jobId);
You are not required to make any architecture decisions to start using Hangfire. You can begin with simple setup, where background processing is implemented on the web application side.
Later, when you face performance problems, you can separate the processing among different processes or servers – Hangfire uses distributed locks to handle synchronization issues.
<!-- Tab panes -->
<div class="tab-content tab-content-center text-center">
<div role="tabpanel" class="tab-pane active" id="process">
<img src="{{ site.cdn }}/img/process.png" alt="Single Process">
</div>
<div role="tabpanel" class="tab-pane" id="garden">
<img src="{{ site.cdn }}/img/garden.png" alt="Web Garden">
</div>
<div role="tabpanel" class="tab-pane" id="farm">
<img src="{{ site.cdn }}/img/farm.png" alt="Web Farm">
</div>
<div role="tabpanel" class="tab-pane" id="service">
<img src="{{ site.cdn }}/img/service.png" alt="Separate Service">
</div>
<div role="tabpanel" class="tab-pane" id="server">
<img src="{{ site.cdn }}/img/server.png" alt="Separate Server">
</div>
</div>
Hangfire is shipped with an awesome tool – Web Monitoring UI. It is implemented as an OWIN extension and can be hosted inside any application – ASP.NET, Console or Windows Service. Monitoring UI allows you to see and control any aspect of background job processing, including statistics, exceptions and background job history.
Just look at the screenshots below, and you'll love it!