Started implementing server share backend. Redesigned server authorization for api calls. Refactored controller names for servers. Moved some responses to correct namespace
This commit is contained in:
@@ -8,6 +8,8 @@ namespace MoonlightServers.Daemon.ServerSystem.SubSystems;
|
||||
|
||||
public class StatsSubSystem : ServerSubSystem
|
||||
{
|
||||
public ServerStats CurrentStats { get; private set; }
|
||||
|
||||
private readonly DockerClient DockerClient;
|
||||
private readonly IHubContext<ServerWebSocketHub> HubContext;
|
||||
|
||||
@@ -20,6 +22,8 @@ public class StatsSubSystem : ServerSubSystem
|
||||
{
|
||||
DockerClient = dockerClient;
|
||||
HubContext = hubContext;
|
||||
|
||||
CurrentStats = new();
|
||||
}
|
||||
|
||||
public Task Attach(string containerId)
|
||||
@@ -44,6 +48,9 @@ public class StatsSubSystem : ServerSubSystem
|
||||
{
|
||||
var stats = ConvertToStats(response);
|
||||
|
||||
// Update current stats for usage of other components
|
||||
CurrentStats = stats;
|
||||
|
||||
await HubContext.Clients
|
||||
.Group(Configuration.Id.ToString())
|
||||
.SendAsync("StatsUpdated", stats);
|
||||
@@ -66,6 +73,9 @@ public class StatsSubSystem : ServerSubSystem
|
||||
}
|
||||
}
|
||||
|
||||
// Reset current stats
|
||||
CurrentStats = new();
|
||||
|
||||
Logger.LogDebug("Stopped fetching container stats");
|
||||
});
|
||||
|
||||
@@ -76,20 +86,16 @@ public class StatsSubSystem : ServerSubSystem
|
||||
{
|
||||
var result = new ServerStats();
|
||||
|
||||
// When killed this field will be null so we just return
|
||||
if (response.CPUStats.CPUUsage == null)
|
||||
return result;
|
||||
|
||||
#region CPU
|
||||
|
||||
if(response.CPUStats is { CPUUsage.PercpuUsage: not null }) // Sometimes some values are just null >:/
|
||||
|
||||
if(response.CPUStats != null && response.PreCPUStats.CPUUsage != null) // Sometimes some values are just null >:/
|
||||
{
|
||||
var cpuDelta = (float)response.CPUStats.CPUUsage.TotalUsage - response.PreCPUStats.CPUUsage.TotalUsage;
|
||||
var cpuSystemDelta = (float)response.CPUStats.SystemUsage - response.PreCPUStats.SystemUsage;
|
||||
|
||||
var cpuCoreCount = (int)response.CPUStats.OnlineCPUs;
|
||||
|
||||
if (cpuCoreCount == 0)
|
||||
if (cpuCoreCount == 0 && response.CPUStats.CPUUsage.PercpuUsage != null)
|
||||
cpuCoreCount = response.CPUStats.CPUUsage.PercpuUsage.Count;
|
||||
|
||||
var cpuPercent = 0f;
|
||||
@@ -115,10 +121,13 @@ public class StatsSubSystem : ServerSubSystem
|
||||
|
||||
#region Network
|
||||
|
||||
foreach (var network in response.Networks)
|
||||
if (response.Networks != null)
|
||||
{
|
||||
result.NetworkRead += network.Value.RxBytes;
|
||||
result.NetworkWrite += network.Value.TxBytes;
|
||||
foreach (var network in response.Networks)
|
||||
{
|
||||
result.NetworkRead += network.Value.RxBytes;
|
||||
result.NetworkWrite += network.Value.TxBytes;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -92,6 +92,14 @@ public class StorageSubSystem : ServerSubSystem
|
||||
|
||||
public async Task Reinitialize()
|
||||
{
|
||||
if (IsInitialized && StateMachine.State != ServerState.Offline)
|
||||
{
|
||||
throw new HttpApiException(
|
||||
"Unable to reinitialize storage sub system while the server is not offline",
|
||||
400
|
||||
);
|
||||
}
|
||||
|
||||
IsInitialized = false;
|
||||
|
||||
await EnsureRuntimeVolumeCreated();
|
||||
@@ -303,6 +311,22 @@ public class StorageSubSystem : ServerSubSystem
|
||||
if (existingDiskInfo.Exists)
|
||||
{
|
||||
var expectedSize = ByteConverter.FromMegaBytes(Configuration.Disk).Bytes;
|
||||
|
||||
// If the disk size matches, we are done here
|
||||
if (expectedSize == existingDiskInfo.Length)
|
||||
{
|
||||
Logger.LogDebug("Virtual disk size matches expected size");
|
||||
return;
|
||||
}
|
||||
|
||||
// We cant resize while the server is running as this would lead to possible file corruptions
|
||||
// and crashes of the software the server is running
|
||||
if (StateMachine.State != ServerState.Offline)
|
||||
{
|
||||
Logger.LogDebug("Skipping disk resizing while server is not offline");
|
||||
await ConsoleSubSystem.WriteMoonlight("Skipping disk resizing as the server is not offline");
|
||||
return;
|
||||
}
|
||||
|
||||
if (expectedSize > existingDiskInfo.Length)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user