tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ServerService.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Runtime.Versioning;
5using System.ServiceProcess;
6using System.Threading;
7
8using Microsoft.Extensions.Logging;
9using Microsoft.Extensions.Logging.EventLog;
10
13
15{
19 [SupportedOSPlatform("windows")]
21 {
25 public const string Name = Constants.CanonicalPackageName;
26
31
35 readonly Lazy<ILoggerFactory> loggerFactory;
36
40 readonly string[] commandLineArguments;
41
45#pragma warning disable CA2213 // Disposable fields should be disposed
47#pragma warning restore CA2213 // Disposable fields should be disposed
48
55 public ServerService(IWatchdogFactory watchdogFactory, string[] commandLineArguments, LogLevel minimumLogLevel)
56 {
57 this.watchdogFactory = watchdogFactory ?? throw new ArgumentNullException(nameof(watchdogFactory));
58 this.commandLineArguments = commandLineArguments ?? throw new ArgumentNullException(nameof(commandLineArguments));
59
60 ServiceName = Name;
61 loggerFactory = new Lazy<ILoggerFactory>(() => LoggerFactory.Create(builder => builder.AddEventLog(new EventLogSettings
62 {
63 LogName = EventLog.Log,
64 MachineName = EventLog.MachineName,
65 SourceName = EventLog.Source,
66 Filter = (message, logLevel) => logLevel >= minimumLogLevel,
67 })));
68 }
69
73 public void Run() => Run(this);
74
76 protected override void Dispose(bool disposing)
77 {
78 if (disposing)
79 {
80 OnStop();
81
82 if (loggerFactory.IsValueCreated)
83 loggerFactory.Value.Dispose();
84 }
85
86 base.Dispose(disposing);
87 }
88
90 protected override void OnCustomCommand(int command) => serviceLifetime!.HandleCustomCommand(command);
91
93 protected override void OnStart(string[] args)
94 {
95 var newArgs = new List<string>(commandLineArguments.Length + args.Length + 1)
96 {
97 "--General:SetupWizardMode=Never",
98 };
99
100 newArgs.AddRange(commandLineArguments);
101 newArgs.AddRange(args);
102
104 Stop,
105 signalChecker => watchdogFactory.CreateWatchdog(signalChecker, loggerFactory.Value),
106 loggerFactory.Value.CreateLogger<ServiceLifetime>(),
107 newArgs.ToArray());
108 }
109
111 protected override void OnStop()
112 {
113 var oldLifetime = Interlocked.Exchange(ref serviceLifetime, null);
114 oldLifetime?.DisposeAsync().GetAwaiter().GetResult();
115 }
116 }
117}
const string CanonicalPackageName
The name of the project.
Definition Constants.cs:11
Represents a IWatchdog as a ServiceBase.
override void OnCustomCommand(int command)
volatile? ServiceLifetime serviceLifetime
The active ServiceLifetime.
override void OnStart(string[] args)
readonly IWatchdogFactory watchdogFactory
The IWatchdog for the ServerService.
readonly Lazy< ILoggerFactory > loggerFactory
The Lazy<T> ILoggerFactory used by the ServerService.
override void Dispose(bool disposing)
const string Name
The canonical windows service name.
ServerService(IWatchdogFactory watchdogFactory, string[] commandLineArguments, LogLevel minimumLogLevel)
Initializes a new instance of the ServerService class.
readonly string[] commandLineArguments
The Array of command line arguments the service was invoked with.
void Run()
Executes the ServerService.
Represents the lifetime of the service.
void HandleCustomCommand(int command)
Handle a custom service command .
IWatchdog CreateWatchdog(ISignalChecker signalChecker, ILoggerFactory loggerFactory)
Create a IWatchdog.