As part of my application I wanted to run a background service. In some fantasy future this might run as a separate process on another machine, scaling independently of the API server, so the service would naturally be isolated in its own class. For now I just needed something that would run scheduled jobs and be initialized during the
Startup methods. The most popular solution for this problem seems to be a library called Hangfire which has had ASP.NET Core support since v1.6.0 (v1.6.16 at the time of writing).
Hangfire is backed by a database, so part of the setup involves selecting a database connector. There are two options for MySql, but the link for Hangfire.MySql goes 404, so I opted for Hangfire.MySqlStorage. I was able to get the basics of Hangfire working with this connector, although I did encounter some problems, notably that the Recurring Jobs dashboard page causes MySql exceptions and doesn’t load. One factor in this may be that, with Hangfire.Mysql as well as Pomelo.EntityFrameworkCore.MySql, I have references to different definitions of various MySql.Data.* classes in multiple assemblies. But as it currently works for my purposes, I haven’t pursued those errors further.
The other decision around the database is whether to share with the application database or use a separate schema. I opted for the latter to avoid any complications with my migration and test data code.
With that, we present the code. Firstly the
<PackageReference Include="Hangfire" Version="1.6.*" />
<PackageReference Include="Hangfire.Autofac" Version="2.3.*" />
<PackageReference Include="Hangfire.MySqlStorage" Version="1.1.0-alpha" />
And then the startup functions. The first is called from
protected virtual void AddHangfireService(IServiceCollection services)
TransactionIsolationLevel = System.Data.IsolationLevel.ReadCommitted,
QueuePollInterval = TimeSpan.FromSeconds(60),
JobExpirationCheckInterval = TimeSpan.FromHours(1),
CountersAggregateInterval = TimeSpan.FromMinutes(5),
PrepareSchemaIfNecessary = true,
DashboardJobListLimit = 50000,
TransactionTimeout = TimeSpan.FromMinutes(1),
and the second from
protected virtual void ConfigureHangfire(IApplicationBuilder app)
notifier => notifier.Rollup(DateTime.UtcNow.AddDays(-1)),
Cron.Daily(15) // 15:00 UTC - i.e. 3am NZST, 1am AEST
This runs my job daily at 1500 UTC, which is the middle of the night from my perspective.
One aspect that Hangfire does very well is integrate with dependency injection frameworks. I have used Autofac, and you can see in the code above that nowhere have I had to construct the class for the
notifier variable, instead the interface parameter
INotifier suffices. The integration with Autofac is established in
options.UseAutofacActivator(this.IocContainer); in the first code block. At the time
UseAutofacActivator is called
this.IocContainer is still null, but it doesn’t appear to be used until after Autofac is setup, which happens very soon thereafter.