Re-implemented server state machine. Cleaned up code

TODO: Handle trigger errors
This commit is contained in:
2025-02-12 23:02:00 +01:00
parent 4088bfaef5
commit f45699f300
44 changed files with 913 additions and 831 deletions

View File

@@ -0,0 +1,50 @@
using Docker.DotNet;
namespace MoonlightServers.Daemon.Abstractions;
public partial class Server
{
private async Task Destroy()
{
// Note: This only destroys the container, it doesn't delete any data
var dockerClient = ServiceProvider.GetRequiredService<DockerClient>();
try
{
var container = await dockerClient.Containers.InspectContainerAsync(
RuntimeContainerName
);
if (container.State.Running)
{
// Stop container when running
await LogToConsole("Stopping container");
await dockerClient.Containers.StopContainerAsync(container.ID, new()
{
WaitBeforeKillSeconds = 30 // TODO: Config
});
}
await LogToConsole("Removing container");
await dockerClient.Containers.RemoveContainerAsync(container.ID, new());
}
catch (DockerContainerNotFoundException){}
// Canceling server tasks & listeners
await CancelTasks();
// and recreating cancellation token
Cancellation = new();
}
public async Task CancelTasks()
{
// Note: This will keep the docker container running, it will just cancel the server cancellation token
if (!Cancellation.IsCancellationRequested)
await Cancellation.CancelAsync();
}
}