NBloodServerSupervisor/Supervisor/PrivateServerManager.cs

73 lines
1.9 KiB
C#
Raw Normal View History

2020-01-23 21:03:54 +01:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
using Common;
2020-01-23 21:03:54 +01:00
namespace Supervisor
{
static class PrivateServerManager
{
public static void Start()
{
Task.Factory.StartNew(() =>
{
while (true)
{
TempMapFolderCleanup();
Thread.Sleep(TimeSpan.FromSeconds(10));
2020-01-23 21:03:54 +01:00
KillUnusedServers();
}
});
}
private static void KillUnusedServers()
{
2020-01-27 17:25:45 +01:00
try
{
var killables = Program.State.Servers.Values.Where(s =>
s.IsPrivate
&& !s.IsStarted
&& s.CurrentPlayers < 2
&& (DateTime.UtcNow - s.SpawnedAtUtc) > TimeSpan.FromMinutes(10));
2020-01-23 21:03:54 +01:00
2020-01-27 17:25:45 +01:00
foreach (var server in killables)
{
Process.GetProcessById(server.ProcessId).Kill();
}
}
catch
2020-01-23 21:03:54 +01:00
{
// Log...
}
}
private static void TempMapFolderCleanup()
{
try
{
if (!Directory.Exists(CommandLineUtils.TempMapDir))
{
Directory.CreateDirectory(CommandLineUtils.TempMapDir);
}
foreach (var dir in Directory.GetDirectories(CommandLineUtils.TempMapDir))
{
if (DateTime.UtcNow - File.GetCreationTimeUtc(dir) > TimeSpan.FromDays(1))
{
Directory.Delete(dir, recursive: true);
}
}
}
catch
{
// Log...
2020-01-23 21:03:54 +01:00
}
}
}
}