tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
Program.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Runtime.InteropServices;
6using System.Threading;
7using System.Threading.Tasks;
8
9using Microsoft.Extensions.Hosting.Systemd;
10using Microsoft.Extensions.Logging;
11
15
17{
21 static class Program
22 {
26 internal static IWatchdogFactory WatchdogFactory { get; set; }
27
31 static Program()
32 {
34 }
35
41 internal static async Task<int> Main(string[] args)
42 {
43 System.Console.Title = $"{Constants.CanonicalPackageName} Host Watchdog v{Assembly.GetExecutingAssembly().GetName().Version?.Semver()}";
44
45 var arguments = new List<string>(args);
46 var trace = arguments.Remove("--trace-host-watchdog");
47 var debug = arguments.Remove("--debug-host-watchdog");
48
49 const string SystemDArg = "--Internal:UsingSystemD=true";
50 if (!arguments.Any(arg => arg.Equals(SystemDArg, StringComparison.OrdinalIgnoreCase))
51 && SystemdHelpers.IsSystemdService())
52 arguments.Add(SystemDArg);
53
54 using var loggerFactory = LoggerFactory.Create(builder =>
55 {
56 if (trace)
57 builder.SetMinimumLevel(LogLevel.Trace);
58 else if (debug)
59 builder.SetMinimumLevel(LogLevel.Debug);
60
61 builder.AddConsole();
62 });
63
64 var logger = loggerFactory.CreateLogger(nameof(Program));
65 try
66 {
67 if (trace && debug)
68 {
69 logger.LogCritical("Please specify only 1 of --trace-host-watchdog or --debug-host-watchdog!");
70 return 2;
71 }
72
73 using var cts = new CancellationTokenSource();
74 void AppDomainHandler(object? a, EventArgs b) => cts.Cancel();
75 AppDomain.CurrentDomain.ProcessExit += AppDomainHandler;
76 try
77 {
78 System.Console.CancelKeyPress += (a, b) =>
79 {
80 b.Cancel = true;
81 cts.Cancel();
82 };
83
84 var watchdog = WatchdogFactory.CreateWatchdog(
85 RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
86 ? new NoopSignalChecker()
87 : new PosixSignalChecker(
88 loggerFactory.CreateLogger<PosixSignalChecker>()),
89 loggerFactory);
90
91 return await watchdog.RunAsync(false, arguments.ToArray(), cts.Token)
92 ? 0
93 : 1;
94 }
95 finally
96 {
97 AppDomain.CurrentDomain.ProcessExit -= AppDomainHandler;
98 }
99 }
100 catch (Exception ex)
101 {
102 logger.LogCritical(ex, "Failed to run!");
103 return 3;
104 }
105 }
106 }
107}
Contains the entrypoint for the application.
Definition Program.cs:22
static Program()
Initializes static members of the Program class.
Definition Program.cs:31
Entrypoint for the Process.
Definition Program.cs:20
IWatchdog CreateWatchdog(ISignalChecker signalChecker, ILoggerFactory loggerFactory)
Create a IWatchdog.A new IWatchdog.
ValueTask< bool > RunAsync(bool runConfigure, string[] args, CancellationToken cancellationToken)
Run the IWatchdog.