-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from Lombiq/issue/OFFI-164
OFFI-164: Adding maintenance
- Loading branch information
Showing
7 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
26 changes: 26 additions & 0 deletions
26
Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateEnabledFeatures/Startup.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<UpdateEnabledFeaturesMaintenanceOptions>( | ||
_shellConfiguration, | ||
"Lombiq_Hosting_Tenants_Maintenance:UpdateEnabledFeatures"); | ||
|
||
services.AddScoped<IMaintenanceProvider, UpdateEnabledFeaturesMaintenanceProvider>(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
....Maintenance/Maintenance/UpdateEnabledFeatures/UpdateEnabledFeaturesMaintenanceOptions.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} |
67 changes: 67 additions & 0 deletions
67
...nts.Maintenance/Maintenance/UpdateEnabledFeatures/UpdateFeatureListMaintenanceProvider.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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<UpdateEnabledFeaturesMaintenanceProvider> _logger; | ||
|
||
public UpdateEnabledFeaturesMaintenanceProvider( | ||
IOptions<UpdateEnabledFeaturesMaintenanceOptions> options, | ||
IShellFeaturesManager shellFeaturesManager, | ||
ILogger<UpdateEnabledFeaturesMaintenanceProvider> logger) | ||
{ | ||
_options = options.Value; | ||
_shellFeaturesManager = shellFeaturesManager; | ||
_logger = logger; | ||
} | ||
|
||
public override Task<bool> ShouldExecuteAsync(MaintenanceTaskExecutionContext context) => | ||
Task.FromResult(_options.IsEnabled && !context.WasLatestExecutionSuccessful()); | ||
|
||
public override async Task ExecuteAsync(MaintenanceTaskExecutionContext context) | ||
{ | ||
var availableFeatures = (await _shellFeaturesManager.GetAvailableFeaturesAsync()).ToList(); | ||
|
||
if (ReplaceAndSplitByCommas(_options.EnableFeatures) is { } enableFeaturesOption) | ||
{ | ||
var enableFeatures = availableFeatures.Where(feature => | ||
enableFeaturesOption.Contains(feature.Id)).ToList(); | ||
await _shellFeaturesManager.EnableFeaturesAsync(enableFeatures, force: true); | ||
await NotifyAsync(enableFeatures); | ||
} | ||
|
||
if (ReplaceAndSplitByCommas(_options.DisableFeatures) 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<IFeatureInfo> 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; | ||
} | ||
|
||
private static string[] ReplaceAndSplitByCommas(string input) => | ||
input?.Replace(" ", string.Empty).SplitByCommas(); | ||
} |
This file contains 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
This file contains 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