Fixed race condition with restart to make sure it only happens after the listener closes.
This commit is contained in:
parent
068cc7c06d
commit
d2c8c8d3cd
|
@ -11,6 +11,7 @@ using Timer = System.Timers.Timer;
|
||||||
Server.Server server = new Server.Server();
|
Server.Server server = new Server.Server();
|
||||||
HashSet<int> shineBag = new HashSet<int>();
|
HashSet<int> shineBag = new HashSet<int>();
|
||||||
CancellationTokenSource cts = new CancellationTokenSource();
|
CancellationTokenSource cts = new CancellationTokenSource();
|
||||||
|
bool restartRequested = false;
|
||||||
Task listenTask = server.Listen(cts.Token);
|
Task listenTask = server.Listen(cts.Token);
|
||||||
Logger consoleLogger = new Logger("Console");
|
Logger consoleLogger = new Logger("Console");
|
||||||
DiscordBot bot = new DiscordBot();
|
DiscordBot bot = new DiscordBot();
|
||||||
|
@ -551,12 +552,7 @@ CommandHandler.RegisterCommand("restartserver", args =>
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
consoleLogger.Info("Received restartserver command");
|
consoleLogger.Info("Received restartserver command");
|
||||||
string? path = System.Reflection.Assembly.GetEntryAssembly()?.GetName().Name;
|
restartRequested = true;
|
||||||
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);
|
|
||||||
cts.Cancel();
|
cts.Cancel();
|
||||||
return "restarting...";
|
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);
|
||||||
|
}
|
Loading…
Reference in New Issue