Implemented more life cycle handling. Added support for rootless environments
This commit is contained in:
@@ -1,12 +1,62 @@
|
||||
using MoonCore.Helpers;
|
||||
using MoonlightServers.Daemon.Configuration;
|
||||
using MoonlightServers.Daemon.Helpers;
|
||||
using MoonlightServers.Daemon.Models.Cache;
|
||||
using MoonlightServers.DaemonShared.Enums;
|
||||
|
||||
namespace MoonlightServers.Daemon.Models;
|
||||
|
||||
public class Server
|
||||
{
|
||||
public ILogger Logger { get; set; }
|
||||
public ServerConsole Console { get; set; }
|
||||
public IServiceProvider ServiceProvider { get; set; }
|
||||
public ServerState State => StateMachine.CurrentState;
|
||||
public StateMachine<ServerState> StateMachine { get; set; }
|
||||
public ServerConfiguration Configuration { get; set; }
|
||||
public string? ContainerId { get; set; }
|
||||
|
||||
// This can be used to stop streaming when the server gets destroyed or something
|
||||
public CancellationTokenSource Cancellation { get; set; }
|
||||
|
||||
#region Small helpers
|
||||
|
||||
public string RuntimeContainerName => $"moonlight-runtime-{Configuration.Id}";
|
||||
public string InstallContainerName => $"moonlight-install-{Configuration.Id}";
|
||||
|
||||
public string RuntimeVolumePath
|
||||
{
|
||||
get
|
||||
{
|
||||
var appConfig = ServiceProvider.GetRequiredService<AppConfiguration>();
|
||||
|
||||
var localPath = PathBuilder.Dir(
|
||||
appConfig.Storage.Volumes,
|
||||
Configuration.Id.ToString()
|
||||
);
|
||||
|
||||
var absolutePath = Path.GetFullPath(localPath);
|
||||
|
||||
return absolutePath;
|
||||
}
|
||||
}
|
||||
|
||||
public string InstallVolumePath
|
||||
{
|
||||
get
|
||||
{
|
||||
var appConfig = ServiceProvider.GetRequiredService<AppConfiguration>();
|
||||
|
||||
var localPath = PathBuilder.Dir(
|
||||
appConfig.Storage.Install,
|
||||
Configuration.Id.ToString()
|
||||
);
|
||||
|
||||
var absolutePath = Path.GetFullPath(localPath);
|
||||
|
||||
return absolutePath;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
41
MoonlightServers.Daemon/Models/ServerConsole.cs
Normal file
41
MoonlightServers.Daemon/Models/ServerConsole.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
namespace MoonlightServers.Daemon.Models;
|
||||
|
||||
public class ServerConsole
|
||||
{
|
||||
public event Func<string, Task> OnOutput;
|
||||
public event Func<string, Task> OnInput;
|
||||
|
||||
public string[] Messages => GetMessages();
|
||||
private readonly Queue<string> MessageCache = new();
|
||||
private const int MaxMessagesInCache = 250; //TODO: Config
|
||||
|
||||
public async Task WriteToOutput(string content)
|
||||
{
|
||||
lock (MessageCache)
|
||||
{
|
||||
MessageCache.Enqueue(content);
|
||||
|
||||
if (MessageCache.Count > MaxMessagesInCache)
|
||||
MessageCache.Dequeue();
|
||||
}
|
||||
|
||||
if (OnOutput != null)
|
||||
{
|
||||
await OnOutput
|
||||
.Invoke(content)
|
||||
.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task WriteToInput(string content)
|
||||
{
|
||||
if (OnInput != null)
|
||||
await OnInput.Invoke(content);
|
||||
}
|
||||
|
||||
private string[] GetMessages()
|
||||
{
|
||||
lock (MessageCache)
|
||||
return MessageCache.ToArray();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace MoonlightServers.Daemon.Models;
|
||||
|
||||
public enum ServerState
|
||||
{
|
||||
Offline = 0,
|
||||
Starting = 1,
|
||||
Online = 2,
|
||||
Stopping = 3,
|
||||
Installing = 4
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
namespace MoonlightServers.Daemon.Models;
|
||||
|
||||
public enum ServerTask
|
||||
{
|
||||
None = 0,
|
||||
CreatingStorage = 1,
|
||||
PullingDockerImage = 2,
|
||||
RemovingContainer = 3,
|
||||
CreatingContainer = 4,
|
||||
StartingContainer = 5
|
||||
}
|
||||
Reference in New Issue
Block a user