Cleaned up interfaces. Extracted server state machine trigger handler to seperated classes. Removed legacy code
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using MoonlightServers.Daemon.ServerSystem.Enums;
|
||||
using MoonlightServers.Daemon.ServerSystem.Interfaces;
|
||||
using MoonlightServers.Daemon.ServerSystem.Models;
|
||||
using Stateless;
|
||||
|
||||
namespace MoonlightServers.Daemon.ServerSystem.Handlers;
|
||||
|
||||
public class ShutdownHandler : IServerStateHandler
|
||||
{
|
||||
private readonly ServerContext ServerContext;
|
||||
|
||||
public ShutdownHandler(ServerContext serverContext)
|
||||
{
|
||||
ServerContext = serverContext;
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(StateMachine<ServerState, ServerTrigger>.Transition transition)
|
||||
{
|
||||
// Filter (we only want to handle exists from the runtime, so we filter out the installing state)
|
||||
if (transition is
|
||||
{
|
||||
Destination: ServerState.Offline,
|
||||
Source: not ServerState.Installing,
|
||||
Trigger: ServerTrigger.Exited
|
||||
})
|
||||
return;
|
||||
|
||||
// Plan:
|
||||
// 1. Handle possible crash
|
||||
// 2. Remove runtime
|
||||
|
||||
// 1. Handle possible crash
|
||||
// TODO: Handle crash here
|
||||
|
||||
// 2. Remove runtime
|
||||
|
||||
await ServerContext.Server.Runtime.DestroyAsync();
|
||||
}
|
||||
|
||||
public ValueTask DisposeAsync()
|
||||
=> ValueTask.CompletedTask;
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using MoonlightServers.Daemon.ServerSystem.Enums;
|
||||
using MoonlightServers.Daemon.ServerSystem.Interfaces;
|
||||
using MoonlightServers.Daemon.ServerSystem.Models;
|
||||
using Stateless;
|
||||
|
||||
namespace MoonlightServers.Daemon.ServerSystem.Handlers;
|
||||
|
||||
public class StartupHandler : IServerStateHandler
|
||||
{
|
||||
private IAsyncDisposable? ExitSubscription;
|
||||
|
||||
private readonly ServerContext Context;
|
||||
private Server Server => Context.Server;
|
||||
|
||||
public StartupHandler(ServerContext context)
|
||||
{
|
||||
Context = context;
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(StateMachine<ServerState, ServerTrigger>.Transition transition)
|
||||
{
|
||||
// Filter
|
||||
if (transition is not {Source: ServerState.Offline, Destination: ServerState.Starting, Trigger: ServerTrigger.Start})
|
||||
return;
|
||||
|
||||
// Plan:
|
||||
// 1. Fetch latest configuration
|
||||
// 2. Check if file system exists
|
||||
// 3. Check if file system is mounted
|
||||
// 4. Run file system checks
|
||||
// 5. Create runtime
|
||||
// 6. Attach console
|
||||
// 7. Attach statistics collector
|
||||
// 8. Create online detector
|
||||
// 9. Start runtime
|
||||
|
||||
// 1. Fetch latest configuration
|
||||
// TODO
|
||||
|
||||
// 2. Check if file system exists
|
||||
if (!await Server.RuntimeFileSystem.CheckExistsAsync())
|
||||
await Server.RuntimeFileSystem.CreateAsync();
|
||||
|
||||
// 3. Check if file system is mounted
|
||||
if (!await Server.RuntimeFileSystem.CheckMountedAsync())
|
||||
await Server.RuntimeFileSystem.CheckMountedAsync();
|
||||
|
||||
// 4. Run file system checks
|
||||
await Server.RuntimeFileSystem.PerformChecksAsync();
|
||||
|
||||
// 5. Create runtime
|
||||
var hostPath = await Server.RuntimeFileSystem.GetPathAsync();
|
||||
|
||||
await Server.Runtime.CreateAsync(hostPath);
|
||||
|
||||
if (ExitSubscription == null)
|
||||
{
|
||||
ExitSubscription = await Server.Runtime.SubscribeExited(OnRuntimeExited);
|
||||
}
|
||||
|
||||
// 6. Attach console
|
||||
|
||||
await Server.Console.AttachRuntimeAsync();
|
||||
|
||||
// 7. Attach statistics collector
|
||||
|
||||
await Server.Statistics.AttachRuntimeAsync();
|
||||
|
||||
// 8. Create online detector
|
||||
|
||||
await Server.OnlineDetector.CreateAsync();
|
||||
await Server.OnlineDetector.DestroyAsync();
|
||||
|
||||
// 9. Start runtime
|
||||
|
||||
await Server.Runtime.StartAsync();
|
||||
}
|
||||
|
||||
private async Task OnRuntimeExited(int exitCode)
|
||||
{
|
||||
// TODO: Notify the crash handler component of the exit code
|
||||
|
||||
await Server.StateMachine.FireAsync(ServerTrigger.Exited);
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (ExitSubscription != null)
|
||||
await ExitSubscription.DisposeAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user