From 068cc7c06db288cc63cce11482d418c0d602d72c Mon Sep 17 00:00:00 2001 From: TheUbMunster <66451362+TheUbMunster@users.noreply.github.com> Date: Wed, 27 Jul 2022 23:56:50 -0600 Subject: [PATCH 1/4] Added restart server command --- Server/Program.cs | 20 ++++++++++++++++++++ Server/Server.cs | 1 + 2 files changed, 21 insertions(+) diff --git a/Server/Program.cs b/Server/Program.cs index 045a05d..72a24a7 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -542,6 +542,26 @@ CommandHandler.RegisterCommand("loadsettings", _ => { return "Loaded settings.json"; }); +CommandHandler.RegisterCommand("restartserver", args => +{ + if (args.Length != 0) + { + return "Usage: restartserver (no arguments)"; + } + 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); + cts.Cancel(); + return "restarting..."; + } +}); + Console.CancelKeyPress += (_, e) => { e.Cancel = true; consoleLogger.Info("Received Ctrl+C"); diff --git a/Server/Server.cs b/Server/Server.cs index f312ea3..9a271cf 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -55,6 +55,7 @@ public class Server { } Logger.Info("Server closed"); + Console.WriteLine("\n\n\n"); //for the sake of the restart command. } } From d2c8c8d3cdfdd5e34b18a026e35c5e188bf16ede Mon Sep 17 00:00:00 2001 From: TheUbMunster <66451362+TheUbMunster@users.noreply.github.com> Date: Thu, 28 Jul 2022 00:11:40 -0600 Subject: [PATCH 2/4] Fixed race condition with restart to make sure it only happens after the listener closes. --- Server/Program.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Server/Program.cs b/Server/Program.cs index 72a24a7..57b6ef9 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -11,6 +11,7 @@ using Timer = System.Timers.Timer; Server.Server server = new Server.Server(); HashSet shineBag = new HashSet(); 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; \ No newline at end of file +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); +} \ No newline at end of file From 12c84792e8dede67c12da3e9627259b78b6e1ac7 Mon Sep 17 00:00:00 2001 From: TheUbMunster <66451362+TheUbMunster@users.noreply.github.com> Date: Thu, 28 Jul 2022 17:57:56 -0600 Subject: [PATCH 3/4] Changed "Running (pid)" to "Server Running on (pid)" --- Server/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/Program.cs b/Server/Program.cs index 57b6ef9..4a94b13 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -589,7 +589,7 @@ if (restartRequested) //need to do this here because this needs to happen after 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}"); + Console.WriteLine($"Server Running on (pid): {System.Diagnostics.Process.Start(path)?.Id.ToString() ?? unableToStartMsg}"); } else consoleLogger.Info(unableToStartMsg); From 3a8e7ffb938af35ae1448015569169e43563be2c Mon Sep 17 00:00:00 2001 From: TheUbMunster <66451362+TheUbMunster@users.noreply.github.com> Date: Thu, 28 Jul 2022 17:57:56 -0600 Subject: [PATCH 4/4] Changed "Running (pid)" to "Server Running on (pid)" --- Server/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Server/Program.cs b/Server/Program.cs index 57b6ef9..a35910f 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -554,7 +554,7 @@ CommandHandler.RegisterCommand("restartserver", args => consoleLogger.Info("Received restartserver command"); restartRequested = true; cts.Cancel(); - return "restarting..."; + return "Restarting..."; } }); @@ -589,7 +589,7 @@ if (restartRequested) //need to do this here because this needs to happen after 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}"); + Console.WriteLine($"Server Running on (pid): {System.Diagnostics.Process.Start(path)?.Id.ToString() ?? unableToStartMsg}"); } else consoleLogger.Info(unableToStartMsg);