diff --git a/Model/Server.cs b/Model/Server.cs index 34df56c..273602a 100644 --- a/Model/Server.cs +++ b/Model/Server.cs @@ -8,6 +8,7 @@ namespace Model public class Server { public DateTime SpawnedAtUtc { get; set; } = DateTime.UtcNow; + public DateTime LastHeartBeatUtc { get; set; } public int ProcessId { get; set; } public int Port { get; set; } public bool IsStarted { get; set; } diff --git a/Supervisor/NBloodServerListener.cs b/Supervisor/NBloodServerListener.cs index a384266..6dff515 100644 --- a/Supervisor/NBloodServerListener.cs +++ b/Supervisor/NBloodServerListener.cs @@ -66,6 +66,7 @@ namespace Supervisor new Server() { Port = packetData.Port, + LastHeartBeatUtc = DateTime.UtcNow, IsStarted = packetData.IsStarted, CurrentPlayers = packetData.CurrentPlayers, MaximumPlayers = packetData.MaximumPlayers diff --git a/Supervisor/Program.cs b/Supervisor/Program.cs index 27acac4..1107bd2 100644 --- a/Supervisor/Program.cs +++ b/Supervisor/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Threading; using Model; @@ -17,7 +18,20 @@ namespace Supervisor while (true) { - Thread.Sleep(TimeSpan.FromSeconds(1)); + RemoveCrashedServers(); + Thread.Sleep(TimeSpan.FromSeconds(3)); + } + } + + private static void RemoveCrashedServers() + { + var crashedServers = State.Servers.Values + .Where(s => s.IsStarted && DateTime.UtcNow - s.LastHeartBeatUtc < TimeSpan.FromMinutes(15)) + .Select(s => s.Port); + + foreach (var port in crashedServers) + { + State.Servers.TryRemove(port, out _); } } }