cleanup of tools creation, adds automatic log prefixing.

This commit is contained in:
benbierens 2023-09-13 10:23:05 +02:00
parent 3c724f6206
commit a05a82c030
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
9 changed files with 108 additions and 52 deletions

View File

@ -3,7 +3,7 @@ using KubernetesWorkflow;
namespace CodexPlugin
{
public class CodexPlugin : IProjectPlugin
public class CodexPlugin : IProjectPlugin, IHasLogPrefix
{
private readonly CodexStarter codexStarter;
private readonly IPluginTools tools;
@ -14,7 +14,7 @@ namespace CodexPlugin
this.tools = tools;
}
#region IProjectPlugin Implementation
public string LogPrefix => "(Codex) ";
public void Announce()
{
@ -25,8 +25,6 @@ namespace CodexPlugin
{
}
#endregion
public RunningContainers[] StartCodexNodes(int numberOfNodes, Action<ICodexSetup> setup)
{
var codexSetup = new CodexSetup(numberOfNodes, CodexLogLevel.Trace);

View File

@ -7,12 +7,10 @@ namespace CodexPlugin
public class CodexStarter
{
private readonly IPluginTools pluginTools;
private readonly ILog log;
public CodexStarter(IPluginTools pluginTools)
{
this.pluginTools = pluginTools;
log = new LogPrefixer(pluginTools.GetLog(), "(CodexStarter) ");
}
public RunningContainers[] BringOnline(CodexSetup codexSetup)
@ -120,7 +118,7 @@ namespace CodexPlugin
try
{
Stopwatch.Measure(log, "EnsureOnline", group.EnsureOnline);
Stopwatch.Measure(pluginTools.GetLog(), "EnsureOnline", group.EnsureOnline);
}
catch
{
@ -150,7 +148,7 @@ namespace CodexPlugin
private void Log(string message)
{
log.Log(message);
pluginTools.GetLog().Log(message);
}
//private void StopCrashWatcher(RunningContainers containers)

View File

@ -5,12 +5,15 @@ namespace Core
{
public class EntryPoint
{
private readonly IToolsFactory toolsFactory;
private readonly PluginManager manager = new PluginManager();
public EntryPoint(ILog log, Configuration configuration, string fileManagerRootFolder, ITimeSet timeSet)
{
Tools = new ToolsProvider(log, configuration, fileManagerRootFolder, timeSet);
manager.InstantiatePlugins(PluginFinder.GetPluginTypes(), Tools);
toolsFactory = new ToolsFactory(log, configuration, fileManagerRootFolder, timeSet);
Tools = toolsFactory.CreateTools();
manager.InstantiatePlugins(PluginFinder.GetPluginTypes(), toolsFactory);
}
public EntryPoint(ILog log, Configuration configuration, string fileManagerRootFolder)

View File

@ -1,21 +1,18 @@
using FileUtils;
using KubernetesWorkflow;
using Logging;
using Utils;
namespace Core
namespace Core
{
public class PluginManager
{
private readonly List<IProjectPlugin> projectPlugins = new List<IProjectPlugin>();
public void InstantiatePlugins(Type[] pluginTypes, IPluginTools tools)
internal void InstantiatePlugins(Type[] pluginTypes, IToolsFactory provider)
{
projectPlugins.Clear();
foreach (var pluginType in pluginTypes)
{
var plugin = (IProjectPlugin)Activator.CreateInstance(pluginType, args: tools)!;
projectPlugins.Add(plugin);
var tools = provider.CreateTools();
var plugin = InstantiatePlugins(pluginType, tools);
ApplyLogPrefix(plugin, tools);
}
}
@ -33,6 +30,21 @@ namespace Core
{
return (T)projectPlugins.Single(p => p.GetType() == typeof(T));
}
private IProjectPlugin InstantiatePlugins(Type pluginType, PluginTools tools)
{
var plugin = (IProjectPlugin)Activator.CreateInstance(pluginType, args: tools)!;
projectPlugins.Add(plugin);
return plugin;
}
private void ApplyLogPrefix(IProjectPlugin plugin, PluginTools tools)
{
if (plugin is IHasLogPrefix hasLogPrefix)
{
tools.ApplyLogPrefix(hasLogPrefix.LogPrefix);
}
}
}
public interface IProjectPlugin
@ -41,28 +53,8 @@ namespace Core
void Decommission();
}
public interface IPluginTools : IWorkflowTool, ILogTool, IHttpFactoryTool, IFileTool
public interface IHasLogPrefix
{
}
public interface IWorkflowTool
{
IStartupWorkflow CreateWorkflow(string? namespaceOverride = null);
}
public interface ILogTool
{
ILog GetLog();
}
public interface IHttpFactoryTool
{
Http CreateHttp(Address address, string baseUrl, Action<HttpClient> onClientCreated, string? logAlias = null);
Http CreateHttp(Address address, string baseUrl, string? logAlias = null);
}
public interface IFileTool
{
IFileManager GetFileManager();
string LogPrefix { get; }
}
}

View File

@ -5,14 +5,39 @@ using Utils;
namespace Core
{
public class ToolsProvider : IPluginTools
public interface IPluginTools : IWorkflowTool, ILogTool, IHttpFactoryTool, IFileTool
{
}
public interface IWorkflowTool
{
IStartupWorkflow CreateWorkflow(string? namespaceOverride = null);
}
public interface ILogTool
{
ILog GetLog();
}
public interface IHttpFactoryTool
{
Http CreateHttp(Address address, string baseUrl, Action<HttpClient> onClientCreated, string? logAlias = null);
Http CreateHttp(Address address, string baseUrl, string? logAlias = null);
}
public interface IFileTool
{
IFileManager GetFileManager();
}
internal class PluginTools : IPluginTools
{
private readonly ILog log;
private readonly ITimeSet timeSet;
private readonly WorkflowCreator workflowCreator;
private readonly IFileManager fileManager;
private ILog log;
public ToolsProvider(ILog log, Configuration configuration, string fileManagerRootFolder, ITimeSet timeSet)
public PluginTools(ILog log, Configuration configuration, string fileManagerRootFolder, ITimeSet timeSet)
{
this.log = log;
this.timeSet = timeSet;
@ -20,6 +45,11 @@ namespace Core
workflowCreator = new WorkflowCreator(log, configuration);
}
public void ApplyLogPrefix(string prefix)
{
log = new LogPrefixer(log, prefix);
}
public Http CreateHttp(Address address, string baseUrl, Action<HttpClient> onClientCreated, string? logAlias = null)
{
return new Http(log, timeSet, address, baseUrl, onClientCreated, logAlias);

31
Core/ToolsFactory.cs Normal file
View File

@ -0,0 +1,31 @@
using KubernetesWorkflow;
using Logging;
namespace Core
{
internal interface IToolsFactory
{
PluginTools CreateTools();
}
internal class ToolsFactory : IToolsFactory
{
private readonly ILog log;
private readonly Configuration configuration;
private readonly string fileManagerRootFolder;
private readonly ITimeSet timeSet;
public ToolsFactory(ILog log, Configuration configuration, string fileManagerRootFolder, ITimeSet timeSet)
{
this.log = log;
this.configuration = configuration;
this.fileManagerRootFolder = fileManagerRootFolder;
this.timeSet = timeSet;
}
public PluginTools CreateTools()
{
return new PluginTools(log, configuration, fileManagerRootFolder, timeSet);
}
}
}

View File

@ -7,7 +7,7 @@ namespace MetricsPlugin
{
public static RunningContainer StartMetricsCollector(this CoreInterface ci, RunningContainers[] scrapeTargets)
{
return Plugin(ci).StartMetricsCollector(scrapeTargets);
return null!;// Plugin(ci).StartMetricsCollector(scrapeTargets);
}
private static MetricsPlugin Plugin(CoreInterface ci)

View File

@ -5,8 +5,15 @@ namespace MetricsPlugin
{
public class MetricsPlugin : IProjectPlugin
{
private readonly IPluginTools tools;
private readonly PrometheusStarter starter;
public MetricsPlugin(IPluginTools tools)
{
this.tools = tools;
starter = new PrometheusStarter(tools);
}
#region IProjectPlugin Implementation
public void Announce()
{
@ -17,11 +24,9 @@ namespace MetricsPlugin
{
}
#endregion
public RunningContainer StartMetricsCollector(RunningContainers[] scrapeTargets)
public RunningContainers StartMetricsCollector(RunningContainers[] scrapeTargets)
{
return null!;
return starter.CollectMetricsFor(scrapeTargets);
}
}
}

View File

@ -1,6 +1,5 @@
using Core;
using KubernetesWorkflow;
using System.Text;
namespace MetricsPlugin
{