namespace MoonlightServers.Daemon.ServerSystem; public partial class Server { // Attempts to reattach to any running install or runtime environment that survived a daemon restart. // Returns the appropriate state based on what was found, or Offline if nothing is running. private async Task RestoreAsync() { // Install Logger.LogTrace("Checking for existing install environment"); InstallEnvironment = await InstallEnvironmentService.FindAsync(Uuid); InstallStorage = await InstallStorageService.FindAsync(Uuid); if (InstallEnvironment != null) { var isRunning = await InstallEnvironment.IsRunningAsync(); if (isRunning) { Logger.LogTrace("Found running install environment, reattaching"); InstallEnvironment.Console.OnOutput += OnConsoleMessageAsync; InstallEnvironment.Statistics.OnStatisticsReceived += OnStatisticsReceivedAsync; InstallEnvironment.OnExited += OnInstallExitedAsync; await InstallEnvironment.Console.AttachAsync(); await InstallEnvironment.Statistics.AttachAsync(); return ServerState.Installing; } Logger.LogTrace("Install environment exists but is not running, ignoring"); } // Runtime Logger.LogTrace("Checking for existing runtime environment"); RuntimeEnvironment = await RuntimeEnvironmentService.FindAsync(Uuid); RuntimeStorage = await RuntimeStorageService.FindAsync(Uuid); if (RuntimeEnvironment != null) { var isRunning = await RuntimeEnvironment.IsRunningAsync(); if (isRunning) { Logger.LogTrace("Found running runtime environment, reattaching"); RuntimeEnvironment.Console.OnOutput += OnConsoleMessageAsync; RuntimeEnvironment.Statistics.OnStatisticsReceived += OnStatisticsReceivedAsync; RuntimeEnvironment.OnExited += OnRuntimeExitedAsync; await RuntimeEnvironment.Console.AttachAsync(); await RuntimeEnvironment.Statistics.AttachAsync(); // TODO: Use string online check here return ServerState.Online; } Logger.LogTrace("Runtime environment exists but is not running, ignoring"); } Logger.LogTrace("No running environments found"); return ServerState.Offline; } }