-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathService.cs
57 lines (43 loc) · 1.56 KB
/
Service.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using BSS.Logging;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
#pragma warning disable CS1998
namespace Server
{
internal sealed class Service : BackgroundService
{
internal static volatile Boolean Shutdown = false;
internal static volatile Boolean UngracefulShutdown = false;
private static readonly ManualResetEvent _worker = new(false);
private static Int32 Stopping = 0;
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
Log.FastLog("Received start signal", LogSeverity.Info, "Service");
#if DEBUG
Log.Debug("Hooked to CancelKeyPress", "Service");
Console.CancelKeyPress += (s, e) => InternalShutdown();
#endif
Thread.CurrentThread.Name = "Main Worker Thread";
Program.Run();
_worker.Set();
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
cancellationToken.Register(() => UngracefulShutdown = true);
InternalShutdown();
}
internal static void InternalShutdown()
{
if (Stopping == 1) return;
Interlocked.Add(ref Stopping, 1);
Shutdown = true;
IPC.IpcActionHandle?.Set();
Worker.CloseListeners();
_worker.WaitOne();
Log.Debug("Worker fully stopped - exiting", "Service");
Program.Exit.Set();
}
}
}