Files
Servers/MoonlightServers.Daemon/Abstractions/Server.Destroy.cs

58 lines
1.7 KiB
C#

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());
RuntimeContainerId = null;
}
catch (DockerContainerNotFoundException){}
// Canceling server tasks & listeners and start new ones
await ResetTasks();
}
public async Task ResetTasks()
{
// Note: This will keep the docker container running, it will just cancel the server cancellation token
// and recreate the token
await CancelTasks();
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();
}
}