Hangfire is an open source framework that helps you create, process and manage your background tasks, operations that you would otherwise not want to place in the request pipeline: bulk messages/mailing; batch import from xml, csv, json; create archives; remove users; build various graphs; process images/videos; clear temporary files; recurring automated reports; maintain a database.
The library consists of three important components: the client itself, the repository, and the server.
Client - has the ability to create various types of background tasks using Hangfire.
Server - processing background tasks with the Hangfire server. Implemented as a set of dedicated background threads (not thread pools) that can retrieve tasks from the repository and process them. The server is responsible for the cleanliness of the storage and automatic deletion of obsolete data.
Storage - Hangfire stores background tasks and processing-related information in persistent storage.
Task types
Fire-and-forget - Executed only once, immediately after calling var jobId= BackgroundJob. Enqueue (
( ) = ≻ Console. WriteLine("Fire-and-forget!"));
Delayed - Runs only once, running after a specified time interval var jobID=BackgroundJob.Schedule(
( ) = ≻ Console.WriteLine ("Delayed!"), TimeSpan. FromDays(7));
Recurring - Recurring tasks run many times according to the CRON schedule (for example “14 09 * * WED”, this expression will run the task every Wednesday at 14:09 UTC. Site where you can edit the CRON schedule)
RecurringJob. Add0rUpdate( "myrecurringjob", ( ) = ≻ Console.WriteLine("Recurring!"), Cron.Daily);
Continuations – Runs only once after the parent job completes (When running jobs, the jobId is returned and must then be passed to ContinueJobWith) BackgroundJob.ContinueJobWith( jobId, ( )= ≻Console.WriteLine("Continuation!"));
If there are any exceptions, it will be recorded in the database and in the future there is an opportunity to review it to correct the code. If the task is not completed, there will be several more attempts to complete it after a certain period of time, and there is also the possibility to manually launch a specific task from the Dashboard. A pretty great Dashboard for tracking information about running and pending tasks, etc.
Requirements for Hangfire
●.NET Framework 4.5
●Persistent storage (listed below)
●Newtonsoft.Jsonlibrary ≥ 5.0.1
●SQL Azure, SQL Server 2008 R2 (and later of any edition, including Express) or Redis