Improved logging in server components
This commit is contained in:
@@ -23,7 +23,7 @@ public class Server : IAsyncDisposable
|
|||||||
public IAsyncObservable<ServerState> OnState => OnStateSubject;
|
public IAsyncObservable<ServerState> OnState => OnStateSubject;
|
||||||
|
|
||||||
private readonly EventSubject<ServerState> OnStateSubject = new();
|
private readonly EventSubject<ServerState> OnStateSubject = new();
|
||||||
private readonly ILogger<Server> Logger;
|
private readonly ILogger Logger;
|
||||||
private readonly RemoteService RemoteService;
|
private readonly RemoteService RemoteService;
|
||||||
private readonly ServerConfigurationMapper Mapper;
|
private readonly ServerConfigurationMapper Mapper;
|
||||||
private readonly IHubContext<ServerWebSocketHub> HubContext;
|
private readonly IHubContext<ServerWebSocketHub> HubContext;
|
||||||
@@ -33,7 +33,7 @@ public class Server : IAsyncDisposable
|
|||||||
private IAsyncDisposable? ConsoleSubscription;
|
private IAsyncDisposable? ConsoleSubscription;
|
||||||
|
|
||||||
public Server(
|
public Server(
|
||||||
ILogger<Server> logger,
|
ILoggerFactory loggerFactory,
|
||||||
IConsole console,
|
IConsole console,
|
||||||
IFileSystem fileSystem,
|
IFileSystem fileSystem,
|
||||||
IInstaller installer,
|
IInstaller installer,
|
||||||
@@ -46,7 +46,7 @@ public class Server : IAsyncDisposable
|
|||||||
ServerConfigurationMapper mapper,
|
ServerConfigurationMapper mapper,
|
||||||
IHubContext<ServerWebSocketHub> hubContext)
|
IHubContext<ServerWebSocketHub> hubContext)
|
||||||
{
|
{
|
||||||
Logger = logger;
|
Logger = loggerFactory.CreateLogger($"Servers.Instance.{context.Configuration.Id}.{nameof(Server)}");
|
||||||
Console = console;
|
Console = console;
|
||||||
FileSystem = fileSystem;
|
FileSystem = fileSystem;
|
||||||
Installer = installer;
|
Installer = installer;
|
||||||
@@ -192,6 +192,20 @@ public class Server : IAsyncDisposable
|
|||||||
|
|
||||||
public async Task Delete()
|
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();
|
await FileSystem.Delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,19 +5,22 @@ namespace MoonlightServers.Daemon.ServerSys.Implementations;
|
|||||||
|
|
||||||
public class DefaultRestorer : IRestorer
|
public class DefaultRestorer : IRestorer
|
||||||
{
|
{
|
||||||
private readonly ILogger<DefaultRestorer> Logger;
|
private readonly ILogger Logger;
|
||||||
private readonly IConsole Console;
|
private readonly IConsole Console;
|
||||||
private readonly IProvisioner Provisioner;
|
private readonly IProvisioner Provisioner;
|
||||||
private readonly IInstaller Installer;
|
private readonly IInstaller Installer;
|
||||||
private readonly IStatistics Statistics;
|
private readonly IStatistics Statistics;
|
||||||
|
|
||||||
public DefaultRestorer(
|
public DefaultRestorer(
|
||||||
ILogger<DefaultRestorer> logger,
|
ILoggerFactory loggerFactory,
|
||||||
|
ServerContext context,
|
||||||
IConsole console,
|
IConsole console,
|
||||||
IProvisioner provisioner,
|
IProvisioner provisioner,
|
||||||
IStatistics statistics, IInstaller installer)
|
IStatistics statistics,
|
||||||
|
IInstaller installer
|
||||||
|
)
|
||||||
{
|
{
|
||||||
Logger = logger;
|
Logger = loggerFactory.CreateLogger($"Servers.Instance.{context.Configuration.Id}.{nameof(DefaultRestorer)}");
|
||||||
Console = console;
|
Console = console;
|
||||||
Provisioner = provisioner;
|
Provisioner = provisioner;
|
||||||
Statistics = statistics;
|
Statistics = statistics;
|
||||||
|
|||||||
@@ -17,22 +17,24 @@ public class DockerConsole : IConsole
|
|||||||
|
|
||||||
private readonly ConcurrentList<string> OutputCache = new();
|
private readonly ConcurrentList<string> OutputCache = new();
|
||||||
private readonly DockerClient DockerClient;
|
private readonly DockerClient DockerClient;
|
||||||
private readonly ILogger<DockerConsole> Logger;
|
private readonly ILogger Logger;
|
||||||
private readonly ServerContext Context;
|
private readonly ServerContext Context;
|
||||||
|
|
||||||
private MultiplexedStream? CurrentStream;
|
private MultiplexedStream? CurrentStream;
|
||||||
private CancellationTokenSource Cts = new();
|
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(
|
public DockerConsole(
|
||||||
ServerContext context,
|
ServerContext context,
|
||||||
DockerClient dockerClient,
|
DockerClient dockerClient,
|
||||||
ILogger<DockerConsole> logger)
|
ILoggerFactory loggerFactory
|
||||||
|
)
|
||||||
{
|
{
|
||||||
Context = context;
|
Context = context;
|
||||||
DockerClient = dockerClient;
|
DockerClient = dockerClient;
|
||||||
Logger = logger;
|
Logger = loggerFactory.CreateLogger($"Servers.Instance.{context.Configuration.Id}.{nameof(DockerConsole)}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task Initialize()
|
public Task Initialize()
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public class DockerInstaller : IInstaller
|
|||||||
|
|
||||||
private readonly EventSubject<Message> OnExitedSubject = new();
|
private readonly EventSubject<Message> OnExitedSubject = new();
|
||||||
|
|
||||||
private readonly ILogger<DockerInstaller> Logger;
|
private readonly ILogger Logger;
|
||||||
private readonly DockerEventService EventService;
|
private readonly DockerEventService EventService;
|
||||||
private readonly IConsole Console;
|
private readonly IConsole Console;
|
||||||
private readonly DockerClient DockerClient;
|
private readonly DockerClient DockerClient;
|
||||||
@@ -37,7 +37,7 @@ public class DockerInstaller : IInstaller
|
|||||||
private IAsyncDisposable? ContainerEventSubscription;
|
private IAsyncDisposable? ContainerEventSubscription;
|
||||||
|
|
||||||
public DockerInstaller(
|
public DockerInstaller(
|
||||||
ILogger<DockerInstaller> logger,
|
ILoggerFactory loggerFactory,
|
||||||
DockerEventService eventService,
|
DockerEventService eventService,
|
||||||
IConsole console,
|
IConsole console,
|
||||||
DockerClient dockerClient,
|
DockerClient dockerClient,
|
||||||
@@ -48,7 +48,7 @@ public class DockerInstaller : IInstaller
|
|||||||
ServerConfigurationMapper mapper
|
ServerConfigurationMapper mapper
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
Logger = logger;
|
Logger = loggerFactory.CreateLogger($"Servers.Instance.{context.Configuration.Id}.{nameof(DockerInstaller)}");
|
||||||
EventService = eventService;
|
EventService = eventService;
|
||||||
Console = console;
|
Console = console;
|
||||||
DockerClient = dockerClient;
|
DockerClient = dockerClient;
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ public class DockerProvisioner : IProvisioner
|
|||||||
public bool IsProvisioned { get; private set; }
|
public bool IsProvisioned { get; private set; }
|
||||||
|
|
||||||
private readonly DockerClient DockerClient;
|
private readonly DockerClient DockerClient;
|
||||||
private readonly ILogger<DockerProvisioner> Logger;
|
private readonly ILogger Logger;
|
||||||
private readonly DockerEventService EventService;
|
private readonly DockerEventService EventService;
|
||||||
private readonly ServerContext Context;
|
private readonly ServerContext Context;
|
||||||
private readonly IConsole Console;
|
private readonly IConsole Console;
|
||||||
@@ -34,7 +34,7 @@ public class DockerProvisioner : IProvisioner
|
|||||||
|
|
||||||
public DockerProvisioner(
|
public DockerProvisioner(
|
||||||
DockerClient dockerClient,
|
DockerClient dockerClient,
|
||||||
ILogger<DockerProvisioner> logger,
|
ILoggerFactory loggerFactory,
|
||||||
DockerEventService eventService,
|
DockerEventService eventService,
|
||||||
ServerContext context,
|
ServerContext context,
|
||||||
IConsole console,
|
IConsole console,
|
||||||
@@ -44,7 +44,7 @@ public class DockerProvisioner : IProvisioner
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
DockerClient = dockerClient;
|
DockerClient = dockerClient;
|
||||||
Logger = logger;
|
Logger = loggerFactory.CreateLogger($"Servers.Instance.{context.Configuration.Id}.{nameof(DockerProvisioner)}");
|
||||||
EventService = eventService;
|
EventService = eventService;
|
||||||
Context = context;
|
Context = context;
|
||||||
Console = console;
|
Console = console;
|
||||||
|
|||||||
@@ -9,28 +9,31 @@ public class RegexOnlineDetection : IOnlineDetection
|
|||||||
{
|
{
|
||||||
private readonly ServerContext Context;
|
private readonly ServerContext Context;
|
||||||
private readonly IConsole Console;
|
private readonly IConsole Console;
|
||||||
private readonly ILogger<RegexOnlineDetection> Logger;
|
private readonly ILogger Logger;
|
||||||
|
|
||||||
private Regex? Regex;
|
private Regex? Regex;
|
||||||
private IAsyncDisposable? ConsoleSubscription;
|
private IAsyncDisposable? ConsoleSubscription;
|
||||||
private IAsyncDisposable? StateSubscription;
|
private IAsyncDisposable? StateSubscription;
|
||||||
|
|
||||||
public RegexOnlineDetection(ServerContext context, IConsole console, ILogger<RegexOnlineDetection> logger)
|
public RegexOnlineDetection(
|
||||||
|
ServerContext context,
|
||||||
|
IConsole console,
|
||||||
|
ILoggerFactory loggerFactory)
|
||||||
{
|
{
|
||||||
Context = context;
|
Context = context;
|
||||||
Console = console;
|
Console = console;
|
||||||
Logger = logger;
|
Logger = loggerFactory.CreateLogger($"Servers.Instance.{context.Configuration.Id}.{nameof(RegexOnlineDetection)}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Initialize()
|
public async Task Initialize()
|
||||||
{
|
{
|
||||||
Logger.LogInformation("Subscribing to state changes");
|
Logger.LogDebug("Subscribing to state changes");
|
||||||
|
|
||||||
StateSubscription = await Context.Self.OnState.SubscribeAsync(async state =>
|
StateSubscription = await Context.Self.OnState.SubscribeAsync(async state =>
|
||||||
{
|
{
|
||||||
if (state == ServerState.Starting) // Subscribe to console when starting
|
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)
|
if(ConsoleSubscription != null)
|
||||||
await ConsoleSubscription.DisposeAsync();
|
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
|
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();
|
await ConsoleSubscription.DisposeAsync();
|
||||||
ConsoleSubscription = null;
|
ConsoleSubscription = null;
|
||||||
|
|||||||
@@ -17,10 +17,12 @@ public class ServerFactory
|
|||||||
var scope = ServiceProvider.CreateAsyncScope();
|
var scope = ServiceProvider.CreateAsyncScope();
|
||||||
|
|
||||||
var context = scope.ServiceProvider.GetRequiredService<ServerContext>();
|
var context = scope.ServiceProvider.GetRequiredService<ServerContext>();
|
||||||
var server = scope.ServiceProvider.GetRequiredService<Server>();
|
|
||||||
|
|
||||||
context.Configuration = configuration;
|
context.Configuration = configuration;
|
||||||
context.ServiceScope = scope;
|
context.ServiceScope = scope;
|
||||||
|
|
||||||
|
var server = scope.ServiceProvider.GetRequiredService<Server>();
|
||||||
|
|
||||||
context.Self = server;
|
context.Self = server;
|
||||||
|
|
||||||
return server;
|
return server;
|
||||||
|
|||||||
Reference in New Issue
Block a user