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);
}
///