Nick Branstein
Senior Consultant @ KiZAN Technologies
Video game lover and enthusiast
Cleveland sports fan
Twitter: @nickbranstein
Blog: brosteins.com
Chapters 1-5 are available in early access right now
...then we wait...
Asynchronously
Open source .NET framework
Manage and run asynchronous background jobs
Any public method
No message class or request/response models are needed
Fire-and-forget (one time)
Recurring
Delayed
Batches (pro only)
Install-Package Hangfire
Simple to install an configure
Powerful built in monitoring
Scalable
Persistent
Reliable
Extensible
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
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));
Successful jobs are purged in 24 hours (configurable)
Failed jobs remain in the queue and are automatically retried
Requeue jobs
Delete jobs
Manually trigger recurring jobs
Console.WriteLine("Hello World!");
public void DoBuildHouseJob()
{
// Build a House
}
BackgroundJob.Enqueue(() => Console.WriteLine("Hello world!"));
BackgroundJob.Enqueue(() => DoBuildHouseJob());
RecurringJob.AddOrUpdate(() =>
"my-job-id",
Console.WriteLine("Hello world!"),
Cron.Daily
);
RecurringJob.AddOrUpdate(() =>
"my-job-id",
DoBuildHouseJob(),
Cron.Daily
);
BackgroundJob.Schedule(() =>
Console.WriteLine("Hello world!"),
TimeSpan.FromDays(1)
);
BackgroundJob.Schedule(() =>
DoBuildHouseJob(),
TimeSpan.FromDays(1)
);
Keep job arguments simple
Make jobs reentrant
Logging
@nickbranstein
brosteins.com
https://github.com/NickBranstein/PresentationsLabs are also on Github (CPL16-Hangfire)