From 4ac4f3f9870a71bb16d92ed535ff3440a09678ec Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Wed, 2 May 2018 16:41:10 -0400 Subject: [PATCH] Watchdog for life --- .../Components/DreamDaemonExecutor.cs | 10 +++++----- .../Components/IDreamDaemonExecutor.cs | 3 ++- .../Components/IInstanceManager.cs | 2 +- .../Components/IInstanceShutdownHandler.cs | 10 ++++++++++ .../Components/IInstanceShutdownMethod.cs | 11 ----------- .../Components/InstanceManager.cs | 10 +++++++--- src/Tgstation.Server.Host/Components/Watchdog.cs | 2 +- 7 files changed, 26 insertions(+), 22 deletions(-) create mode 100644 src/Tgstation.Server.Host/Components/IInstanceShutdownHandler.cs delete mode 100644 src/Tgstation.Server.Host/Components/IInstanceShutdownMethod.cs diff --git a/src/Tgstation.Server.Host/Components/DreamDaemonExecutor.cs b/src/Tgstation.Server.Host/Components/DreamDaemonExecutor.cs index c56c2a6e4c..f8b74ce401 100644 --- a/src/Tgstation.Server.Host/Components/DreamDaemonExecutor.cs +++ b/src/Tgstation.Server.Host/Components/DreamDaemonExecutor.cs @@ -33,9 +33,9 @@ namespace Tgstation.Server.Host.Components } /// - /// The for the + /// The for the /// - readonly IInstanceShutdownMethod instanceShutdownMethod; + readonly IInstanceShutdownHandler instanceShutdownMethod; /// /// The for the /// @@ -46,14 +46,14 @@ namespace Tgstation.Server.Host.Components /// /// The value of /// The value of - public DreamDaemonExecutor(IInstanceShutdownMethod instanceShutdownMethod, IIOManager ioManager) + public DreamDaemonExecutor(IInstanceShutdownHandler instanceShutdownMethod, IIOManager ioManager) { this.instanceShutdownMethod = instanceShutdownMethod ?? throw new ArgumentNullException(nameof(instanceShutdownMethod)); this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager)); } /// - public async Task RunDreamDaemon(DreamDaemonLaunchParameters launchParameters, TaskCompletionSource onSuccessfulStartup, string dreamDaemonPath, string dmbPath, string accessToken, bool usePrimaryPort, CancellationToken cancellationToken) + public async Task RunDreamDaemon(DreamDaemonLaunchParameters launchParameters, TaskCompletionSource onSuccessfulStartup, string dreamDaemonPath, string dmbPath, string accessToken, bool usePrimaryPort, bool alwaysKill, CancellationToken cancellationToken) { using (var proc = new Process()) { @@ -85,7 +85,7 @@ namespace Tgstation.Server.Host.Components } finally { - if (!instanceShutdownMethod.GracefulShutdown) + if (!alwaysKill && !await instanceShutdownMethod.PreserveActiveExecutablesIfNecessary(launchParameters, accessToken, proc.Id, usePrimaryPort).ConfigureAwait(false)) { proc.Kill(); proc.WaitForExit(); diff --git a/src/Tgstation.Server.Host/Components/IDreamDaemonExecutor.cs b/src/Tgstation.Server.Host/Components/IDreamDaemonExecutor.cs index 5ed6534bbf..c6e444cecd 100644 --- a/src/Tgstation.Server.Host/Components/IDreamDaemonExecutor.cs +++ b/src/Tgstation.Server.Host/Components/IDreamDaemonExecutor.cs @@ -18,8 +18,9 @@ namespace Tgstation.Server.Host.Components /// The path to the .dmb to run, the working directory will be derived from this /// The access token to be used for communication /// If the server should open on or of + /// If the resulting process should never be left alive /// The for the operation /// A representing the lifetime of the process and resulting in the exit code - Task RunDreamDaemon(DreamDaemonLaunchParameters launchParameters, TaskCompletionSource onSuccessfulStartup, string dreamDaemonPath, string dmbPath, string accessToken, bool usePrimaryPort, CancellationToken cancellationToken); + Task RunDreamDaemon(DreamDaemonLaunchParameters launchParameters, TaskCompletionSource onSuccessfulStartup, string dreamDaemonPath, string dmbPath, string accessToken, bool usePrimaryPort, bool alwaysKill, CancellationToken cancellationToken); } } diff --git a/src/Tgstation.Server.Host/Components/IInstanceManager.cs b/src/Tgstation.Server.Host/Components/IInstanceManager.cs index 356a2fd9c8..080ab59d06 100644 --- a/src/Tgstation.Server.Host/Components/IInstanceManager.cs +++ b/src/Tgstation.Server.Host/Components/IInstanceManager.cs @@ -6,7 +6,7 @@ namespace Tgstation.Server.Host.Components /// /// For managing s /// - interface IInstanceManager : IInstanceShutdownMethod + interface IInstanceManager : IInstanceShutdownHandler { /// /// Get the associated with given diff --git a/src/Tgstation.Server.Host/Components/IInstanceShutdownHandler.cs b/src/Tgstation.Server.Host/Components/IInstanceShutdownHandler.cs new file mode 100644 index 0000000000..e07de394b6 --- /dev/null +++ b/src/Tgstation.Server.Host/Components/IInstanceShutdownHandler.cs @@ -0,0 +1,10 @@ +using System.Threading.Tasks; +using Tgstation.Server.Api.Models.Internal; + +namespace Tgstation.Server.Host.Components +{ + interface IInstanceShutdownHandler + { + Task PreserveActiveExecutablesIfNecessary(DreamDaemonLaunchParameters launchParameters, string accessToken, int pid, bool primary); + } +} diff --git a/src/Tgstation.Server.Host/Components/IInstanceShutdownMethod.cs b/src/Tgstation.Server.Host/Components/IInstanceShutdownMethod.cs deleted file mode 100644 index 14ced74d67..0000000000 --- a/src/Tgstation.Server.Host/Components/IInstanceShutdownMethod.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Tgstation.Server.Host.Components -{ - interface IInstanceShutdownMethod - { - bool GracefulShutdown { get; set; } - } -} diff --git a/src/Tgstation.Server.Host/Components/InstanceManager.cs b/src/Tgstation.Server.Host/Components/InstanceManager.cs index ae48802658..1032aff8fa 100644 --- a/src/Tgstation.Server.Host/Components/InstanceManager.cs +++ b/src/Tgstation.Server.Host/Components/InstanceManager.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using Tgstation.Server.Api.Models.Internal; using Tgstation.Server.Host.Core; using Tgstation.Server.Host.Models; @@ -14,9 +15,6 @@ namespace Tgstation.Server.Host.Components /// sealed class InstanceManager : IInstanceManager, IHostedService { - /// - public bool GracefulShutdown { get; set; } - /// /// The for the /// @@ -131,5 +129,11 @@ namespace Tgstation.Server.Host.Components await Task.WhenAll(instances.Select(x => x.Value.StopAsync(cancellationToken))).ConfigureAwait(false); instances.Clear(); } + + /// + public Task PreserveActiveExecutablesIfNecessary(DreamDaemonLaunchParameters launchParameters, string accessToken, int pid, bool primary) + { + throw new NotImplementedException(); + } } } diff --git a/src/Tgstation.Server.Host/Components/Watchdog.cs b/src/Tgstation.Server.Host/Components/Watchdog.cs index 3213641a92..71688ed6fe 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog.cs @@ -78,7 +78,7 @@ namespace Tgstation.Server.Host.Components async Task RunServer(DreamDaemonLaunchParameters launchParameters, TaskCompletionSource onSuccessfulStartup, string accessToken, string dreamDaemonPath, bool isPrimary, CancellationToken cancellationToken) { using (var dmb = await dmbFactory.LockNextDmb(cancellationToken).ConfigureAwait(false)) - return await dreamDaemonExecutor.RunDreamDaemon(launchParameters, onSuccessfulStartup, dreamDaemonPath, String.Concat(dmb.PrimaryDirectory, dmb.DmbName), accessToken, isPrimary, cancellationToken).ConfigureAwait(false); + return await dreamDaemonExecutor.RunDreamDaemon(launchParameters, onSuccessfulStartup, dreamDaemonPath, String.Concat(dmb.PrimaryDirectory, dmb.DmbName), accessToken, isPrimary, false, cancellationToken).ConfigureAwait(false); } ///