diff --git a/Cultiv.Hangfire/HangfireComposer.cs b/Cultiv.Hangfire/HangfireComposer.cs index d42c8a3..7bdb03d 100644 --- a/Cultiv.Hangfire/HangfireComposer.cs +++ b/Cultiv.Hangfire/HangfireComposer.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using Hangfire; using Hangfire.Console; using Hangfire.SqlServer; @@ -7,114 +6,68 @@ using Microsoft.Extensions.DependencyInjection; using Umbraco.Cms.Core.Composing; using Umbraco.Cms.Core.DependencyInjection; -using Umbraco.Cms.Core.Manifest; using Umbraco.Cms.Web.Common.ApplicationBuilder; using Umbraco.Cms.Web.Common.Authorization; -using Umbraco.Extensions; -namespace Cultiv.Hangfire +namespace Cultiv.Hangfire; + +public class HangfireComposer : IComposer { - public class HangfireComposer : IComposer + public void Compose(IUmbracoBuilder builder) { - public void Compose(IUmbracoBuilder builder) - { - builder.ManifestFilters().Append(); - - var connectionString = GetConnectionString(builder); - if (string.IsNullOrEmpty(connectionString)) - { - // This might happen when the package is installed before Umbraco is installed - // https://github.com/nul800sebastiaan/Cultiv.Hangfire/issues/11 - return; - } - - // Configure Hangfire to use our current database and add the option to write console messages - 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(); + builder.ManifestFilters().Append(); - AddAuthorizedUmbracoDashboard(builder); - - // 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); + var connectionString = builder.GetConnectionString(); + if (string.IsNullOrEmpty(connectionString)) + { + // This might happen when the package is installed before Umbraco is installed + // https://github.com/nul800sebastiaan/Cultiv.Hangfire/issues/11 + return; } - private static void AddAuthorizedUmbracoDashboard(IUmbracoBuilder builder) + // Configure Hangfire to use our current database and add the option to write console messages + builder.Services.AddHangfire(configuration => { - // Add the dashboard and make sure it's authorized with the named policy above - builder.Services.Configure(options => - { - options.AddFilter(new UmbracoPipelineFilter(Constants.System.HangfireDashboard) + configuration + .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) + .UseSimpleAssemblyNameTypeSerializer() + .UseRecommendedSerializerSettings() + .UseConsole() + .UseSqlServerStorage(connectionString, new SqlServerStorageOptions { - Endpoints = app => app.UseEndpoints(endpoints => - { - endpoints.MapHangfireDashboardWithAuthorizationPolicy( - pattern: Constants.System.Endpoint, - options: new DashboardOptions(), - authorizationPolicyName: AuthorizationPolicies.SectionAccessSettings); - }) + CommandBatchMaxTimeout = TimeSpan.FromMinutes(5), + SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5), + QueuePollInterval = TimeSpan.Zero, + UseRecommendedIsolationLevel = true, + DisableGlobalLocks = true, }); - }); - } + }); - private static readonly List AllowedSqlProviderNames = - new() { Umbraco.Cms.Persistence.SqlServer.Constants.ProviderName, "System.Data.SqlClient" }; + // Run the required server so your queued jobs will get executed + builder.Services.AddHangfireServer(); - private static string GetConnectionString(IUmbracoBuilder builder) - { - var connectionString = - builder.Config.GetUmbracoConnectionString(Constants.System.AlternativeConnectionStringName); - if (string.IsNullOrWhiteSpace(connectionString) == false) - { - return connectionString; - } + AddAuthorizedUmbracoDashboard(builder); - var providerName = - builder.Config.GetConnectionStringProviderName(Umbraco.Cms.Core.Constants.System.UmbracoConnectionName); - if (providerName != null && AllowedSqlProviderNames.InvariantContains(providerName) == false) - { - throw new NotSupportedException( - $"Cultiv.Hangfire only works on providers `{string.Join("`, `", AllowedSqlProviderNames)}`, your current provider ({providerName}) is not supported."); - } - - return builder.Config.GetUmbracoConnectionString(); - } + // 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); } - internal class CultivHangfireManifestFilter : IManifestFilter + private static void AddAuthorizedUmbracoDashboard(IUmbracoBuilder builder) { - public void Filter(List manifests) + // Add the dashboard and make sure it's authorized with the named policy above + builder.Services.Configure(options => { - manifests.Add(new PackageManifest + options.AddFilter(new UmbracoPipelineFilter(Constants.System.HangfireDashboard) { - PackageName = "Cultiv.Hangfire", - Dashboards = new[] + Endpoints = app => app.UseEndpoints(endpoints => { - new ManifestDashboard - { - Alias = "cultiv.Hangfire", - Sections = new[] { Umbraco.Cms.Core.Constants.Applications.Settings }, - View = "/App_Plugins/Cultiv.Hangfire/dashboard.html" - } - } + endpoints.MapHangfireDashboardWithAuthorizationPolicy( + pattern: Constants.System.Endpoint, + options: new DashboardOptions(), + authorizationPolicyName: AuthorizationPolicies.SectionAccessSettings); + }) }); - } + }); } -} +} \ No newline at end of file diff --git a/Cultiv.Hangfire/ManifestFilter.cs b/Cultiv.Hangfire/ManifestFilter.cs new file mode 100644 index 0000000..e2f6b84 --- /dev/null +++ b/Cultiv.Hangfire/ManifestFilter.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; +using Umbraco.Cms.Core.Manifest; + +namespace Cultiv.Hangfire; + +internal class ManifestFilter : IManifestFilter +{ + public void Filter(List manifests) + { + manifests.Add(new PackageManifest + { + PackageName = "Cultiv.Hangfire", + Dashboards = new[] + { + new ManifestDashboard + { + Alias = "cultiv.Hangfire", + Sections = new[] { Umbraco.Cms.Core.Constants.Applications.Settings }, + View = "/App_Plugins/Cultiv.Hangfire/dashboard.html" + } + } + }); + } +} \ No newline at end of file diff --git a/Cultiv.Hangfire/UmbracoBuilderExtensions.cs b/Cultiv.Hangfire/UmbracoBuilderExtensions.cs new file mode 100644 index 0000000..af5152b --- /dev/null +++ b/Cultiv.Hangfire/UmbracoBuilderExtensions.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using Umbraco.Cms.Core.DependencyInjection; +using Umbraco.Extensions; + +namespace Cultiv.Hangfire; + +public static class UmbracoBuilderExtensions +{ + public static string GetConnectionString(this IUmbracoBuilder builder) + { + var connectionString = + builder.Config.GetUmbracoConnectionString(Constants.System.AlternativeConnectionStringName); + if (string.IsNullOrWhiteSpace(connectionString) == false) + { + return connectionString; + } + + var providerName = + builder.Config.GetConnectionStringProviderName(Umbraco.Cms.Core.Constants.System.UmbracoConnectionName); + if (providerName != null && AllowedSqlProviderNames.InvariantContains(providerName) == false) + { + throw new NotSupportedException( + $"Cultiv.Hangfire only works on providers `{string.Join("`, `", AllowedSqlProviderNames)}`, your current provider ({providerName}) is not supported."); + } + + return builder.Config.GetUmbracoConnectionString(); + } + + + private static readonly List AllowedSqlProviderNames = + new() { Umbraco.Cms.Persistence.SqlServer.Constants.ProviderName, "System.Data.SqlClient" }; +} \ No newline at end of file