Fixed race condition with restart to make sure it only happens after the listener closes.

This commit is contained in:
TheUbMunster 2022-07-28 00:11:40 -06:00
parent 068cc7c06d
commit d2c8c8d3cd
1 changed files with 15 additions and 7 deletions

View File

@ -11,6 +11,7 @@ using Timer = System.Timers.Timer;
Server.Server server = new Server.Server();
HashSet<int> shineBag = new HashSet<int>();
CancellationTokenSource cts = new CancellationTokenSource();
bool restartRequested = false;
Task listenTask = server.Listen(cts.Token);
Logger consoleLogger = new Logger("Console");
DiscordBot bot = new DiscordBot();
@ -551,12 +552,7 @@ CommandHandler.RegisterCommand("restartserver", args =>
else
{
consoleLogger.Info("Received restartserver command");
string? path = System.Reflection.Assembly.GetEntryAssembly()?.GetName().Name;
const string unableToStartMsg = "Unable to ascertain the executable location, you'll need to re-run the server manually.";
if (path != null)
Console.WriteLine($"Running: {System.Diagnostics.Process.Start(path)?.Id.ToString() ?? unableToStartMsg}");
else
consoleLogger.Info(unableToStartMsg);
restartRequested = true;
cts.Cancel();
return "restarting...";
}
@ -585,4 +581,16 @@ Task.Run(() => {
}
});
await listenTask;
await listenTask;
if (restartRequested) //need to do this here because this needs to happen after the listener closes, and there isn't an
//easy way to sync in the restartserver command without it exiting Main()
{
string? path = System.Reflection.Assembly.GetEntryAssembly()?.GetName().Name;
const string unableToStartMsg = "Unable to ascertain the executable location, you'll need to re-run the server manually.";
if (path != null) //path is probably just "Server", but in the context of the assembly, that's all you need to restart it.
{
Console.WriteLine($"Running (pid): {System.Diagnostics.Process.Start(path)?.Id.ToString() ?? unableToStartMsg}");
}
else
consoleLogger.Info(unableToStartMsg);
}