mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-05-11 20:34:03 +02:00
Improve exception handling when configuring and starting the Minecraft server
This commit is contained in:
parent
36dbc6f984
commit
55b643c513
Agent
Phantom.Agent.Minecraft/Launcher
Phantom.Agent.Services/Instances/States
Common/Phantom.Common.Data/Instance
@ -5,6 +5,7 @@ using Phantom.Agent.Minecraft.Instance;
|
||||
using Phantom.Agent.Minecraft.Java;
|
||||
using Phantom.Agent.Minecraft.Server;
|
||||
using Phantom.Common.Minecraft;
|
||||
using Serilog;
|
||||
|
||||
namespace Phantom.Agent.Minecraft.Launcher;
|
||||
|
||||
@ -15,7 +16,7 @@ public abstract class BaseLauncher {
|
||||
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)) {
|
||||
return new LaunchResult.InvalidJavaRuntime();
|
||||
}
|
||||
@ -52,12 +53,29 @@ public abstract class BaseLauncher {
|
||||
var process = new Process { StartInfo = startInfo };
|
||||
var session = new InstanceSession(process);
|
||||
|
||||
await AcceptEula(instanceProperties);
|
||||
await UpdateServerProperties(instanceProperties);
|
||||
try {
|
||||
await AcceptEula(instanceProperties);
|
||||
await UpdateServerProperties(instanceProperties);
|
||||
} catch (Exception e) {
|
||||
logger.Error(e, "Caught exception while configuring the server.");
|
||||
return new LaunchResult.CouldNotConfigureMinecraftServer();
|
||||
}
|
||||
|
||||
process.Start();
|
||||
process.BeginOutputReadLine();
|
||||
process.BeginErrorReadLine();
|
||||
try {
|
||||
process.Start();
|
||||
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);
|
||||
}
|
||||
@ -80,8 +98,8 @@ public abstract class BaseLauncher {
|
||||
await using var fileStream = new FileStream(serverPropertiesFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
|
||||
try {
|
||||
serverPropertiesData.Load(fileStream);
|
||||
} catch (ParseException) {
|
||||
// ignore
|
||||
} catch (ParseException e) {
|
||||
throw new Exception("Could not parse server.properties file: " + serverPropertiesFilePath, e);
|
||||
}
|
||||
|
||||
instanceProperties.ServerProperties.SetTo(serverPropertiesData);
|
||||
|
@ -12,4 +12,8 @@ public abstract record LaunchResult {
|
||||
public sealed record InvalidJvmArguments : LaunchResult;
|
||||
|
||||
public sealed record CouldNotDownloadMinecraftServer : LaunchResult;
|
||||
|
||||
public sealed record CouldNotConfigureMinecraftServer : LaunchResult;
|
||||
|
||||
public sealed record CouldNotStartMinecraftServer : LaunchResult;
|
||||
}
|
||||
|
@ -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) {
|
||||
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) {
|
||||
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) {
|
||||
throw new LaunchFailureException(InstanceLaunchFailReason.UnknownError, "Session failed to launch.");
|
||||
|
@ -8,20 +8,24 @@ public enum InstanceLaunchFailReason {
|
||||
RconPortAlreadyInUse,
|
||||
JavaRuntimeNotFound,
|
||||
InvalidJvmArguments,
|
||||
CouldNotDownloadMinecraftServer
|
||||
CouldNotDownloadMinecraftServer,
|
||||
CouldNotConfigureMinecraftServer,
|
||||
CouldNotStartMinecraftServer
|
||||
}
|
||||
|
||||
public static class InstanceLaunchFailReasonExtensions {
|
||||
public static string ToSentence(this InstanceLaunchFailReason reason) {
|
||||
return reason switch {
|
||||
InstanceLaunchFailReason.ServerPortNotAllowed => "Server port not allowed.",
|
||||
InstanceLaunchFailReason.ServerPortAlreadyInUse => "Server port already in use.",
|
||||
InstanceLaunchFailReason.RconPortNotAllowed => "Rcon port not allowed.",
|
||||
InstanceLaunchFailReason.RconPortAlreadyInUse => "Rcon port already in use.",
|
||||
InstanceLaunchFailReason.JavaRuntimeNotFound => "Java runtime not found.",
|
||||
InstanceLaunchFailReason.InvalidJvmArguments => "Invalid JVM arguments.",
|
||||
InstanceLaunchFailReason.CouldNotDownloadMinecraftServer => "Could not download Minecraft server.",
|
||||
_ => "Unknown error."
|
||||
InstanceLaunchFailReason.ServerPortNotAllowed => "Server port not allowed.",
|
||||
InstanceLaunchFailReason.ServerPortAlreadyInUse => "Server port already in use.",
|
||||
InstanceLaunchFailReason.RconPortNotAllowed => "Rcon port not allowed.",
|
||||
InstanceLaunchFailReason.RconPortAlreadyInUse => "Rcon port already in use.",
|
||||
InstanceLaunchFailReason.JavaRuntimeNotFound => "Java runtime not found.",
|
||||
InstanceLaunchFailReason.InvalidJvmArguments => "Invalid JVM arguments.",
|
||||
InstanceLaunchFailReason.CouldNotDownloadMinecraftServer => "Could not download Minecraft server.",
|
||||
InstanceLaunchFailReason.CouldNotConfigureMinecraftServer => "Could not configure Minecraft server.",
|
||||
InstanceLaunchFailReason.CouldNotStartMinecraftServer => "Could not start Minecraft server.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user