1 using Microsoft.Extensions.Logging;
2 using Microsoft.Extensions.Logging.EventLog;
6 using System.ServiceProcess;
8 using System.Threading.Tasks;
21 public const string Name =
"tgstation-server-4";
46 if (watchdogFactory == null)
47 throw new ArgumentNullException(nameof(watchdogFactory));
48 if (loggerFactory == null)
49 throw new ArgumentNullException(nameof(loggerFactory));
51 #pragma warning disable CS0618 // Type or member is obsolete 52 loggerFactory.AddEventLog(
new EventLogSettings
54 LogName = EventLog.Log,
55 MachineName = EventLog.MachineName,
56 SourceName = EventLog.Source,
57 Filter = (message, logLevel) => logLevel >= minumumLogLevel
59 #pragma warning restore CS0618 // Type or member is obsolete 71 async Task
RunWatchdog(
string[] args, CancellationToken cancellationToken)
73 await watchdog.RunAsync(
false, args, cancellationTokenSource.Token).ConfigureAwait(
false);
75 void StopServiceAsync()
79 Task.Run(Stop, cancellationToken);
81 catch (OperationCanceledException) { }
84 EventLog.WriteEntry(String.Format(CultureInfo.InvariantCulture,
"Error stopping service! Exception: {0}", e));
92 protected override void Dispose(
bool disposing)
94 cancellationTokenSource?.Dispose();
95 base.Dispose(disposing);
99 protected override void OnStart(
string[] args)
101 cancellationTokenSource?.Dispose();
102 cancellationTokenSource =
new CancellationTokenSource();
103 watchdogTask = RunWatchdog(args, cancellationTokenSource.Token);
109 cancellationTokenSource.Cancel();
110 watchdogTask.GetAwaiter().GetResult();
async Task RunWatchdog(string[] args, CancellationToken cancellationToken)
Executes the watchdog, stopping the service if it exits
IWatchdog CreateWatchdog(ILoggerFactory loggerFactory)
Create a IWatchdog
Use server authentication
override void OnStart(string[] args)
Task watchdogTask
The Task recieved from IWatchdog.RunAsync(bool, string[], CancellationToken) of watchdog ...
override void Dispose(bool disposing)
ServerService(IWatchdogFactory watchdogFactory, ILoggerFactory loggerFactory, LogLevel minumumLogLevel)
Construct a ServerService
Factory for creating IWatchdogs
readonly IWatchdog watchdog
The IWatchdog for the ServerService
Represents a IWatchdog as a ServiceBase
CancellationTokenSource cancellationTokenSource
The cancellationTokenSource for the ServerService