remove crashed servers from the list (#2)

This commit is contained in:
CommonLoon102 2020-01-25 15:59:28 +00:00 committed by GitHub
parent 1efbc1784b
commit 8313a88942
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 1 deletions

View File

@ -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; }

View File

@ -66,6 +66,7 @@ namespace Supervisor
new Server()
{
Port = packetData.Port,
LastHeartBeatUtc = DateTime.UtcNow,
IsStarted = packetData.IsStarted,
CurrentPlayers = packetData.CurrentPlayers,
MaximumPlayers = packetData.MaximumPlayers

View File

@ -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 _);
}
}
}