Improved logging in server components
This commit is contained in:
@@ -23,7 +23,7 @@ public class Server : IAsyncDisposable
|
||||
public IAsyncObservable<ServerState> OnState => OnStateSubject;
|
||||
|
||||
private readonly EventSubject<ServerState> OnStateSubject = new();
|
||||
private readonly ILogger<Server> Logger;
|
||||
private readonly ILogger Logger;
|
||||
private readonly RemoteService RemoteService;
|
||||
private readonly ServerConfigurationMapper Mapper;
|
||||
private readonly IHubContext<ServerWebSocketHub> HubContext;
|
||||
@@ -33,7 +33,7 @@ public class Server : IAsyncDisposable
|
||||
private IAsyncDisposable? ConsoleSubscription;
|
||||
|
||||
public Server(
|
||||
ILogger<Server> logger,
|
||||
ILoggerFactory loggerFactory,
|
||||
IConsole console,
|
||||
IFileSystem fileSystem,
|
||||
IInstaller installer,
|
||||
@@ -46,7 +46,7 @@ public class Server : IAsyncDisposable
|
||||
ServerConfigurationMapper mapper,
|
||||
IHubContext<ServerWebSocketHub> hubContext)
|
||||
{
|
||||
Logger = logger;
|
||||
Logger = loggerFactory.CreateLogger($"Servers.Instance.{context.Configuration.Id}.{nameof(Server)}");
|
||||
Console = console;
|
||||
FileSystem = fileSystem;
|
||||
Installer = installer;
|
||||
@@ -192,6 +192,20 @@ public class Server : IAsyncDisposable
|
||||
|
||||
public async Task Delete()
|
||||
{
|
||||
if (Installer.IsRunning)
|
||||
{
|
||||
Logger.LogDebug("Installer still running. Aborting and cleaning up");
|
||||
|
||||
await Installer.Abort();
|
||||
await Installer.Cleanup();
|
||||
}
|
||||
|
||||
if (Provisioner.IsProvisioned)
|
||||
await Provisioner.Deprovision();
|
||||
|
||||
if (FileSystem.IsMounted)
|
||||
await FileSystem.Unmount();
|
||||
|
||||
await FileSystem.Delete();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,19 +5,22 @@ namespace MoonlightServers.Daemon.ServerSys.Implementations;
|
||||
|
||||
public class DefaultRestorer : IRestorer
|
||||
{
|
||||
private readonly ILogger<DefaultRestorer> Logger;
|
||||
private readonly ILogger Logger;
|
||||
private readonly IConsole Console;
|
||||
private readonly IProvisioner Provisioner;
|
||||
private readonly IInstaller Installer;
|
||||
private readonly IStatistics Statistics;
|
||||
|
||||
public DefaultRestorer(
|
||||
ILogger<DefaultRestorer> logger,
|
||||
ILoggerFactory loggerFactory,
|
||||
ServerContext context,
|
||||
IConsole console,
|
||||
IProvisioner provisioner,
|
||||
IStatistics statistics, IInstaller installer)
|
||||
IStatistics statistics,
|
||||
IInstaller installer
|
||||
)
|
||||
{
|
||||
Logger = logger;
|
||||
Logger = loggerFactory.CreateLogger($"Servers.Instance.{context.Configuration.Id}.{nameof(DefaultRestorer)}");
|
||||
Console = console;
|
||||
Provisioner = provisioner;
|
||||
Statistics = statistics;
|
||||
@@ -41,7 +44,7 @@ public class DefaultRestorer : IRestorer
|
||||
await Console.CollectFromRuntime();
|
||||
await Console.AttachToRuntime();
|
||||
await Statistics.SubscribeToRuntime();
|
||||
|
||||
|
||||
return ServerState.Online;
|
||||
}
|
||||
|
||||
@@ -52,7 +55,7 @@ public class DefaultRestorer : IRestorer
|
||||
await Console.CollectFromInstallation();
|
||||
await Console.AttachToInstallation();
|
||||
await Statistics.SubscribeToInstallation();
|
||||
|
||||
|
||||
return ServerState.Installing;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,22 +17,24 @@ public class DockerConsole : IConsole
|
||||
|
||||
private readonly ConcurrentList<string> OutputCache = new();
|
||||
private readonly DockerClient DockerClient;
|
||||
private readonly ILogger<DockerConsole> Logger;
|
||||
private readonly ILogger Logger;
|
||||
private readonly ServerContext Context;
|
||||
|
||||
private MultiplexedStream? CurrentStream;
|
||||
private CancellationTokenSource Cts = new();
|
||||
|
||||
private const string MlPrefix = "\x1b[1;38;2;200;90;200mM\x1b[1;38;2;204;110;230mo\x1b[1;38;2;170;130;245mo\x1b[1;38;2;140;150;255mn\x1b[1;38;2;110;180;255ml\x1b[1;38;2;100;200;255mi\x1b[1;38;2;100;220;255mg\x1b[1;38;2;120;235;255mh\x1b[1;38;2;140;250;255mt\x1b[0m \x1b[3;38;2;200;200;200m{0}\x1b[0m\n\r";
|
||||
private const string MlPrefix =
|
||||
"\x1b[1;38;2;200;90;200mM\x1b[1;38;2;204;110;230mo\x1b[1;38;2;170;130;245mo\x1b[1;38;2;140;150;255mn\x1b[1;38;2;110;180;255ml\x1b[1;38;2;100;200;255mi\x1b[1;38;2;100;220;255mg\x1b[1;38;2;120;235;255mh\x1b[1;38;2;140;250;255mt\x1b[0m \x1b[3;38;2;200;200;200m{0}\x1b[0m\n\r";
|
||||
|
||||
public DockerConsole(
|
||||
ServerContext context,
|
||||
DockerClient dockerClient,
|
||||
ILogger<DockerConsole> logger)
|
||||
ILoggerFactory loggerFactory
|
||||
)
|
||||
{
|
||||
Context = context;
|
||||
DockerClient = dockerClient;
|
||||
Logger = logger;
|
||||
Logger = loggerFactory.CreateLogger($"Servers.Instance.{context.Configuration.Id}.{nameof(DockerConsole)}");
|
||||
}
|
||||
|
||||
public Task Initialize()
|
||||
|
||||
@@ -19,7 +19,7 @@ public class DockerInstaller : IInstaller
|
||||
|
||||
private readonly EventSubject<Message> OnExitedSubject = new();
|
||||
|
||||
private readonly ILogger<DockerInstaller> Logger;
|
||||
private readonly ILogger Logger;
|
||||
private readonly DockerEventService EventService;
|
||||
private readonly IConsole Console;
|
||||
private readonly DockerClient DockerClient;
|
||||
@@ -37,7 +37,7 @@ public class DockerInstaller : IInstaller
|
||||
private IAsyncDisposable? ContainerEventSubscription;
|
||||
|
||||
public DockerInstaller(
|
||||
ILogger<DockerInstaller> logger,
|
||||
ILoggerFactory loggerFactory,
|
||||
DockerEventService eventService,
|
||||
IConsole console,
|
||||
DockerClient dockerClient,
|
||||
@@ -48,7 +48,7 @@ public class DockerInstaller : IInstaller
|
||||
ServerConfigurationMapper mapper
|
||||
)
|
||||
{
|
||||
Logger = logger;
|
||||
Logger = loggerFactory.CreateLogger($"Servers.Instance.{context.Configuration.Id}.{nameof(DockerInstaller)}");
|
||||
EventService = eventService;
|
||||
Console = console;
|
||||
DockerClient = dockerClient;
|
||||
|
||||
@@ -18,7 +18,7 @@ public class DockerProvisioner : IProvisioner
|
||||
public bool IsProvisioned { get; private set; }
|
||||
|
||||
private readonly DockerClient DockerClient;
|
||||
private readonly ILogger<DockerProvisioner> Logger;
|
||||
private readonly ILogger Logger;
|
||||
private readonly DockerEventService EventService;
|
||||
private readonly ServerContext Context;
|
||||
private readonly IConsole Console;
|
||||
@@ -34,7 +34,7 @@ public class DockerProvisioner : IProvisioner
|
||||
|
||||
public DockerProvisioner(
|
||||
DockerClient dockerClient,
|
||||
ILogger<DockerProvisioner> logger,
|
||||
ILoggerFactory loggerFactory,
|
||||
DockerEventService eventService,
|
||||
ServerContext context,
|
||||
IConsole console,
|
||||
@@ -44,7 +44,7 @@ public class DockerProvisioner : IProvisioner
|
||||
)
|
||||
{
|
||||
DockerClient = dockerClient;
|
||||
Logger = logger;
|
||||
Logger = loggerFactory.CreateLogger($"Servers.Instance.{context.Configuration.Id}.{nameof(DockerProvisioner)}");
|
||||
EventService = eventService;
|
||||
Context = context;
|
||||
Console = console;
|
||||
|
||||
@@ -9,28 +9,31 @@ public class RegexOnlineDetection : IOnlineDetection
|
||||
{
|
||||
private readonly ServerContext Context;
|
||||
private readonly IConsole Console;
|
||||
private readonly ILogger<RegexOnlineDetection> Logger;
|
||||
private readonly ILogger Logger;
|
||||
|
||||
private Regex? Regex;
|
||||
private IAsyncDisposable? ConsoleSubscription;
|
||||
private IAsyncDisposable? StateSubscription;
|
||||
|
||||
public RegexOnlineDetection(ServerContext context, IConsole console, ILogger<RegexOnlineDetection> logger)
|
||||
public RegexOnlineDetection(
|
||||
ServerContext context,
|
||||
IConsole console,
|
||||
ILoggerFactory loggerFactory)
|
||||
{
|
||||
Context = context;
|
||||
Console = console;
|
||||
Logger = logger;
|
||||
Logger = loggerFactory.CreateLogger($"Servers.Instance.{context.Configuration.Id}.{nameof(RegexOnlineDetection)}");
|
||||
}
|
||||
|
||||
public async Task Initialize()
|
||||
{
|
||||
Logger.LogInformation("Subscribing to state changes");
|
||||
Logger.LogDebug("Subscribing to state changes");
|
||||
|
||||
StateSubscription = await Context.Self.OnState.SubscribeAsync(async state =>
|
||||
{
|
||||
if (state == ServerState.Starting) // Subscribe to console when starting
|
||||
{
|
||||
Logger.LogInformation("Detected state change to online. Subscribing to console in order to check for the regex matches");
|
||||
Logger.LogDebug("Detected state change to online. Subscribing to console in order to check for the regex matches");
|
||||
|
||||
if(ConsoleSubscription != null)
|
||||
await ConsoleSubscription.DisposeAsync();
|
||||
@@ -48,7 +51,7 @@ public class RegexOnlineDetection : IOnlineDetection
|
||||
}
|
||||
else if (ConsoleSubscription != null) // Unsubscribe from console when any other state and not already unsubscribed
|
||||
{
|
||||
Logger.LogInformation("Detected state change to {state}. Unsubscribing from console", state);
|
||||
Logger.LogDebug("Detected state change to {state}. Unsubscribing from console", state);
|
||||
|
||||
await ConsoleSubscription.DisposeAsync();
|
||||
ConsoleSubscription = null;
|
||||
|
||||
@@ -17,10 +17,12 @@ public class ServerFactory
|
||||
var scope = ServiceProvider.CreateAsyncScope();
|
||||
|
||||
var context = scope.ServiceProvider.GetRequiredService<ServerContext>();
|
||||
var server = scope.ServiceProvider.GetRequiredService<Server>();
|
||||
|
||||
context.Configuration = configuration;
|
||||
context.ServiceScope = scope;
|
||||
|
||||
var server = scope.ServiceProvider.GetRequiredService<Server>();
|
||||
|
||||
context.Self = server;
|
||||
|
||||
return server;
|
||||
|
||||
Reference in New Issue
Block a user