Last active
September 18, 2021 18:48
-
-
Save nul800sebastiaan/83c3da828a72c15a89259938402f2449 to your computer and use it in GitHub Desktop.
Acoompanies this blog post: https://cultiv.nl/blog/setting-up-hangfire-in-umbraco-9/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Hangfire; | |
using Hangfire.Console; | |
using Hangfire.SqlServer; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Umbraco.Cms.Core.Composing; | |
using Umbraco.Cms.Core.DependencyInjection; | |
using Umbraco.Cms.Web.BackOffice.Authorization; | |
using Umbraco.Cms.Web.Common.ApplicationBuilder; | |
namespace Cultiv.Hangfire.Composers | |
{ | |
public class HangfireComposer : IComposer | |
{ | |
public void Compose(IUmbracoBuilder builder) | |
{ | |
// Configure Hangfire to use our current database and add the option to write console messages | |
var connectionString = builder.Config.GetConnectionString(Umbraco.Cms.Core.Constants.System.UmbracoConnectionName); | |
builder.Services.AddHangfire(configuration => | |
{ | |
configuration | |
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170) | |
.UseSimpleAssemblyNameTypeSerializer() | |
.UseRecommendedSerializerSettings() | |
.UseConsole() | |
.UseSqlServerStorage(connectionString, new SqlServerStorageOptions | |
{ | |
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5), | |
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5), | |
QueuePollInterval = TimeSpan.Zero, | |
UseRecommendedIsolationLevel = true, | |
DisableGlobalLocks = true, | |
}); | |
}); | |
// Run the required server so your queued jobs will get executed | |
builder.Services.AddHangfireServer(); | |
// Add a named policy to authorize requests to the dashboard | |
builder.Services.AddAuthorization(options => | |
{ | |
options.AddPolicy("HangfireDashboard", policy => | |
{ | |
// We require a logged in backoffice user who has access to the settings section | |
policy.AuthenticationSchemes.Add(Umbraco.Cms.Core.Constants.Security.BackOfficeAuthenticationType); | |
policy.Requirements.Add(new SectionRequirement(Umbraco.Cms.Core.Constants.Applications.Settings)); | |
}); | |
}); | |
// Add the dashboard and make sure it's authorized with the named policy above | |
builder.Services.Configure<UmbracoPipelineOptions>(options => | |
{ | |
options.AddFilter(new UmbracoPipelineFilter("HangfireDashboard") | |
{ | |
Endpoints = app => app.UseEndpoints(endpoints => | |
{ | |
endpoints.MapHangfireDashboard( | |
pattern: "/umbraco/backoffice/hangfire", | |
options: new DashboardOptions()).RequireAuthorization("HangfireDashboard"); | |
}).UseHangfireDashboard() | |
}); | |
}); | |
// For some reason we need to give it the connection string again, else we get this error: | |
// https://discuss.hangfire.io/t/jobstorage-current-property-value-has-not-been-initialized/884 | |
JobStorage.Current = new SqlServerStorage(connectionString); | |
// Now we can queue our jobs | |
var jobsQueue = new JobsQueue(); | |
jobsQueue.ScheduleJobs(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case you're interested in the silly job as well, here's
JobsQueue.cs
and
IJobsQueue.cs