From 5e80f0259fd5f409f9d201737779560338884d4c Mon Sep 17 00:00:00 2001 From: Jordan Dominion Date: Sun, 25 Jun 2023 19:32:20 -0400 Subject: [PATCH] Fix the implementation of `-r` in service project --- src/Tgstation.Server.Host.Service/Program.cs | 51 +++++++++++++------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/src/Tgstation.Server.Host.Service/Program.cs b/src/Tgstation.Server.Host.Service/Program.cs index f2f78bc9a8..0ff0dda89c 100644 --- a/src/Tgstation.Server.Host.Service/Program.cs +++ b/src/Tgstation.Server.Host.Service/Program.cs @@ -45,10 +45,10 @@ namespace Tgstation.Server.Host.Service public bool Detach { get; } /// - /// The --resume or -r option. + /// The --restart or -r option. /// [Option(ShortName = "r")] - public bool Resume { get; } + public bool Restart { get; } /// /// The --install or -i option. @@ -145,7 +145,9 @@ namespace Tgstation.Server.Host.Service if (Configure) await RunConfigure(CancellationToken.None); // DCT: None available + bool stopped = false; if (Uninstall) + { using (var installer = new ServiceInstaller()) { installer.Context = new InstallContext("tgs-uninstall.log", null); @@ -156,21 +158,25 @@ namespace Tgstation.Server.Host.Service installer.Uninstall(null); } - if (Install) - { - RunServiceInstall(); + stopped = true; } + if (Install) + stopped |= RunServiceInstall(); + if (standardRun) { using var service = new ServerService(WatchdogFactory, Trace ? LogLevel.Trace : Debug ? LogLevel.Debug : LogLevel.Information); ServiceBase.Run(service); } - if (Resume) + if (Restart) foreach (ServiceController sc in ServiceController.GetServices()) if (sc.ServiceName == ServerService.Name) { + if (!stopped) + RestartService(sc); + sc.Start(); break; } @@ -195,18 +201,7 @@ namespace Tgstation.Server.Host.Service return false; // is this needed after exit? // Stop it first to give it some cleanup time - if (sc.Status == ServiceControllerStatus.Running) - { - if (Detach) - sc.ExecuteCommand( - ServerService.GetCommand( - PipeCommands.CommandDetachingShutdown) - .Value); - else - sc.Stop(); - - sc.WaitForStatus(ServiceControllerStatus.Stopped); - } + RestartService(sc); // And remove it using var serviceInstaller = new ServiceInstaller(); @@ -241,6 +236,26 @@ namespace Tgstation.Server.Host.Service return serviceStopped; } + /// + /// Restarts a service using a given . + /// + /// The for the service to restart. + void RestartService(ServiceController serviceController) + { + if (serviceController.Status != ServiceControllerStatus.Running) + return; + + if (Detach) + serviceController.ExecuteCommand( + ServerService.GetCommand( + PipeCommands.CommandDetachingShutdown) + .Value); + else + serviceController.Stop(); + + serviceController.WaitForStatus(ServiceControllerStatus.Stopped); + } + /// /// Runs the host application with the setup wizard. ///