mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-30 00:22:40 +01:00
Stop the service when the host watchdog exits
This commit is contained in:
@@ -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
|
||||
/// <inheritdoc />
|
||||
public void WriteEntry(string message, EventLogEntryType type, int eventID, short category) => EventLog.WriteEntry(message, type, eventID, category);
|
||||
|
||||
/// <summary>
|
||||
/// Executes the <see cref="watchdog"/>, stopping the service if it exits
|
||||
/// </summary>
|
||||
/// <param name="args">The arguments for the <see cref="watchdog"/></param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
|
||||
/// <returns>A <see cref="Task"/> representing the running operation</returns>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[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);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
Reference in New Issue
Block a user