Files
slp-modular-cms/src/SlpModularCms.Core/Hosting/ModuleOrchestrator.cs
T
SluijsensandClaude Sonnet 5 0447993181 Completes local-dev-master-slave-setup: dual-instance frontend tooling, module-capability gating, and master/slave protocol self-healing fixes
Frontend (Unit 2 completion): dual dev-server tooling (pnpm dev:slave,
pnpm dev:all), per-instance browser tab titles, and a backend
capability check (SystemController + useSystemCapabilities +
ModuleGuard) so a Master-only page is hidden on a slave instance
instead of assuming every backend has every module.

Master/slave protocol fixes surfaced by actually running master and
slave side by side locally:
- Deactivating a CMS instance (Inactive) now releases the slave's
  master gate instead of leaving it stuck on its last pushed status.
- The periodic integrity check now also re-pushes status to every
  reachable slave (previously URL-verification only) and runs once
  immediately on startup.
- Added the originally-specified (but never implemented) slave-pull
  path: a slave now periodically polls its own status from the master
  (GET /api/v1/SlaveStatus) and fails open to Available if the master
  is unreachable for too long, complementing the existing push.
- The slave's own Settings page can no longer "successfully" change
  local availability while the master controls it; it's now locked
  with an explanatory banner and the backend rejects the write with
  409 instead of silently no-op'ing it.
- CMS instance status badges now match the dashboard's color/icon
  styling instead of a plain grey badge.

Also corrected the master-cms-module design docs to match this
as-built behavior, and flagged (without a full rewrite) a larger,
pre-existing divergence between its inception-stage application
design and what construction actually built.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-04 19:53:52 +02:00

114 lines
3.8 KiB
C#

using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SlpModularCms.Core.Modules;
namespace SlpModularCms.Core.Hosting;
/// <summary>
/// Orkestrator die modules ontdekt en hun lifecycle beheert.
/// </summary>
public class ModuleOrchestrator
{
private readonly List<IModule> _modules = new();
private readonly ILogger<ModuleOrchestrator> _logger;
public ModuleOrchestrator(ILogger<ModuleOrchestrator> logger)
{
_logger = logger;
}
/// <summary>
/// Names of the modules discovered on this instance (e.g. so the frontend can tell
/// a master-only feature apart from a slave instance without that module loaded).
/// </summary>
public IReadOnlyList<string> ModuleNames => _modules.Select(m => m.Name).ToArray();
public void DiscoverModules()
{
_logger.LogInformation("Start module discovery...");
// Forceer het laden van module assemblies van disk
var path = AppDomain.CurrentDomain.BaseDirectory;
var moduleFiles = Directory.GetFiles(path, "SlpModularCms.Modules.*.dll");
foreach (var file in moduleFiles)
{
try
{
var assemblyName = AssemblyName.GetAssemblyName(file);
if (AppDomain.CurrentDomain.GetAssemblies().All(a => a.FullName != assemblyName.FullName))
{
Assembly.Load(assemblyName);
_logger.LogDebug("Assembly geladen: {AssemblyName}", assemblyName.Name);
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Kon assembly niet laden van disk: {FilePath}", file);
}
}
var assemblies = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => a.FullName != null && a.FullName.StartsWith("SlpModularCms.Modules"))
.ToList();
foreach (var assembly in assemblies)
{
var moduleTypes = assembly.GetTypes()
.Where(t => typeof(IModule).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);
foreach (var type in moduleTypes)
{
try
{
if (Activator.CreateInstance(type) is IModule module)
{
_modules.Add(module);
_logger.LogInformation("Module ontdekt: {ModuleName} v{Version}", module.Name, module.Version);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Fout bij het instantiëren van module type {TypeName}", type.FullName);
}
}
}
_logger.LogInformation("{Count} modules succesvol geladen.", _modules.Count);
}
public void RegisterModuleServices(IServiceCollection services)
{
foreach (var module in _modules)
{
try
{
module.RegisterServices(services);
_logger.LogInformation("Services geregistreerd voor module: {ModuleName}", module.Name);
}
catch (Exception ex)
{
_logger.LogError(ex, "Fout bij het registreren van services voor module {ModuleName}", module.Name);
}
}
}
public void UseModules(IApplicationBuilder app)
{
foreach (var module in _modules)
{
try
{
module.UseModule(app);
_logger.LogInformation("Module geactiveerd in pipeline: {ModuleName}", module.Name);
}
catch (Exception ex)
{
_logger.LogError(ex, "Fout bij het activeren van module {ModuleName} in de pipeline", module.Name);
}
}
}
}