1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2025-05-18 13:34:04 +02:00

Fix not flushing logs when calling Environment.Exit

This commit is contained in:
chylex 2023-04-02 08:45:37 +02:00
parent 626f141a2c
commit ad7964677e
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
5 changed files with 50 additions and 39 deletions
Agent
Phantom.Agent.Services/Rpc
Phantom.Agent
Server/Phantom.Server

View File

@ -54,6 +54,8 @@ public sealed class MessageListener : IMessageToAgentListener {
}; };
Logger.Fatal("Agent authentication failed: {Error}", errorMessage); Logger.Fatal("Agent authentication failed: {Error}", errorMessage);
PhantomLogger.Dispose();
Environment.Exit(1); Environment.Exit(1);
return Task.FromResult(NoReply.Instance); return Task.FromResult(NoReply.Instance);

View File

@ -25,21 +25,21 @@ try {
PhantomLogger.Root.InformationHeading("Initializing Phantom Panel agent..."); PhantomLogger.Root.InformationHeading("Initializing Phantom Panel agent...");
PhantomLogger.Root.Information("Agent version: {Version}", fullVersion); PhantomLogger.Root.Information("Agent version: {Version}", fullVersion);
var (serverHost, serverPort, javaSearchPath, agentKeyToken, agentKeyFilePath, agentName, maxInstances, maxMemory, allowedServerPorts, allowedRconPorts, maxConcurrentBackupCompressionTasks) = Variables.LoadOrExit(); var (serverHost, serverPort, javaSearchPath, agentKeyToken, agentKeyFilePath, agentName, maxInstances, maxMemory, allowedServerPorts, allowedRconPorts, maxConcurrentBackupCompressionTasks) = Variables.LoadOrStop();
var agentKey = await AgentKey.Load(agentKeyToken, agentKeyFilePath); var agentKey = await AgentKey.Load(agentKeyToken, agentKeyFilePath);
if (agentKey == null) { if (agentKey == null) {
Environment.Exit(1); return 1;
} }
var folders = new AgentFolders("./data", "./temp", javaSearchPath); var folders = new AgentFolders("./data", "./temp", javaSearchPath);
if (!folders.TryCreate()) { if (!folders.TryCreate()) {
Environment.Exit(1); return 1;
} }
var agentGuid = await GuidFile.CreateOrLoad(folders.DataFolderPath); var agentGuid = await GuidFile.CreateOrLoad(folders.DataFolderPath);
if (agentGuid == null) { if (agentGuid == null) {
Environment.Exit(1); return 1;
} }
var (serverCertificate, agentToken) = agentKey.Value; var (serverCertificate, agentToken) = agentKey.Value;
@ -67,10 +67,15 @@ try {
await rpcTask; await rpcTask;
rpcDisconnectSemaphore.Dispose(); rpcDisconnectSemaphore.Dispose();
} }
return 0;
} catch (OperationCanceledException) { } catch (OperationCanceledException) {
// Ignore. return 0;
} catch (StopProcedureException) {
return 1;
} catch (Exception e) { } catch (Exception e) {
PhantomLogger.Root.Fatal(e, "Caught exception in entry point."); PhantomLogger.Root.Fatal(e, "Caught exception in entry point.");
return 1;
} finally { } finally {
shutdownCancellationTokenSource.Dispose(); shutdownCancellationTokenSource.Dispose();

View File

@ -41,13 +41,12 @@ sealed record Variables(
return JavaRuntimeDiscovery.GetSystemSearchPath() ?? throw new Exception("Could not automatically determine the path to Java installations on this system. Please set the JAVA_SEARCH_PATH environment variable to the folder containing Java installations."); return JavaRuntimeDiscovery.GetSystemSearchPath() ?? throw new Exception("Could not automatically determine the path to Java installations on this system. Please set the JAVA_SEARCH_PATH environment variable to the folder containing Java installations.");
} }
public static Variables LoadOrExit() { public static Variables LoadOrStop() {
try { try {
return LoadOrThrow(); return LoadOrThrow();
} catch (Exception e) { } catch (Exception e) {
PhantomLogger.Root.Fatal(e.Message); PhantomLogger.Root.Fatal(e.Message);
Environment.Exit(1); throw StopProcedureException.Instance;
throw;
} }
} }
} }

