Fix the implementation of -r in service project

This commit is contained in:
Jordan Dominion
2023-06-25 21:00:31 -04:00
parent 13d9a92abb
commit 5e80f0259f
+33 -18
View File
@@ -45,10 +45,10 @@ namespace Tgstation.Server.Host.Service
public bool Detach { get; }
/// <summary>
/// The --resume or -r option.
/// The --restart or -r option.
/// </summary>
[Option(ShortName = "r")]
public bool Resume { get; }
public bool Restart { get; }
/// <summary>
/// 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;
}
/// <summary>
/// Restarts a service using a given <paramref name="serviceController"/>.
/// </summary>
/// <param name="serviceController">The <see cref="ServiceController"/> for the service to restart.</param>
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);
}
/// <summary>
/// Runs the host application with the setup wizard.
/// </summary>