Re-implemented server state machine. Cleaned up code
TODO: Handle trigger errors
This commit is contained in:
50
MoonlightServers.Daemon/Abstractions/Server.Destroy.cs
Normal file
50
MoonlightServers.Daemon/Abstractions/Server.Destroy.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user