Fixed event/observer issues

This commit is contained in:
2025-07-30 17:12:21 +02:00
parent bb81ca9674
commit eaf8c36f7f
8 changed files with 113 additions and 87 deletions

View File

@@ -4,17 +4,19 @@ using System.Text;
using Docker.DotNet;
using Docker.DotNet.Models;
using MoonCore.Helpers;
using MoonCore.Observability;
using MoonlightServers.Daemon.Helpers;
using MoonlightServers.Daemon.ServerSys.Abstractions;
namespace MoonlightServers.Daemon.ServerSys.Implementations;
public class DockerConsole : IConsole
{
public IAsyncObservable<string> OnOutput => OnOutputSubject.ToAsyncObservable();
public IAsyncObservable<string> OnInput => OnInputSubject.ToAsyncObservable();
public IAsyncObservable<string> OnOutput => OnOutputSubject;
public IAsyncObservable<string> OnInput => OnInputSubject;
private readonly Subject<string> OnOutputSubject = new();
private readonly Subject<string> OnInputSubject = new();
private readonly EventSubject<string> OnOutputSubject = new();
private readonly EventSubject<string> OnInputSubject = new();
private readonly ConcurrentList<string> OutputCache = new();
private readonly DockerClient DockerClient;
@@ -140,15 +142,14 @@ public class DockerConsole : IConsole
return Task.CompletedTask;
}
public Task WriteToOutput(string content)
public async Task WriteToOutput(string content)
{
OutputCache.Add(content);
if (OutputCache.Count > 250) // TODO: Config
OutputCache.RemoveRange(0, 100);
OnOutputSubject.OnNext(content);
return Task.CompletedTask;
await OnOutputSubject.OnNextAsync(content);
}
public async Task WriteToInput(string content)
@@ -164,6 +165,8 @@ public class DockerConsole : IConsole
contentBuffer.Length,
Cts.Token
);
await OnInputSubject.OnNextAsync(content);
}
public async Task WriteToMoonlight(string content)

View File

@@ -2,7 +2,10 @@ using System.Reactive.Linq;
using System.Reactive.Subjects;
using Docker.DotNet;
using Docker.DotNet.Models;
using MoonCore.Observability;
using MoonlightServers.Daemon.Configuration;
using MoonlightServers.Daemon.Extensions;
using MoonlightServers.Daemon.Helpers;
using MoonlightServers.Daemon.Mappers;
using MoonlightServers.Daemon.ServerSys.Abstractions;
using MoonlightServers.Daemon.Services;
@@ -11,10 +14,10 @@ namespace MoonlightServers.Daemon.ServerSys.Implementations;
public class DockerInstaller : IInstaller
{
public IAsyncObservable<object> OnExited => OnExitedSubject.ToAsyncObservable();
public IAsyncObservable<object> OnExited => OnExitedSubject;
public bool IsRunning { get; private set; } = false;
private readonly Subject<Message> OnExitedSubject = new();
private readonly EventSubject<Message> OnExitedSubject = new();
private readonly ILogger<DockerInstaller> Logger;
private readonly DockerEventService EventService;
@@ -64,7 +67,7 @@ public class DockerInstaller : IInstaller
ContainerEventSubscription = await EventService
.OnContainerEvent
.SubscribeAsync(HandleContainerEvent);
.SubscribeEventAsync(HandleContainerEvent);
// Check for any already existing runtime container to reclaim
Logger.LogDebug("Searching for orphan container to reclaim");
@@ -82,19 +85,17 @@ public class DockerInstaller : IInstaller
}
}
private ValueTask HandleContainerEvent(Message message)
private async ValueTask HandleContainerEvent(Message message)
{
// Only handle events for our own container
if (message.ID != ContainerId)
return ValueTask.CompletedTask;
return;
// Only handle die events
if (message.Action != "die")
return ValueTask.CompletedTask;
return;
OnExitedSubject.OnNext(message);
return ValueTask.CompletedTask;
await OnExitedSubject.OnNextAsync(message);
}
public Task Sync()

View File

@@ -3,6 +3,9 @@ using System.Reactive.Linq;
using System.Reactive.Subjects;
using Docker.DotNet;
using Docker.DotNet.Models;
using MoonCore.Observability;
using MoonlightServers.Daemon.Extensions;
using MoonlightServers.Daemon.Helpers;
using MoonlightServers.Daemon.Mappers;
using MoonlightServers.Daemon.ServerSys.Abstractions;
using MoonlightServers.Daemon.Services;
@@ -11,7 +14,7 @@ namespace MoonlightServers.Daemon.ServerSys.Implementations;
public class DockerProvisioner : IProvisioner
{
public IAsyncObservable<object> OnExited => OnExitedSubject.ToAsyncObservable();
public IAsyncObservable<object> OnExited => OnExitedSubject;
public bool IsProvisioned { get; private set; }
private readonly DockerClient DockerClient;
@@ -23,7 +26,7 @@ public class DockerProvisioner : IProvisioner
private readonly ServerConfigurationMapper Mapper;
private readonly IFileSystem FileSystem;
private Subject<object> OnExitedSubject = new();
private EventSubject<object> OnExitedSubject = new();
private string? ContainerId;
private string ContainerName;
@@ -56,7 +59,7 @@ public class DockerProvisioner : IProvisioner
ContainerEventSubscription = await EventService
.OnContainerEvent
.SubscribeAsync(HandleContainerEvent);
.SubscribeEventAsync(HandleContainerEvent);
// Check for any already existing runtime container to reclaim
Logger.LogDebug("Searching for orphan container to reclaim");
@@ -74,19 +77,17 @@ public class DockerProvisioner : IProvisioner
}
}
private ValueTask HandleContainerEvent(Message message)
private async ValueTask HandleContainerEvent(Message message)
{
// Only handle events for our own container
if (message.ID != ContainerId)
return ValueTask.CompletedTask;
return;
// Only handle die events
if (message.Action != "die")
return ValueTask.CompletedTask;
return;
OnExitedSubject.OnNext(message);
return ValueTask.CompletedTask;
await OnExitedSubject.OnNextAsync(message);
}
public Task Sync()

View File

@@ -1,14 +1,14 @@
using System.Reactive.Linq;
using System.Reactive.Subjects;
using MoonCore.Observability;
using MoonlightServers.Daemon.Helpers;
using MoonlightServers.Daemon.ServerSys.Abstractions;
namespace MoonlightServers.Daemon.ServerSys.Implementations;
public class DockerStatistics : IStatistics
{
public IAsyncObservable<ServerStats> OnStats => OnStatsSubject.ToAsyncObservable();
public IAsyncObservable<ServerStats> OnStats => OnStatsSubject;
private readonly Subject<ServerStats> OnStatsSubject = new();
private readonly EventSubject<ServerStats> OnStatsSubject = new();
public Task Initialize()
=> Task.CompletedTask;