Hangfire 1.5.5

Why?

  • Simple to use

  • Can be used in any type of .net application e.g. asp.net, win service, console etc

  • No need for windows scheduler

  • Mgmt and visibility of jobs via the library and dash board

  • Can monitor, review, re-run and track all background jobs

  • Will log start/complete job time

  • Will fire background job multiple times if it doesn't complete ok

  • Stores job definition in database

  • Only marks job as complete when last line of job has been execute ok

  • Background job can be simple (i.e. the job instruction and job execution are run in under teh same app) or distributed (i.e the job is run under another app)

  • e.g. WebAPI executes the job instruction to save the job definition to the database

  • e.g. Win Service executes the actual job

  • Removes ASP.NET issues e.g. running background threads can pose issues, multiple instances might fire job more than once, app domin and app pool recycling

  • Open source free to use for commercial use

Hangfire Jobs

  • Fire and forget jobs (one off jobs)

  • Delay jobs (fires after a delay)

  • Reocurring jobs (fires on a schedule)

  • Continuation jobs (fire after parent jobs)

  • Batch jobs (fires a batch of jobs)

  • Batch continuations (fires after a batch of jobs has completed)

Hangfire Architeture

  • Hangfire client (creates jobs)

  • Hangfire server (runs jobs)

  • Hangfire job storage (stores jobs)

Installing and Running Hangfire

  • Installing Hangfire using Nuget:

-- Installs; HangfireCore, Hangfire, HangfireSqlServer

  • Configuring Hangfire:

-- If using OWIN copy the Hangfire startup code from readme.txt to Startup.cs

-- If not using OWIN copy the Hangfire startup code from readme.txt to Global.asax

-- Also add using Hangfire; statement

-- What is the start up code:

--- GlobalConfiguration.Configuration is an extension method to tell hangfire the database connection name or database connection string to the Hangfire database

--- You need to create an empty Hangfire database, so when Hangfire starts up it will automatically create the database objects and required data

--- app.UseHangfireDashboard() starts up the dash board

--- app.UseHangfireServer() starts up the server that will get and run the jobs

--- Access the Hangfire dash board by the root url/hangfire

  • Coding a background job:

-- e.g. BackgroundJob.Enqueue(() => _service.DoSomething(entity.Id));

Hangfire Integration

  • Single process; run hangire client and server within same app

  • Web garden; multiple instances of simple process apps. The same jobs won't picked up)

  • Web farm; Web garden on multiple servers

  • Seperate service; hangfire client in seperate app from Hangfire sever (note that both apps need access background jobs code/project)

  • Seperate server; Serate service on different servers

Hangfire Pro Features

  • Batch jobs and chaining batch jobs is a pro feature

  • Allows complex workflows i.e. multiple continuations

  • Supports Redis storage

  • Allows windows performance counters to be used

Hangfire Background Jobs

  • Fire-and-forget (i.e. Enqueued job):

(starts a job straight away)

-- BackgroundJob.Enqueue()

--- The fire-and-forget enqueue method takes a lambda expression and store it as json in the Hangfire database

--- It serialises the method and parameter(s) into json

--- Creates a background job record/definition in the Hangfire database and queue

--- Returns to the caller immediately

  • Delayed (i.e. Scheduled job):

(starts a job later)

-- BackgroundJob.Schedule()

--- Specify using lambda and TimeSpan

--- Hangfire server queues scheduled jobs

--- If running hangfire Server in ASP.NET then configure to always run in IIS

  • Recurring (i.e. Recurring job):

(adds or updates a reocurring job)

-- AddOrUpdate() to create or update a recurring job

--- Specify by passing a job identifier, lambda expression and CRON type or expression

-- RemoveIfExists() to remove a job

--- Pass a job id

-- Trigger() to trigger a reocurring job immediately without impacting existign schedule

--- Specify by passing a job id

-- Stores a reocurring job to storage and checks every minute to see if it needs to run the job

  • Using batches

(is for hangfire Pro only, is a different Nuget package i.e. Hangfire.Pro)

