1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-11 15:52:25 -05:00

fixed some async startup stuff and made things nicer

This commit is contained in:
RaidMax
2017-08-08 23:35:23 -05:00
parent 5e9b11ed64
commit 00bade57a7
8 changed files with 135 additions and 98 deletions

View File

@ -64,43 +64,6 @@ namespace IW4MAdmin
public void Init()
{
#region CONFIG
var Configs = Directory.EnumerateFiles("config/servers").Where(x => x.Contains(".cfg"));
if (Configs.Count() == 0)
ServerConfigurationGenerator.Generate();
foreach (var file in Configs)
{
var Conf = ServerConfiguration.Read(file);
var ServerInstance = new IW4MServer(this, Conf);
Task.Run(async () =>
{
try
{
await ServerInstance.Initialize();
Servers.Add(ServerInstance);
// this way we can keep track of execution time and see if problems arise.
var Status = new AsyncStatus(ServerInstance, UPDATE_FREQUENCY);
TaskStatuses.Add(Status);
Logger.WriteVerbose($"Now monitoring {ServerInstance.Hostname}");
}
catch (ServerException e)
{
Logger.WriteWarning($"Not monitoring server {Conf.IP}:{Conf.Port} due to uncorrectable errors");
if (e.GetType() == typeof(DvarException))
Logger.WriteError($"Could not get the dvar value for {(e as DvarException).Data["dvar_name"]} (ensure the server has a map loaded)");
else if (e.GetType() == typeof(NetworkException))
Logger.WriteError(e.Message);
}
});
}
#endregion
#region PLUGINS
SharedLibrary.Plugins.PluginImporter.Load(this);
@ -120,6 +83,50 @@ namespace IW4MAdmin
}
#endregion
#region CONFIG
var Configs = Directory.EnumerateFiles("config/servers").Where(x => x.Contains(".cfg"));
if (Configs.Count() == 0)
ServerConfigurationGenerator.Generate();
foreach (var file in Configs)
{
var Conf = ServerConfiguration.Read(file);
Task.Run(async () =>
{
try
{
var ServerInstance = new IW4MServer(this, Conf);
await ServerInstance.Initialize();
lock (Servers)
{
Servers.Add(ServerInstance);
}
Logger.WriteVerbose($"Now monitoring {ServerInstance.Hostname}");
// this way we can keep track of execution time and see if problems arise.
var Status = new AsyncStatus(ServerInstance, UPDATE_FREQUENCY);
lock (TaskStatuses)
{
TaskStatuses.Add(Status);
}
}
catch (ServerException e)
{
Logger.WriteWarning($"Not monitoring server {Conf.IP}:{Conf.Port} due to uncorrectable errors");
if (e.GetType() == typeof(DvarException))
Logger.WriteError($"Could not get the dvar value for {(e as DvarException).Data["dvar_name"]} (ensure the server has a map loaded)");
else if (e.GetType() == typeof(NetworkException))
Logger.WriteError(e.Message);
}
});
}
#endregion
#region COMMANDS
if ((ClientDatabase as ClientsDB).GetOwner() == null)
Commands.Add(new COwner("owner", "claim ownership of the server", "owner", Player.Permission.User, 0, false));
@ -173,22 +180,23 @@ namespace IW4MAdmin
Running = true;
}
public void Start()
{
while (Running)
{
foreach (var Status in TaskStatuses)
for (int i = 0; i < TaskStatuses.Count; i++)
{
var Status = TaskStatuses[i];
if (Status.RequestedTask == null || Status.RequestedTask.IsCompleted)
{
Status.Update(new Task(() => (Status.Dependant as Server).ProcessUpdatesAsync(Status.GetToken())));
if (Status.RunAverage > 500)
if (Status.RunAverage > 1000)
Logger.WriteWarning($"Update task average execution is longer than desired for {(Status.Dependant as Server).GetIP()}::{(Status.Dependant as Server).GetPort()} [{Status.RunAverage}ms]");
}
}
Thread.Sleep(UPDATE_FREQUENCY);
Thread.Sleep(300);
}
#if !DEBUG
foreach (var S in Servers)
@ -199,7 +207,7 @@ namespace IW4MAdmin
webServiceTask.Stop();
}
public void Stop()
{
Running = false;

View File

@ -293,6 +293,9 @@ namespace IW4MAdmin
public override async Task ExecuteEvent(Event E)
{
if (Throttled)
return;
await ProcessEvent(E);
foreach (IPlugin P in SharedLibrary.Plugins.PluginImporter.ActivePlugins)
@ -341,15 +344,18 @@ namespace IW4MAdmin
#endif
{
if ((DateTime.Now - LastPoll).TotalMinutes < 5 && ConnectionErrors > 1)
if ((DateTime.Now - LastPoll).TotalMinutes < 2 && ConnectionErrors >= 1)
return;
try
{
await PollPlayersAsync();
if (ConnectionErrors > 0)
{
Logger.WriteInfo($"Connection has been reestablished with {IP}:{Port}");
Throttled = false;
}
ConnectionErrors = 0;
LastPoll = DateTime.Now;
}
@ -358,7 +364,11 @@ namespace IW4MAdmin
{
ConnectionErrors++;
if (ConnectionErrors == 1)
{
Logger.WriteError($"{e.Message} {IP}:{Port}, reducing polling rate");
Throttled = true;
}
return;
}
LastMessage = DateTime.Now - start;

Binary file not shown.

View File

@ -4,6 +4,7 @@ CHANGELOG:
-fixed the issue with webfront chat history
-fixed console issue of spamming 'polling rate decreased' when server goes offline
-'unknown' admin in webfront defaults to 'IW4MAdmin'
-streamlined the async server initialization
VERSION 1.3