tgstation-server  4.3.2
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 System;
4 using System.Diagnostics;
5 using System.Globalization;
6 using System.ServiceProcess;
7 using System.Threading;
8 using System.Threading.Tasks;
10 
11 namespace Tgstation.Server.Host.Service
12 {
16  sealed class ServerService : ServiceBase
17  {
21  public const string Name = "tgstation-server-4";
22 
26  readonly IWatchdog watchdog;
27 
32 
36  CancellationTokenSource cancellationTokenSource;
37 
44  public ServerService(IWatchdogFactory watchdogFactory, ILoggerFactory loggerFactory, LogLevel minumumLogLevel)
45  {
46  if (watchdogFactory == null)
47  throw new ArgumentNullException(nameof(watchdogFactory));
48  if (loggerFactory == null)
49  throw new ArgumentNullException(nameof(loggerFactory));
50 
51 #pragma warning disable CS0618 // Type or member is obsolete
52  loggerFactory.AddEventLog(new EventLogSettings
53  {
54  LogName = EventLog.Log,
55  MachineName = EventLog.MachineName,
56  SourceName = EventLog.Source,
57  Filter = (message, logLevel) => logLevel >= minumumLogLevel
58  });
59 #pragma warning restore CS0618 // Type or member is obsolete
60 
61  ServiceName = Name;
62  watchdog = watchdogFactory.CreateWatchdog(loggerFactory);
63  }
64 
71  async Task RunWatchdog(string[] args, CancellationToken cancellationToken)
72  {
73  await watchdog.RunAsync(false, args, cancellationTokenSource.Token).ConfigureAwait(false);
74 
75  void StopServiceAsync()
76  {
77  try
78  {
79  Task.Run(Stop, cancellationToken);
80  }
81  catch (OperationCanceledException) { }
82  catch (Exception e)
83  {
84  EventLog.WriteEntry(String.Format(CultureInfo.InvariantCulture, "Error stopping service! Exception: {0}", e));
85  }
86  }
87 
88  StopServiceAsync();
89  }
90 
92  protected override void Dispose(bool disposing)
93  {
94  cancellationTokenSource?.Dispose();
95  base.Dispose(disposing);
96  }
97 
99  protected override void OnStart(string[] args)
100  {
101  cancellationTokenSource?.Dispose();
102  cancellationTokenSource = new CancellationTokenSource();
103  watchdogTask = RunWatchdog(args, cancellationTokenSource.Token);
104  }
105 
107  protected override void OnStop()
108  {
109  cancellationTokenSource.Cancel();
110  watchdogTask.GetAwaiter().GetResult();
111  }
112  }
113 }
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