1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2025-05-31 07:34:03 +02:00

Improve exception handling when configuring and starting the Minecraft server

This commit is contained in:
chylex 2022-10-24 14:04:29 +02:00
parent 36dbc6f984
commit 55b643c513
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
4 changed files with 50 additions and 18 deletions
Agent
Phantom.Agent.Minecraft/Launcher
Phantom.Agent.Services/Instances/States
Common/Phantom.Common.Data/Instance

View File

@ -5,6 +5,7 @@ using Phantom.Agent.Minecraft.Instance;
using Phantom.Agent.Minecraft.Java; using Phantom.Agent.Minecraft.Java;
using Phantom.Agent.Minecraft.Server; using Phantom.Agent.Minecraft.Server;
using Phantom.Common.Minecraft; using Phantom.Common.Minecraft;
using Serilog;
namespace Phantom.Agent.Minecraft.Launcher; namespace Phantom.Agent.Minecraft.Launcher;
@ -15,7 +16,7 @@ public abstract class BaseLauncher {
this.instanceProperties = instanceProperties; this.instanceProperties = instanceProperties;
} }
public async Task<LaunchResult> Launch(LaunchServices services, EventHandler<DownloadProgressEventArgs> downloadProgressEventHandler, CancellationToken cancellationToken) { public async Task<LaunchResult> Launch(ILogger logger, LaunchServices services, EventHandler<DownloadProgressEventArgs> downloadProgressEventHandler, CancellationToken cancellationToken) {
if (!services.JavaRuntimeRepository.TryGetByGuid(instanceProperties.JavaRuntimeGuid, out var javaRuntimeExecutable)) { if (!services.JavaRuntimeRepository.TryGetByGuid(instanceProperties.JavaRuntimeGuid, out var javaRuntimeExecutable)) {
return new LaunchResult.InvalidJavaRuntime(); return new LaunchResult.InvalidJavaRuntime();
} }
@ -52,12 +53,29 @@ public abstract class BaseLauncher {
var process = new Process { StartInfo = startInfo }; var process = new Process { StartInfo = startInfo };
var session = new InstanceSession(process); var session = new InstanceSession(process);
await AcceptEula(instanceProperties); try {
await UpdateServerProperties(instanceProperties); await AcceptEula(instanceProperties);
await UpdateServerProperties(instanceProperties);
} catch (Exception e) {
logger.Error(e, "Caught exception while configuring the server.");
return new LaunchResult.CouldNotConfigureMinecraftServer();
}
process.Start(); try {
process.BeginOutputReadLine(); process.Start();
process.BeginErrorReadLine(); process.BeginOutputReadLine();
process.BeginErrorReadLine();
} catch (Exception launchException) {
logger.Error(launchException, "Caught exception launching the server process.");
try {
process.Kill();
} catch (Exception killException) {
logger.Error(killException, "Caught exception trying to kill the server process after a failed launch.");
}
return new LaunchResult.CouldNotStartMinecraftServer();
}
return new LaunchResult.Success(session); return new LaunchResult.Success(session);
} }
@ -80,8 +98,8 @@ public abstract class BaseLauncher {
await using var fileStream = new FileStream(serverPropertiesFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); await using var fileStream = new FileStream(serverPropertiesFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
try { try {
serverPropertiesData.Load(fileStream); serverPropertiesData.Load(fileStream);
} catch (ParseException) { } catch (ParseException e) {
// ignore throw new Exception("Could not parse server.properties file: " + serverPropertiesFilePath, e);
} }
instanceProperties.ServerProperties.SetTo(serverPropertiesData); instanceProperties.ServerProperties.SetTo(serverPropertiesData);

View File

@ -12,4 +12,8 @@ public abstract record LaunchResult {
public sealed record InvalidJvmArguments : LaunchResult; public sealed record InvalidJvmArguments : LaunchResult;
public sealed record CouldNotDownloadMinecraftServer : LaunchResult; public sealed record CouldNotDownloadMinecraftServer : LaunchResult;
public sealed record CouldNotConfigureMinecraftServer : LaunchResult;
public sealed record CouldNotStartMinecraftServer : LaunchResult;
} }

View File

@ -37,7 +37,7 @@ sealed class InstanceLaunchingState : IInstanceState, IDisposable {
} }
} }
var launchResult = await context.Launcher.Launch(context.LaunchServices, OnDownloadProgress, cancellationToken); var launchResult = await context.Launcher.Launch(context.Logger, context.LaunchServices, OnDownloadProgress, cancellationToken);
if (launchResult is LaunchResult.InvalidJavaRuntime) { if (launchResult is LaunchResult.InvalidJavaRuntime) {
throw new LaunchFailureException(InstanceLaunchFailReason.JavaRuntimeNotFound, "Session failed to launch, invalid Java runtime."); throw new LaunchFailureException(InstanceLaunchFailReason.JavaRuntimeNotFound, "Session failed to launch, invalid Java runtime.");
} }
@ -47,6 +47,12 @@ sealed class InstanceLaunchingState : IInstanceState, IDisposable {
else if (launchResult is LaunchResult.CouldNotDownloadMinecraftServer) { else if (launchResult is LaunchResult.CouldNotDownloadMinecraftServer) {
throw new LaunchFailureException(InstanceLaunchFailReason.CouldNotDownloadMinecraftServer, "Session failed to launch, could not download Minecraft server."); throw new LaunchFailureException(InstanceLaunchFailReason.CouldNotDownloadMinecraftServer, "Session failed to launch, could not download Minecraft server.");
} }
else if (launchResult is LaunchResult.CouldNotConfigureMinecraftServer) {
throw new LaunchFailureException(InstanceLaunchFailReason.CouldNotConfigureMinecraftServer, "Session failed to launch, could not configure Minecraft server.");
}
else if (launchResult is LaunchResult.CouldNotStartMinecraftServer) {
throw new LaunchFailureException(InstanceLaunchFailReason.CouldNotStartMinecraftServer, "Session failed to launch, could not start Minecraft server.");
}
if (launchResult is not LaunchResult.Success launchSuccess) { if (launchResult is not LaunchResult.Success launchSuccess) {
throw new LaunchFailureException(InstanceLaunchFailReason.UnknownError, "Session failed to launch."); throw new LaunchFailureException(InstanceLaunchFailReason.UnknownError, "Session failed to launch.");

View File

@ -8,20 +8,24 @@ public enum InstanceLaunchFailReason {
RconPortAlreadyInUse, RconPortAlreadyInUse,
JavaRuntimeNotFound, JavaRuntimeNotFound,
InvalidJvmArguments, InvalidJvmArguments,
CouldNotDownloadMinecraftServer CouldNotDownloadMinecraftServer,
CouldNotConfigureMinecraftServer,
CouldNotStartMinecraftServer
} }
public static class InstanceLaunchFailReasonExtensions { public static class InstanceLaunchFailReasonExtensions {
public static string ToSentence(this InstanceLaunchFailReason reason) { public static string ToSentence(this InstanceLaunchFailReason reason) {
return reason switch { return reason switch {
InstanceLaunchFailReason.ServerPortNotAllowed => "Server port not allowed.", InstanceLaunchFailReason.ServerPortNotAllowed => "Server port not allowed.",
InstanceLaunchFailReason.ServerPortAlreadyInUse => "Server port already in use.", InstanceLaunchFailReason.ServerPortAlreadyInUse => "Server port already in use.",
InstanceLaunchFailReason.RconPortNotAllowed => "Rcon port not allowed.", InstanceLaunchFailReason.RconPortNotAllowed => "Rcon port not allowed.",
InstanceLaunchFailReason.RconPortAlreadyInUse => "Rcon port already in use.", InstanceLaunchFailReason.RconPortAlreadyInUse => "Rcon port already in use.",
InstanceLaunchFailReason.JavaRuntimeNotFound => "Java runtime not found.", InstanceLaunchFailReason.JavaRuntimeNotFound => "Java runtime not found.",
InstanceLaunchFailReason.InvalidJvmArguments => "Invalid JVM arguments.", InstanceLaunchFailReason.InvalidJvmArguments => "Invalid JVM arguments.",
InstanceLaunchFailReason.CouldNotDownloadMinecraftServer => "Could not download Minecraft server.", InstanceLaunchFailReason.CouldNotDownloadMinecraftServer => "Could not download Minecraft server.",
_ => "Unknown error." InstanceLaunchFailReason.CouldNotConfigureMinecraftServer => "Could not configure Minecraft server.",
InstanceLaunchFailReason.CouldNotStartMinecraftServer => "Could not start Minecraft server.",
_ => "Unknown error."
}; };
} }
} }