View File

@ -15,19 +15,18 @@ using WebConfiguration = Phantom.Server.Web.Configuration;
using WebLauncher = Phantom.Server.Web.Launcher; using WebLauncher = Phantom.Server.Web.Launcher;
var cancellationTokenSource = new CancellationTokenSource(); var cancellationTokenSource = new CancellationTokenSource();
var taskManager = new TaskManager(PhantomLogger.Create<TaskManager>("Server"));
PosixSignals.RegisterCancellation(cancellationTokenSource, static () => { PosixSignals.RegisterCancellation(cancellationTokenSource, static () => {
PhantomLogger.Root.InformationHeading("Stopping Phantom Panel server..."); PhantomLogger.Root.InformationHeading("Stopping Phantom Panel server...");
}); });
static void CreateFolderOrExit(string path, UnixFileMode chmod) { static void CreateFolderOrStop(string path, UnixFileMode chmod) {
if (!Directory.Exists(path)) { if (!Directory.Exists(path)) {
try { try {
Directories.Create(path, chmod); Directories.Create(path, chmod);
} catch (Exception e) { } catch (Exception e) {
PhantomLogger.Root.Fatal(e, "Error creating folder: {FolderName}", path); PhantomLogger.Root.Fatal(e, "Error creating folder: {FolderName}", path);
Environment.Exit(1); throw StopProcedureException.Instance;
} }
} }
} }
@ -38,49 +37,56 @@ try {
PhantomLogger.Root.InformationHeading("Initializing Phantom Panel server..."); PhantomLogger.Root.InformationHeading("Initializing Phantom Panel server...");
PhantomLogger.Root.Information("Server version: {Version}", fullVersion); PhantomLogger.Root.Information("Server version: {Version}", fullVersion);
var (webServerHost, webServerPort, webBasePath, rpcServerHost, rpcServerPort, sqlConnectionString) = Variables.LoadOrExit(); var (webServerHost, webServerPort, webBasePath, rpcServerHost, rpcServerPort, sqlConnectionString) = Variables.LoadOrStop();
string secretsPath = Path.GetFullPath("./secrets"); string secretsPath = Path.GetFullPath("./secrets");
CreateFolderOrExit(secretsPath, Chmod.URWX_GRX); CreateFolderOrStop(secretsPath, Chmod.URWX_GRX);
string webKeysPath = Path.GetFullPath("./keys"); string webKeysPath = Path.GetFullPath("./keys");
CreateFolderOrExit(webKeysPath, Chmod.URWX); CreateFolderOrStop(webKeysPath, Chmod.URWX);
var certificateData = await CertificateFiles.CreateOrLoad(secretsPath); var certificateData = await CertificateFiles.CreateOrLoad(secretsPath);
if (certificateData == null) { if (certificateData == null) {
Environment.Exit(1); return 1;
} }
var (certificate, agentToken) = certificateData.Value; var (certificate, agentToken) = certificateData.Value;
var rpcConfiguration = new RpcConfiguration(PhantomLogger.Create("Rpc"), PhantomLogger.Create<TaskManager>("Rpc"), rpcServerHost, rpcServerPort, certificate);
var webConfiguration = new WebConfiguration(PhantomLogger.Create("Web"), webServerHost, webServerPort, webBasePath, webKeysPath, cancellationTokenSource.Token);
PhantomLogger.Root.InformationHeading("Launching Phantom Panel server..."); PhantomLogger.Root.InformationHeading("Launching Phantom Panel server...");
var administratorToken = TokenGenerator.Create(60); var taskManager = new TaskManager(PhantomLogger.Create<TaskManager>("Server"));
PhantomLogger.Root.Information("Your administrator token is: {AdministratorToken}", administratorToken); try {
PhantomLogger.Root.Information("For administrator setup, visit: {HttpUrl}{SetupPath}", webConfiguration.HttpUrl, webConfiguration.BasePath + "setup"); var rpcConfiguration = new RpcConfiguration(PhantomLogger.Create("Rpc"), PhantomLogger.Create<TaskManager>("Rpc"), rpcServerHost, rpcServerPort, certificate);
var webConfiguration = new WebConfiguration(PhantomLogger.Create("Web"), webServerHost, webServerPort, webBasePath, webKeysPath, cancellationTokenSource.Token);
var serviceConfiguration = new ServiceConfiguration(fullVersion, TokenGenerator.GetBytesOrThrow(administratorToken), cancellationTokenSource.Token); var administratorToken = TokenGenerator.Create(60);
var webConfigurator = new WebConfigurator(serviceConfiguration, taskManager, agentToken); PhantomLogger.Root.Information("Your administrator token is: {AdministratorToken}", administratorToken);
var webApplication = await WebLauncher.CreateApplication(webConfiguration, webConfigurator, options => options.UseNpgsql(sqlConnectionString, static options => { PhantomLogger.Root.Information("For administrator setup, visit: {HttpUrl}{SetupPath}", webConfiguration.HttpUrl, webConfiguration.BasePath + "setup");
options.CommandTimeout(10).MigrationsAssembly(typeof(ApplicationDbContextDesignFactory).Assembly.FullName);
}));
await Task.WhenAll( var serviceConfiguration = new ServiceConfiguration(fullVersion, TokenGenerator.GetBytesOrThrow(administratorToken), cancellationTokenSource.Token);
RpcLauncher.Launch(rpcConfiguration, webApplication.Services.GetRequiredService<MessageToServerListenerFactory>().CreateListener, cancellationTokenSource.Token), var webConfigurator = new WebConfigurator(serviceConfiguration, taskManager, agentToken);
WebLauncher.Launch(webConfiguration, webApplication) var webApplication = await WebLauncher.CreateApplication(webConfiguration, webConfigurator, options => options.UseNpgsql(sqlConnectionString, static options => {
); options.CommandTimeout(10).MigrationsAssembly(typeof(ApplicationDbContextDesignFactory).Assembly.FullName);
}));
await Task.WhenAll(
RpcLauncher.Launch(rpcConfiguration, webApplication.Services.GetRequiredService<MessageToServerListenerFactory>().CreateListener, cancellationTokenSource.Token),
WebLauncher.Launch(webConfiguration, webApplication)
);
} finally {
cancellationTokenSource.Cancel();
await taskManager.Stop();
}
return 0;
} catch (OperationCanceledException) { } catch (OperationCanceledException) {
// Ignore. return 0;
} catch (StopProcedureException) { } catch (StopProcedureException) {
// Ignore. return 1;
} catch (Exception e) {
PhantomLogger.Root.Fatal(e, "Caught exception in entry point.");
return 1;
} finally { } finally {
cancellationTokenSource.Cancel();
await taskManager.Stop();
cancellationTokenSource.Dispose(); cancellationTokenSource.Dispose();
PhantomLogger.Root.Information("Bye!"); PhantomLogger.Root.Information("Bye!");
PhantomLogger.Dispose(); PhantomLogger.Dispose();

View File

@ -31,13 +31,12 @@ sealed record Variables(
); );
} }
public static Variables LoadOrExit() { public static Variables LoadOrStop() {
try { try {
return LoadOrThrow(); return LoadOrThrow();
} catch (Exception e) { } catch (Exception e) {
PhantomLogger.Root.Fatal(e.Message); PhantomLogger.Root.Fatal(e.Message);
Environment.Exit(1); throw StopProcedureException.Instance;
throw;
} }
} }
} }