From 3909a3d9596011d3ff9fe26751d20f3ac9573ee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20N=C3=A9meth?= Date: Wed, 11 Dec 2024 17:37:12 +0100 Subject: [PATCH 1/7] Adding maintenance --- .../Constants/FeatureNames.cs | 2 + .../Lombiq.Hosting.Tenants.Maintenance.csproj | 5 ++ .../UpdateEnabledFeatures/Startup.cs | 26 ++++++++ ...UpdateEnabledFeaturesMaintenanceOptions.cs | 8 +++ .../UpdateFeatureListMaintenanceProvider.cs | 66 +++++++++++++++++++ .../CustomMaintenanceOptions.cs | 6 ++ .../CustomMaintenanceProvider.cs | 55 ++++++++++++++++ .../UpdateOpenIdSettings/Startup.cs | 26 ++++++++ .../Services/ICustomMaintenanceHandler.cs | 9 +++ 9 files changed, 203 insertions(+) create mode 100644 Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/Startup.cs create mode 100644 Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/UpdateEnabledFeaturesMaintenanceOptions.cs create mode 100644 Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/UpdateFeatureListMaintenanceProvider.cs create mode 100644 Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceOptions.cs create mode 100644 Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs create mode 100644 Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/Startup.cs create mode 100644 Lombiq.Hosting.Tenants.Maintenance/Services/ICustomMaintenanceHandler.cs diff --git a/Lombiq.Hosting.Tenants.Maintenance/Constants/FeatureNames.cs b/Lombiq.Hosting.Tenants.Maintenance/Constants/FeatureNames.cs index a62185d5..0433aad8 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Constants/FeatureNames.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Constants/FeatureNames.cs @@ -11,4 +11,6 @@ public static class FeatureNames public const string ChangeUserSensitiveContent = Maintenance + "." + nameof(ChangeUserSensitiveContent); public const string DeleteOrRebuildElasticsearchIndices = Maintenance + "." + nameof(DeleteOrRebuildElasticsearchIndices); public const string DeleteElasticsearchIndicesBeforeSetup = Maintenance + "." + nameof(DeleteElasticsearchIndicesBeforeSetup); + public const string UpdateEnabledFeatures = Maintenance + "." + nameof(UpdateEnabledFeatures); + public const string CustomMaintenance = Maintenance + "." + nameof(CustomMaintenance); } diff --git a/Lombiq.Hosting.Tenants.Maintenance/Lombiq.Hosting.Tenants.Maintenance.csproj b/Lombiq.Hosting.Tenants.Maintenance/Lombiq.Hosting.Tenants.Maintenance.csproj index 9c2b1d8e..2c9cf0fd 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Lombiq.Hosting.Tenants.Maintenance.csproj +++ b/Lombiq.Hosting.Tenants.Maintenance/Lombiq.Hosting.Tenants.Maintenance.csproj @@ -31,6 +31,7 @@ + @@ -56,4 +57,8 @@ + + + + diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/Startup.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/Startup.cs new file mode 100644 index 00000000..6dab280c --- /dev/null +++ b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/Startup.cs @@ -0,0 +1,26 @@ +using Lombiq.HelpfulLibraries.OrchardCore.Mvc; +using Lombiq.Hosting.Tenants.Maintenance.Constants; +using Lombiq.Hosting.Tenants.Maintenance.Services; +using Microsoft.Extensions.DependencyInjection; +using OrchardCore.Environment.Shell.Configuration; +using OrchardCore.Modules; + +namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.UpdateEnabledFeatures; + +[Feature(FeatureNames.UpdateEnabledFeatures)] +public sealed class Startup : StartupBase +{ + private readonly IShellConfiguration _shellConfiguration; + + public Startup(IShellConfiguration shellConfiguration) => + _shellConfiguration = shellConfiguration; + + public override void ConfigureServices(IServiceCollection services) + { + services.BindAndConfigureSection( + _shellConfiguration, + "Lombiq_Hosting_Tenants_Maintenance:UpdateEnabledFeatures"); + + services.AddScoped(); + } +} diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/UpdateEnabledFeaturesMaintenanceOptions.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/UpdateEnabledFeaturesMaintenanceOptions.cs new file mode 100644 index 00000000..82ce0ae6 --- /dev/null +++ b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/UpdateEnabledFeaturesMaintenanceOptions.cs @@ -0,0 +1,8 @@ +namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.UpdateEnabledFeatures; + +public class UpdateEnabledFeaturesMaintenanceOptions +{ + public bool IsEnabled { get; set; } + public string EnableFeatures { get; set; } + public string DisableFeatures { get; set; } +} diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/UpdateFeatureListMaintenanceProvider.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/UpdateFeatureListMaintenanceProvider.cs new file mode 100644 index 00000000..1bdfdb4a --- /dev/null +++ b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/UpdateFeatureListMaintenanceProvider.cs @@ -0,0 +1,66 @@ +using Lombiq.Hosting.Tenants.Maintenance.Extensions; +using Lombiq.Hosting.Tenants.Maintenance.Models; +using Lombiq.Hosting.Tenants.Maintenance.Services; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using OrchardCore.Environment.Extensions.Features; +using OrchardCore.Environment.Shell; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.UpdateEnabledFeatures; + +public class UpdateEnabledFeaturesMaintenanceProvider : MaintenanceProviderBase +{ + private readonly UpdateEnabledFeaturesMaintenanceOptions _options; + private readonly IShellFeaturesManager _shellFeaturesManager; + private readonly ILogger _logger; + + public UpdateEnabledFeaturesMaintenanceProvider( + IOptions options, + IShellFeaturesManager shellFeaturesManager, + ILogger logger) + { + _options = options.Value; + _shellFeaturesManager = shellFeaturesManager; + _logger = logger; + } + + public override Task ShouldExecuteAsync(MaintenanceTaskExecutionContext context) => + Task.FromResult(_options.IsEnabled + && !context.WasLatestExecutionSuccessful() + ); + + public override async Task ExecuteAsync(MaintenanceTaskExecutionContext context) + { + var availableFeatures = (await _shellFeaturesManager.GetAvailableFeaturesAsync()).ToList(); + + if (_options.EnableFeatures?.Replace(" ", string.Empty).SplitByCommas() is { } enableFeaturesOption) + { + var enableFeatures = availableFeatures.Where(feature => + enableFeaturesOption.Contains(feature.Id)).ToList(); + await _shellFeaturesManager.EnableFeaturesAsync(enableFeatures, force: true); + await NotifyAsync(enableFeatures); + } + + if (_options.DisableFeatures?.Replace(" ", string.Empty).SplitByCommas() is { } disableFeaturesOption) + { + var disableFeatures = availableFeatures.Where(feature => + disableFeaturesOption.Contains(feature.Id)).ToList(); + await _shellFeaturesManager.DisableFeaturesAsync(disableFeatures, force: true); + await NotifyAsync(disableFeatures, enabled: false); + } + } + + private ValueTask NotifyAsync(IEnumerable features, bool enabled = true) + { + foreach (var feature in features) + { + _logger.LogInformation("{FeatureName} was {Action} by maintenance.", feature.Name ?? feature.Id, enabled ? "enabled" : "disabled"); + } + + return ValueTask.CompletedTask; + } +} diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceOptions.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceOptions.cs new file mode 100644 index 00000000..89d7d0bc --- /dev/null +++ b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceOptions.cs @@ -0,0 +1,6 @@ +namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.UpdateOpenIdSettings; + +public class CustomMaintenanceOptions +{ + public bool IsEnabled { get; set; } +} diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs new file mode 100644 index 00000000..e3759226 --- /dev/null +++ b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs @@ -0,0 +1,55 @@ +using Lombiq.Hosting.Tenants.Maintenance.Extensions; +using Lombiq.Hosting.Tenants.Maintenance.Models; +using Lombiq.Hosting.Tenants.Maintenance.Services; +using Microsoft.AspNetCore.DataProtection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using OfficeFreund.OpenId.Constants; +using OfficeFreund.OpenId.Services; +using OfficeFreund.OpenId.Settings; +using OrchardCore.ContentManagement.Utilities; +using OrchardCore.Environment.Shell; +using OrchardCore.Modules; +using OrchardCore.OpenId.Configuration; +using OrchardCore.OpenId.Services; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.UpdateOpenIdSettings; + +public class CustomMaintenanceProvider : MaintenanceProviderBase +{ + private readonly ShellSettings _shellSettings; + private readonly ILogger _logger; + private readonly CustomMaintenanceOptions _options; + private readonly IEnumerable _customMaintenanceHandlers; + + public CustomMaintenanceProvider( + ShellSettings shellSettings, + IOptions updateOpenIdSettingsMaintenanceOptions, + ILogger logger, + IEnumerable customMaintenanceHandlers) + { + _shellSettings = shellSettings; + _options = updateOpenIdSettingsMaintenanceOptions.Value; + _logger = logger; + _customMaintenanceHandlers = customMaintenanceHandlers; + } + + public override Task ShouldExecuteAsync(MaintenanceTaskExecutionContext context) => + Task.FromResult(_options.IsEnabled + // && !context.WasLatestExecutionSuccessful() + ); + + public override async Task ExecuteAsync(MaintenanceTaskExecutionContext context) + { + context.ReloadShellAfterMaintenanceCompletion = true; + + _logger.LogInformation("Custom maintenance was started for {TenantName} tenant.", _shellSettings.Name); + + await _customMaintenanceHandlers.AwaitEachAsync(handler => handler.ExecuteAsync(context)); + + _logger.LogInformation("Custom maintenance was done for {TenantName} tenant.", _shellSettings.Name); + } +} diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/Startup.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/Startup.cs new file mode 100644 index 00000000..76331a41 --- /dev/null +++ b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/Startup.cs @@ -0,0 +1,26 @@ +using Lombiq.HelpfulLibraries.OrchardCore.Mvc; +using Lombiq.Hosting.Tenants.Maintenance.Constants; +using Lombiq.Hosting.Tenants.Maintenance.Services; +using Microsoft.Extensions.DependencyInjection; +using OrchardCore.Environment.Shell.Configuration; +using OrchardCore.Modules; + +namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.UpdateOpenIdSettings; + +[Feature(FeatureNames.CustomMaintenance)] +public sealed class Startup : StartupBase +{ + private readonly IShellConfiguration _shellConfiguration; + + public Startup(IShellConfiguration shellConfiguration) => + _shellConfiguration = shellConfiguration; + + public override void ConfigureServices(IServiceCollection services) + { + services.BindAndConfigureSection( + _shellConfiguration, + "Lombiq_Hosting_Tenants_Maintenance:CustomMaintenance"); + + services.AddScoped(); + } +} diff --git a/Lombiq.Hosting.Tenants.Maintenance/Services/ICustomMaintenanceHandler.cs b/Lombiq.Hosting.Tenants.Maintenance/Services/ICustomMaintenanceHandler.cs new file mode 100644 index 00000000..b1812f8c --- /dev/null +++ b/Lombiq.Hosting.Tenants.Maintenance/Services/ICustomMaintenanceHandler.cs @@ -0,0 +1,9 @@ +using Lombiq.Hosting.Tenants.Maintenance.Models; +using System.Threading.Tasks; + +namespace Lombiq.Hosting.Tenants.Maintenance.Services; + +public interface ICustomMaintenanceHandler +{ + Task ExecuteAsync(MaintenanceTaskExecutionContext context); +} From fa36734a7303a114bd77096acfaa3a5e59b947a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20N=C3=A9meth?= Date: Wed, 11 Dec 2024 21:16:46 +0100 Subject: [PATCH 2/7] DRYing --- .../UpdateFeatureListMaintenanceProvider.cs | 11 ++++++----- .../UpdateOpenIdSettings/CustomMaintenanceProvider.cs | 4 +--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/UpdateFeatureListMaintenanceProvider.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/UpdateFeatureListMaintenanceProvider.cs index 1bdfdb4a..7e8f1725 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/UpdateFeatureListMaintenanceProvider.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/UpdateFeatureListMaintenanceProvider.cs @@ -29,15 +29,13 @@ public UpdateEnabledFeaturesMaintenanceProvider( } public override Task ShouldExecuteAsync(MaintenanceTaskExecutionContext context) => - Task.FromResult(_options.IsEnabled - && !context.WasLatestExecutionSuccessful() - ); + Task.FromResult(_options.IsEnabled && !context.WasLatestExecutionSuccessful()); public override async Task ExecuteAsync(MaintenanceTaskExecutionContext context) { var availableFeatures = (await _shellFeaturesManager.GetAvailableFeaturesAsync()).ToList(); - if (_options.EnableFeatures?.Replace(" ", string.Empty).SplitByCommas() is { } enableFeaturesOption) + if (ReplaceAndSplitByCommas(_options.EnableFeatures) is { } enableFeaturesOption) { var enableFeatures = availableFeatures.Where(feature => enableFeaturesOption.Contains(feature.Id)).ToList(); @@ -45,7 +43,7 @@ public override async Task ExecuteAsync(MaintenanceTaskExecutionContext context) await NotifyAsync(enableFeatures); } - if (_options.DisableFeatures?.Replace(" ", string.Empty).SplitByCommas() is { } disableFeaturesOption) + if (ReplaceAndSplitByCommas(_options.DisableFeatures) is { } disableFeaturesOption) { var disableFeatures = availableFeatures.Where(feature => disableFeaturesOption.Contains(feature.Id)).ToList(); @@ -63,4 +61,7 @@ private ValueTask NotifyAsync(IEnumerable features, bool enabled = return ValueTask.CompletedTask; } + + private static string[] ReplaceAndSplitByCommas(string input) => + input?.Replace(" ", string.Empty).SplitByCommas(); } diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs index e3759226..e8723f07 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs @@ -38,9 +38,7 @@ public CustomMaintenanceProvider( } public override Task ShouldExecuteAsync(MaintenanceTaskExecutionContext context) => - Task.FromResult(_options.IsEnabled - // && !context.WasLatestExecutionSuccessful() - ); + Task.FromResult(_options.IsEnabled && !context.WasLatestExecutionSuccessful()); public override async Task ExecuteAsync(MaintenanceTaskExecutionContext context) { From 030872481e4e2474266b69e03877a1e697cb798d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20N=C3=A9meth?= Date: Thu, 12 Dec 2024 13:57:09 +0100 Subject: [PATCH 3/7] Code analyzer violation fixed --- .../UpdateOpenIdSettings/CustomMaintenanceProvider.cs | 9 --------- .../Services/ICustomMaintenanceHandler.cs | 6 ++++++ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs index e8723f07..53abf078 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs @@ -1,18 +1,9 @@ using Lombiq.Hosting.Tenants.Maintenance.Extensions; using Lombiq.Hosting.Tenants.Maintenance.Models; using Lombiq.Hosting.Tenants.Maintenance.Services; -using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using OfficeFreund.OpenId.Constants; -using OfficeFreund.OpenId.Services; -using OfficeFreund.OpenId.Settings; -using OrchardCore.ContentManagement.Utilities; using OrchardCore.Environment.Shell; -using OrchardCore.Modules; -using OrchardCore.OpenId.Configuration; -using OrchardCore.OpenId.Services; -using System; using System.Collections.Generic; using System.Threading.Tasks; diff --git a/Lombiq.Hosting.Tenants.Maintenance/Services/ICustomMaintenanceHandler.cs b/Lombiq.Hosting.Tenants.Maintenance/Services/ICustomMaintenanceHandler.cs index b1812f8c..ea69d095 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Services/ICustomMaintenanceHandler.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Services/ICustomMaintenanceHandler.cs @@ -3,7 +3,13 @@ namespace Lombiq.Hosting.Tenants.Maintenance.Services; +/// +/// Implementations of this interface can be used to execute custom maintenance tasks. +/// public interface ICustomMaintenanceHandler { + /// + /// Executes the maintenance logic. + /// Task ExecuteAsync(MaintenanceTaskExecutionContext context); } From cf206e12a03c4d817c6271604283fea33672bf1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20N=C3=A9meth?= Date: Thu, 12 Dec 2024 17:33:52 +0100 Subject: [PATCH 4/7] Delete not needed --- .../CustomMaintenanceOptions.cs | 6 --- .../CustomMaintenanceProvider.cs | 44 ------------------- .../UpdateOpenIdSettings/Startup.cs | 26 ----------- 3 files changed, 76 deletions(-) delete mode 100644 Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceOptions.cs delete mode 100644 Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs delete mode 100644 Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/Startup.cs diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceOptions.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceOptions.cs deleted file mode 100644 index 89d7d0bc..00000000 --- a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceOptions.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.UpdateOpenIdSettings; - -public class CustomMaintenanceOptions -{ - public bool IsEnabled { get; set; } -} diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs deleted file mode 100644 index 53abf078..00000000 --- a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/CustomMaintenanceProvider.cs +++ /dev/null @@ -1,44 +0,0 @@ -using Lombiq.Hosting.Tenants.Maintenance.Extensions; -using Lombiq.Hosting.Tenants.Maintenance.Models; -using Lombiq.Hosting.Tenants.Maintenance.Services; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using OrchardCore.Environment.Shell; -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.UpdateOpenIdSettings; - -public class CustomMaintenanceProvider : MaintenanceProviderBase -{ - private readonly ShellSettings _shellSettings; - private readonly ILogger _logger; - private readonly CustomMaintenanceOptions _options; - private readonly IEnumerable _customMaintenanceHandlers; - - public CustomMaintenanceProvider( - ShellSettings shellSettings, - IOptions updateOpenIdSettingsMaintenanceOptions, - ILogger logger, - IEnumerable customMaintenanceHandlers) - { - _shellSettings = shellSettings; - _options = updateOpenIdSettingsMaintenanceOptions.Value; - _logger = logger; - _customMaintenanceHandlers = customMaintenanceHandlers; - } - - public override Task ShouldExecuteAsync(MaintenanceTaskExecutionContext context) => - Task.FromResult(_options.IsEnabled && !context.WasLatestExecutionSuccessful()); - - public override async Task ExecuteAsync(MaintenanceTaskExecutionContext context) - { - context.ReloadShellAfterMaintenanceCompletion = true; - - _logger.LogInformation("Custom maintenance was started for {TenantName} tenant.", _shellSettings.Name); - - await _customMaintenanceHandlers.AwaitEachAsync(handler => handler.ExecuteAsync(context)); - - _logger.LogInformation("Custom maintenance was done for {TenantName} tenant.", _shellSettings.Name); - } -} diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/Startup.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/Startup.cs deleted file mode 100644 index 76331a41..00000000 --- a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateOpenIdSettings/Startup.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Lombiq.HelpfulLibraries.OrchardCore.Mvc; -using Lombiq.Hosting.Tenants.Maintenance.Constants; -using Lombiq.Hosting.Tenants.Maintenance.Services; -using Microsoft.Extensions.DependencyInjection; -using OrchardCore.Environment.Shell.Configuration; -using OrchardCore.Modules; - -namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.UpdateOpenIdSettings; - -[Feature(FeatureNames.CustomMaintenance)] -public sealed class Startup : StartupBase -{ - private readonly IShellConfiguration _shellConfiguration; - - public Startup(IShellConfiguration shellConfiguration) => - _shellConfiguration = shellConfiguration; - - public override void ConfigureServices(IServiceCollection services) - { - services.BindAndConfigureSection( - _shellConfiguration, - "Lombiq_Hosting_Tenants_Maintenance:CustomMaintenance"); - - services.AddScoped(); - } -} From 8de61ce24836bb82f1b19f0cd88a145ef807948f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20N=C3=A9meth?= Date: Thu, 12 Dec 2024 17:34:04 +0100 Subject: [PATCH 5/7] Adding to features and adding documentation --- .../Manifest.cs | 8 ++++++++ Lombiq.Hosting.Tenants.Maintenance/Readme.md | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/Lombiq.Hosting.Tenants.Maintenance/Manifest.cs b/Lombiq.Hosting.Tenants.Maintenance/Manifest.cs index 89c1abfd..1044e4c5 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Manifest.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Manifest.cs @@ -76,3 +76,11 @@ Category = "Maintenance", Dependencies = [] )] + +[assembly: Feature( + Id = UpdateEnabledFeatures, + Name = "Lombiq Hosting - Tenants Maintenance Update Enabled Features", + Description = "Updates the enabled features of tenants.", + Category = "Maintenance", + Dependencies = [] +)] diff --git a/Lombiq.Hosting.Tenants.Maintenance/Readme.md b/Lombiq.Hosting.Tenants.Maintenance/Readme.md index 781b880f..1c27329b 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Readme.md +++ b/Lombiq.Hosting.Tenants.Maintenance/Readme.md @@ -176,3 +176,23 @@ The following configuration should be used to allow the maintenance to run and f } } ``` + +### `Lombiq.Hosting.Tenants.Maintenance.UpdateEnabledFeatures` + +It's a maintenance task that updates the enabled features of a tenant based on the app configuration. It is available on any tenant. + +The following configuration options are available to set the enabled features: + +```json +{ + "OrchardCore": { + "Lombiq_Hosting_Tenants_Maintenance": { + "UpdateEnabledFeatures": { + "IsEnabled": true, + "EnableFeatures": "OrchardCore.Admin, OrchardCore.Alias, OrchardCore.AuditTrail", + "DisableFeatures": "OrchardCore.BackgroundTasks, OrchardCore.ContentFields" + } + } + } +} +``` From 8297f99317cb5501e7c13dca0192eb8c8d3cfc60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20N=C3=A9meth?= Date: Thu, 12 Dec 2024 17:43:21 +0100 Subject: [PATCH 6/7] Obviously not needed --- .../Lombiq.Hosting.Tenants.Maintenance.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Lombiq.Hosting.Tenants.Maintenance/Lombiq.Hosting.Tenants.Maintenance.csproj b/Lombiq.Hosting.Tenants.Maintenance/Lombiq.Hosting.Tenants.Maintenance.csproj index 2c9cf0fd..ab3be356 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Lombiq.Hosting.Tenants.Maintenance.csproj +++ b/Lombiq.Hosting.Tenants.Maintenance/Lombiq.Hosting.Tenants.Maintenance.csproj @@ -57,8 +57,4 @@ - - - - From a94c4bfee2a16d06666e4ee5f93ad86a677c1018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20N=C3=A9meth?= Date: Thu, 12 Dec 2024 21:45:20 +0100 Subject: [PATCH 7/7] Deleting not needed interface --- .../Services/ICustomMaintenanceHandler.cs | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 Lombiq.Hosting.Tenants.Maintenance/Services/ICustomMaintenanceHandler.cs diff --git a/Lombiq.Hosting.Tenants.Maintenance/Services/ICustomMaintenanceHandler.cs b/Lombiq.Hosting.Tenants.Maintenance/Services/ICustomMaintenanceHandler.cs deleted file mode 100644 index ea69d095..00000000 --- a/Lombiq.Hosting.Tenants.Maintenance/Services/ICustomMaintenanceHandler.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Lombiq.Hosting.Tenants.Maintenance.Models; -using System.Threading.Tasks; - -namespace Lombiq.Hosting.Tenants.Maintenance.Services; - -/// -/// Implementations of this interface can be used to execute custom maintenance tasks. -/// -public interface ICustomMaintenanceHandler -{ - /// - /// Executes the maintenance logic. - /// - Task ExecuteAsync(MaintenanceTaskExecutionContext context); -}