-- BatchJob.StartNew()

--- Need to specific Configuration.Usebatches() in config

See pluralsight course and code for more info on batch jobs

  • Chainging batches

See pluralsight course and code

  • Complex workflows

See pluralsight course and code

  • Passing parameters

-- Supports passing of multiple parameters

-- Parameters are serialised (array, collections, custom objects)

-- Serialisation of parameters allows process boundaries to be passed

-- Reference parameters (i.e. pointers to memory locations) not supported. Ref and out not supported

-- It is more efficient to pass concrete vales e.g. ids rather than complex objects e.g. entity

-- The background job thread needs access to the parameter types to deserialize the json objects to .NET objects

  • Passing Dependancies

(How to pass dependant objects)

-- The JobActivator creates an instance using the default constructor

-- Can have a constructor for passing in instances for unit testing

-- Hangfire has extension packages for IoC (StructureMap wasn't listed need to check)

-- Dependancies must be instantiated using the default constructor or you must have a means of injecting those dependancies in

-- Hangfire.StructureMap NuGet package exists

Hangfire Dashboard

  • Is written as OWIN middleware

  • Can be hosted in console app or windows service

  • Requires:

-- Microsoft.Owin.Host.SystemWeb

-- OWIN startup class

-- app.UseHangfireDashboard()

Hangfire Dashboard Features

  • Servers

-- Shows number of worker threads available

-- Can configure server to use a number of threads

-- Shows queues the server is listening to

-- Can configure server to look at specific queues

  • Jobs

  • Retries

-- Default retries is 10 times

-- If backgroundjob throws an exception then it fails and will be retried

  • Recurring

  • Scheduled

  • Failed

-- After 10 job failures the job will be listed here

  • Awaiting

-- Are continuation jobs waiting on other jobs to complete

  • Succeeded

-- Can requeue a job to run again

Hangfire dashboard Configuration

Use app.UseHangfireDashboard(...) and pass DashboardOptions object to change config settings below

  • Creating a link

  • Changing the URL

  • Changing 'Back to site' link

  • Multiple dashboards

Pass a different SqlServerStorage(..) object to UseHangfireDashboard(..) to set multiple Hangfire sql server databases and dash boards

Logging

  • Supports Log4Net

  • Provides automatic logging

  • Can use ILogProvider and ILog for custom logging

Unit Testing

  • Provides IBackgroundJobClient to mock background jobs

Job Filters

  • Can be used to intercept events e.g. start/status of jobs

  • Inhereit from JobFilterAttribute and and interface e.g. IApplyStateFilter, IClientFilter, IElectStateFilter, IServerFilter, IEventsLogsFilter etc

Links

Hangfire demo...

http://localhost:4321/hf

Hangfire job on Console/Web App solution?

http://stackoverflow.com/questions/34441144/hangfire-job-on-console-web-app-solution

How To: Install Hangfire without ASP.NET MVC

http://frankouimette.com/tutorial-installing-hangfire-without-asp-net-mvc/

Processing jobs in a Windows Service

http://docs.hangfire.io/en/latest/background-processing/processing-jobs-in-windows-service.html

How can I use dashboard in Self-Hosted OWIN Console Application?

https://discuss.hangfire.io/t/how-can-i-use-dashboard-in-self-hosted-owin-console-application/1019

https://www.amazon.com/exec/obidos/ASIN/076790768X/theatlanticsyste

https://www.youtube.com/watch?v=0tj6VLuSowQ

https://code.msdn.microsoft.com/ASPNET-MVC-HangFire-bb45a2fe

https://code.msdn.microsoft.com/Integrate-background-jobs-be713dc4

http://docs.hangfire.io/en/latest/background-methods/using-ioc-containers.html

For<jobstorage>().Use(new SqlServerStorage("connection-string-name"));

For<ijobfilterprovider>().Use(JobFilterProviders.Providers);

https://github.com/cocowalla/Hangfire.StructureMap

HttpContext.Current is also not available during the job performance. Don't use it!

results matching ""

    No results matching ""