From be040f16413c388e00a3836147877a8bf6166c9b Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Fri, 5 Oct 2018 12:41:02 -0400 Subject: [PATCH] Stop the service when the host watchdog exits --- .../ServerService.cs | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Tgstation.Server.Host.Service/ServerService.cs b/src/Tgstation.Server.Host.Service/ServerService.cs index 1d34049e31..0758c021a3 100644 --- a/src/Tgstation.Server.Host.Service/ServerService.cs +++ b/src/Tgstation.Server.Host.Service/ServerService.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Logging.EventLog.Internal; using System; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Globalization; using System.ServiceProcess; using System.Threading; using System.Threading.Tasks; @@ -65,6 +66,31 @@ namespace Tgstation.Server.Host.Service /// public void WriteEntry(string message, EventLogEntryType type, int eventID, short category) => EventLog.WriteEntry(message, type, eventID, category); + /// + /// Executes the , stopping the service if it exits + /// + /// The arguments for the + /// The for the operation + /// A representing the running operation + async Task RunWatchdog(string[] args, CancellationToken cancellationToken) + { + await watchdog.RunAsync(false, args, cancellationTokenSource.Token).ConfigureAwait(false); + + void StopServiceAsync() + { + try + { + Task.Run(Stop, cancellationToken); + } + catch (OperationCanceledException) { } + catch (Exception e) + { + EventLog.WriteEntry(String.Format(CultureInfo.InvariantCulture, "Error stopping service! Exception: {0}", e)); + } + } + StopServiceAsync(); + } + /// [SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDisposed", MessageId = "cancellationTokenSource")] protected override void Dispose(bool disposing) @@ -78,7 +104,7 @@ namespace Tgstation.Server.Host.Service { cancellationTokenSource?.Dispose(); cancellationTokenSource = new CancellationTokenSource(); - watchdogTask = watchdog.RunAsync(false, args, cancellationTokenSource.Token); + watchdogTask = RunWatchdog(args, cancellationTokenSource.Token); } ///