Skip to content

Commit

Permalink
Align bootstrappers and move things into commands (#3992)
Browse files Browse the repository at this point in the history
* Align bootstrappers and move things into commands

* Namespace cleanup
  • Loading branch information
danielmarbach committed Mar 12, 2024
1 parent f5db181 commit 7c2e3ab
Show file tree
Hide file tree
Showing 25 changed files with 277 additions and 362 deletions.
10 changes: 7 additions & 3 deletions src/ServiceControl.AcceptanceTests.RavenDB/StartupModeTests.cs
Expand Up @@ -3,10 +3,12 @@
using System;
using System.Threading.Tasks;
using Hosting.Commands;
using Microsoft.Extensions.Hosting;
using NServiceBus;
using NUnit.Framework;
using Particular.ServiceControl;
using Particular.ServiceControl.Hosting;
using Persistence;
using Persistence.RavenDB;
using ServiceBus.Management.Infrastructure.Settings;
using ServiceControl.AcceptanceTesting.InfrastructureConfig;
Expand Down Expand Up @@ -41,10 +43,12 @@ public async Task InitializeSettings()
[Test]
public async Task CanRunMaintenanceMode()
{
var bootstrapper = new MaintenanceBootstrapper(settings);

var host = bootstrapper.HostBuilder.Build();
// ideally we'd be using the MaintenanceModeCommand here but that indefinitely blocks due to the RunAsync
// not terminating.
var hostBuilder = Host.CreateApplicationBuilder();
hostBuilder.Services.AddPersistence(settings, maintenanceMode: true);

using var host = hostBuilder.Build();
await host.StartAsync();
await host.StopAsync();
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Threading.Tasks;
using AcceptanceTesting.EndpointTemplates;
using Hosting.Commands;
using NServiceBus;
using NServiceBus.AcceptanceTesting.Support;
using NServiceBus.Features;
Expand All @@ -12,7 +13,7 @@ public class DefaultServer : IEndpointSetupTemplate
{
// TODO: Revisit the default server base having a bootstrapper reference
public Task<EndpointConfiguration> GetConfiguration(RunDescriptor runDescriptor, EndpointCustomizationConfiguration endpointConfiguration, Func<EndpointConfiguration, Task> configurationBuilderCustomization) =>
new DefaultServerBase<SetupBootstrapper>().GetConfiguration(runDescriptor, endpointConfiguration, async b =>
new DefaultServerBase<SetupCommand>().GetConfiguration(runDescriptor, endpointConfiguration, async b =>
{
b.DisableFeature<Audit>();
Expand Down
Expand Up @@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using AcceptanceTesting;
using Hosting.Commands;
using Infrastructure.DomainEvents;
using Infrastructure.WebApi;
using Microsoft.AspNetCore.Builder;
Expand All @@ -20,6 +21,7 @@
using NServiceBus.AcceptanceTesting.Support;
using NServiceBus.Configuration.AdvancedExtensibility;
using Particular.ServiceControl;
using Particular.ServiceControl.Hosting;
using RavenDB.Shared;
using ServiceBus.Management.Infrastructure.Settings;

Expand Down Expand Up @@ -96,8 +98,8 @@ async Task InitializeServiceControl(ScenarioContext context)

using (new DiagnosticTimer($"Creating infrastructure for {instanceName}"))
{
var setupBootstrapper = new SetupBootstrapper(settings);
await setupBootstrapper.Run();
var setupCommand = new SetupCommand();
await setupCommand.Execute(new HostArguments(Array.Empty<string>()), settings);
}

var configuration = new EndpointConfiguration(instanceName);
Expand Down
Expand Up @@ -11,6 +11,8 @@ namespace ServiceControl.Audit.AcceptanceTests.TestSupport
using AcceptanceTesting;
using Auditing;
using Infrastructure;
using Infrastructure.Hosting;
using Infrastructure.Hosting.Commands;
using Infrastructure.Settings;
using Infrastructure.WebApi;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -102,9 +104,8 @@ async Task InitializeServiceControl(ScenarioContext context)

using (new DiagnosticTimer($"Creating infrastructure for {instanceName}"))
{

var setupBootstrapper = new SetupBootstrapper(settings);
await setupBootstrapper.Run();
var setupCommand = new SetupCommand();
await setupCommand.Execute(new HostArguments(Array.Empty<string>()), settings);
}

var configuration = new EndpointConfiguration(instanceName);
Expand Down
Expand Up @@ -5,6 +5,8 @@
using System.Threading;
using System.Threading.Tasks;
using Audit.Infrastructure;
using Audit.Infrastructure.Hosting;
using Audit.Infrastructure.Hosting.Commands;
using Audit.Infrastructure.Settings;
using Microsoft.Extensions.DependencyInjection;
using NServiceBus;
Expand All @@ -25,9 +27,8 @@ public async Task Should_provision_queues()
ForwardAuditMessages = true,
};

var setupBootstrapper = new SetupBootstrapper(settings);

await setupBootstrapper.Run();
var setupCommand = new SetupCommand();
await setupCommand.Execute(new HostArguments(Array.Empty<string>()), settings);

CollectionAssert.AreEquivalent(new[]
{
Expand Down
Expand Up @@ -4,13 +4,8 @@
using System.Collections.Generic;
using System.Threading.Tasks;

class CommandRunner
class CommandRunner(List<Type> commands)
{
public CommandRunner(List<Type> commands)
{
this.commands = commands;
}

public async Task Execute(HostArguments args, Settings.Settings settings)
{
foreach (var commandType in commands)
Expand All @@ -19,7 +14,5 @@ public async Task Execute(HostArguments args, Settings.Settings settings)
await command.Execute(args, settings);
}
}

readonly List<Type> commands;
}
}

This file was deleted.

@@ -1,14 +1,14 @@
namespace ServiceControl.Audit.Infrastructure
namespace ServiceControl.Audit.Infrastructure.Hosting.Commands
{
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.WindowsServices;
using ServiceControl.Audit.Persistence;
using Persistence;

static class MaintenanceBootstrapper
class MaintenanceModeCommand : AbstractCommand
{
public static async Task Run(Settings.Settings settings)
public override async Task Execute(HostArguments args, Settings.Settings settings)
{
var persistenceConfiguration = PersistenceConfigurationFactory.LoadPersistenceConfiguration(settings.PersistenceType);
var persistenceSettings = persistenceConfiguration.BuildPersistenceSettings(settings);
Expand Down
@@ -1,15 +1,97 @@
namespace ServiceControl.Audit.Infrastructure.Hosting.Commands
{
using System.Collections.Generic;
using System.Threading.Tasks;
using LicenseManagement;
using NServiceBus.Logging;
using Persistence;
using Settings;
using Transports;

class SetupCommand : AbstractCommand
{
public override async Task Execute(HostArguments args, Settings settings)
{
settings.SkipQueueCreation = args.SkipQueueCreation;

await new SetupBootstrapper(settings).Run();
// Validate license:
if (!ValidateLicense(settings))
{
return;
}

var transportSettings = MapSettings(settings);
var transportCustomization = settings.LoadTransportCustomization();

if (settings.IngestAuditMessages)
{
if (settings.SkipQueueCreation)
{
Logger.Info("Skipping queue creation");
}
else
{
var additionalQueues = new List<string> { settings.AuditQueue };

if (settings.ForwardAuditMessages && settings.AuditLogQueue != null)
{
additionalQueues.Add(settings.AuditLogQueue);
}

await transportCustomization.ProvisionQueues(transportSettings, additionalQueues);
}
}

EventSource.Create();

var persistenceConfiguration = PersistenceConfigurationFactory.LoadPersistenceConfiguration(settings.PersistenceType);
var persistenceSettings = persistenceConfiguration.BuildPersistenceSettings(settings);
var persistence = persistenceConfiguration.Create(persistenceSettings);
var installer = persistence.CreateInstaller();

await installer.Install();
}

bool ValidateLicense(Settings settings)
{
if (!string.IsNullOrWhiteSpace(settings.LicenseFileText))
{
if (!LicenseManager.IsLicenseValidForServiceControlInit(settings.LicenseFileText, out var errorMessageForLicenseText))
{
Logger.Error(errorMessageForLicenseText);
return false;
}

if (!LicenseManager.TryImportLicenseFromText(settings.LicenseFileText, out var importErrorMessage))
{
Logger.Error(importErrorMessage);
return false;
}
}
else
{
var license = LicenseManager.FindLicense();
if (!LicenseManager.IsLicenseValidForServiceControlInit(license, out var errorMessageForFoundLicense))
{
Logger.Error(errorMessageForFoundLicense);
return false;
}
}

return true;
}

static TransportSettings MapSettings(Settings settings)
{
var transportSettings = new TransportSettings
{
EndpointName = settings.ServiceName,
ConnectionString = settings.TransportConnectionString,
MaxConcurrency = settings.MaximumConcurrencyLevel
};
return transportSettings;
}

static readonly ILog Logger = LogManager.GetLogger<SetupCommand>();
}
}
Expand Up @@ -41,7 +41,7 @@ public HostArguments(string[] args)
{
Commands =
[
typeof(MaintCommand)
typeof(MaintenanceModeCommand)
];
executionMode = ExecutionMode.Maintenance;
}
Expand Down
94 changes: 0 additions & 94 deletions src/ServiceControl.Audit/Infrastructure/SetupBootstrapper.cs

This file was deleted.

Expand Up @@ -13,7 +13,7 @@ public class DefaultServer : IEndpointSetupTemplate
{
// TODO: Revisit the default server base having a bootstrapper reference
public Task<EndpointConfiguration> GetConfiguration(RunDescriptor runDescriptor, EndpointCustomizationConfiguration endpointConfiguration, Func<EndpointConfiguration, Task> configurationBuilderCustomization) =>
new DefaultServerBase<SetupBootstrapper>(new ConfigureEndpointLearningTransport()).GetConfiguration(runDescriptor, endpointConfiguration, async b =>
new DefaultServerBase<SetupCommand>(new ConfigureEndpointLearningTransport()).GetConfiguration(runDescriptor, endpointConfiguration, async b =>
{
b.DisableFeature<Audit>();
Expand Down
Expand Up @@ -79,8 +79,8 @@ async Task InitializeServiceControl(ScenarioContext context)

using (new DiagnosticTimer($"Creating infrastructure for {instanceName}"))
{
var setupBootstrapper = new SetupBootstrapper(settings);
await setupBootstrapper.Run();
var setupCommand = new SetupCommand();
await setupCommand.Execute(settings);
}

var configuration = new EndpointConfiguration(instanceName);
Expand Down

0 comments on commit 7c2e3ab

Please sign in to comment.