ermi

Using Background Processing to Build Scalable Applications with Hangfire

Nick Branstein

About Me

My Dog Riven

About Me

Senior Consultant @ KiZAN Technologies

Video game lover and enthusiast

Cleveland sports fan

Contact Me

Twitter: @nickbranstein

Blog: brosteins.com

NativeScript in Action

bit.ly/nsinaction

Chapters 1-5 are available in early access right now

The Problem

Lemmings Love To Work

Let's Make Some Houses

Put My Request In For 10 Houses

...then we wait...

The Solution

More Lemmings!

Give Them Different Tasks

Now They Can Work Together

Asynchronously

Hangfire

What

Open source .NET framework

Manage and run asynchronous background jobs

What Is A Background Job?

Any public method

No message class or request/response models are needed

How can you run a job?

Fire-and-forget (one time)

Recurring

Delayed

Batches (pro only)

Where

hangfire.io

Install-Package Hangfire

Why

Simple to install an configure

Powerful built in monitoring

Scalable

Persistent

Reliable

Extensible

Configuration

Setup In ASP.NET


public partial class Startup
{
    public void ConfigureHangfire(IAppBuilder app)
    {
        GlobalConfiguration                
        .Configuration
        .UseSqlServerStorage("DefaultConnection");

        app.UseHangfireDashboard();
        app.UseHangfireServer();
    }
}
                    

You can also configure Hangfire to run in a Windows service

Storage Configuration

Scalability

Scalability Continued

Scalability Configuraiton


var serverOptions = new BackgroundJobServerOptions()
{
    WorkerCount = Environment.ProcessorCount * 5,
    Queues = new[] { QueuePriority.HighPriority, QueuePriority.Default }
};

var recurringOptions = new BackgroundJobServerOptions()
{
    WorkerCount = 1,
    Queues = new[] { QueuePriority.Recurring }
};

_backgroundJobServers.Push(new BackgroundJobServer(serverOptions));
_backgroundJobServers.Push(new BackgroundJobServer(recurringOptions));
                    

The Dashboard

http://localhost:port/hangfire

Server Monitoring

Job Monitoring

Succeeded Jobs

Job Details

Successful jobs are purged in 24 hours (configurable)

Reliability

Failed jobs remain in the queue and are automatically retried

Recurring Jobs

Other Dashboard Functions

Requeue jobs

Delete jobs

Manually trigger recurring jobs

Jobs

Any Public Method


Console.WriteLine("Hello World!");
                    

public void DoBuildHouseJob()
{
    // Build a House
}
                    

Fire-And-Forget


BackgroundJob.Enqueue(() => Console.WriteLine("Hello world!"));
                    

BackgroundJob.Enqueue(() => DoBuildHouseJob());
                    

Recurring


RecurringJob.AddOrUpdate(() => 
    "my-job-id",
	Console.WriteLine("Hello world!"),
	Cron.Daily
);
                    

RecurringJob.AddOrUpdate(() => 
    "my-job-id", 
	DoBuildHouseJob(),
	Cron.Daily
);
                    

Delayed


BackgroundJob.Schedule(() => 
	Console.WriteLine("Hello world!"),
	TimeSpan.FromDays(1)
);
                    

BackgroundJob.Schedule(() => 
	DoBuildHouseJob(),
	TimeSpan.FromDays(1)
);
                    

Best Practices

Keep job arguments simple

Make jobs reentrant

Logging

Demo

Thanks!

@nickbranstein

brosteins.com

https://github.com/NickBranstein/Presentations

Labs are also on Github (CPL16-Hangfire)