tgstation-server
The /tg/station 13 server suite
ServerService.cs
Go to the documentation of this file.
1 using Microsoft.Extensions.Logging;
2 using Microsoft.Extensions.Logging.EventLog;
3 using Microsoft.Extensions.Logging.EventLog.Internal;
4 using System;
5 using System.Diagnostics;
6 using System.Diagnostics.CodeAnalysis;
7 using System.Globalization;
8 using System.ServiceProcess;
9 using System.Threading;
10 using System.Threading.Tasks;
12 
13 namespace Tgstation.Server.Host.Service
14 {
19  {
23  public const string Name = "tgstation-server-4";
24 
28  readonly IWatchdog watchdog;
29 
34 
38  CancellationTokenSource cancellationTokenSource;
39 
46  public ServerService(IWatchdogFactory watchdogFactory, ILoggerFactory loggerFactory, LogLevel minumumLogLevel)
47  {
48  if (watchdogFactory == null)
49  throw new ArgumentNullException(nameof(watchdogFactory));
50  if (loggerFactory == null)
51  throw new ArgumentNullException(nameof(loggerFactory));
52 
53  loggerFactory.AddEventLog(new EventLogSettings
54  {
55  EventLog = this,
56  Filter = (message, logLevel) => logLevel >= minumumLogLevel
57  });
58 
59  ServiceName = Name;
60  watchdog = watchdogFactory.CreateWatchdog(loggerFactory);
61  }
62 
64  public int MaxMessageSize => (int)EventLog.MaximumKilobytes * 1024;
65 
67  public void WriteEntry(string message, EventLogEntryType type, int eventID, short category) => EventLog.WriteEntry(message, type, eventID, category);
68 
75  async Task RunWatchdog(string[] args, CancellationToken cancellationToken)
76  {
77  await watchdog.RunAsync(false, args, cancellationToken).ConfigureAwait(false);
78 
79  void StopServiceAsync()
80  {
81  try
82  {
83  Task.Run(Stop, cancellationToken);
84  }
85  catch (OperationCanceledException) { }
86  catch (Exception e)
87  {
88  EventLog.WriteEntry(String.Format(CultureInfo.InvariantCulture, "Error stopping service! Exception: {0}", e));
89  }
90  }
91  StopServiceAsync();
92  }
93 
95  [SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDisposed", MessageId = "cancellationTokenSource")]
96  protected override void Dispose(bool disposing)
97  {
98  cancellationTokenSource?.Dispose();
99  base.Dispose(disposing);
100  }
101 
103  protected override void OnStart(string[] args)
104  {
105  cancellationTokenSource?.Dispose();
106  cancellationTokenSource = new CancellationTokenSource();
107  watchdogTask = RunWatchdog(args, cancellationTokenSource.Token);
108  }
109 
111  protected override void OnStop()
112  {
113  cancellationTokenSource.Cancel();
114  watchdogTask.GetAwaiter().GetResult();
115  }
116  }
117 }
async Task RunWatchdog(string[] args, CancellationToken cancellationToken)
Executes the watchdog, stopping the service if it exits
IWatchdog CreateWatchdog(ILoggerFactory loggerFactory)
Create a IWatchdog
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
The watchdog for a Host
Definition: IWatchdog.cs:9
readonly IWatchdog watchdog
The IWatchdog for the ServerService
Represents a IWatchdog as a ServiceBase
CancellationTokenSource cancellationTokenSource
The cancellationTokenSource for the ServerService