Recreated plugin with new project template. Started implementing server system daemon
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using Docker.DotNet;
|
||||
using MoonlightServers.Daemon.ServerSystem.Abstractions;
|
||||
using MoonlightServers.Daemon.ServerSystem.Implementations.Docker.Events;
|
||||
|
||||
namespace MoonlightServers.Daemon.ServerSystem.Implementations.Docker;
|
||||
|
||||
public class DockerInstallEnv : IInstallEnvironment
|
||||
{
|
||||
public IInstallStatistics Statistics => InnerStatistics;
|
||||
public IInstallConsole Console => InnerConsole;
|
||||
|
||||
public event Func<Task>? OnExited;
|
||||
|
||||
public string ContainerId { get; }
|
||||
|
||||
private readonly DockerClient DockerClient;
|
||||
private readonly ILogger Logger;
|
||||
private readonly DockerEventService EventService;
|
||||
|
||||
private readonly DockerStatistics InnerStatistics;
|
||||
private readonly DockerConsole InnerConsole;
|
||||
|
||||
public DockerInstallEnv(string containerId, DockerClient dockerClient, ILogger logger, DockerEventService eventService)
|
||||
{
|
||||
ContainerId = containerId;
|
||||
DockerClient = dockerClient;
|
||||
Logger = logger;
|
||||
EventService = eventService;
|
||||
|
||||
InnerStatistics = new DockerStatistics();
|
||||
InnerConsole = new DockerConsole(containerId, dockerClient, logger);
|
||||
|
||||
EventService.OnContainerDied += HandleDieEventAsync;
|
||||
}
|
||||
|
||||
public async Task<bool> IsRunningAsync()
|
||||
{
|
||||
var container = await DockerClient.Containers.InspectContainerAsync(ContainerId);
|
||||
return container.State.Running;
|
||||
}
|
||||
|
||||
public async Task StartAsync()
|
||||
=> await DockerClient.Containers.StartContainerAsync(ContainerId, new());
|
||||
|
||||
public async Task KillAsync()
|
||||
=> await DockerClient.Containers.KillContainerAsync(ContainerId, new());
|
||||
|
||||
private async Task HandleDieEventAsync(ContainerDieEvent dieEvent)
|
||||
{
|
||||
if(dieEvent.ContainerId != ContainerId)
|
||||
return;
|
||||
|
||||
if(OnExited != null)
|
||||
await OnExited.Invoke();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
EventService.OnContainerDied -= HandleDieEventAsync;
|
||||
|
||||
await InnerConsole.DisposeAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user