This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathProgram.cs
72 lines (65 loc) · 2.72 KB
/
Program.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Threading.Tasks;
using CommandLine;
using Improbable.OnlineServices.Base.Server;
using Improbable.OnlineServices.Common.Analytics;
using Improbable.OnlineServices.Common.Analytics.ExceptionHandlers;
using Mono.Unix;
using Mono.Unix.Native;
using Serilog;
using Serilog.Formatting.Compact;
namespace Improbable.OnlineServices.SampleMatcher
{
class MatcherArgs : CommandLineArgs, IAnalyticsCommandLineArgs
{
public string Endpoint { get; set; }
public bool AllowInsecureEndpoints { get; set; }
public string ConfigPath { get; set; }
public string GcpKeyPath { get; set; }
public string Environment { get; set; }
public string EventSchema { get; set; }
}
class Program
{
static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(new RenderedCompactJsonFormatter())
.Enrich.FromLogContext()
.CreateLogger();
Parser.Default.ParseArguments<MatcherArgs>(args)
.WithParsed(parsedArgs =>
{
IAnalyticsSender analyticsSender = new AnalyticsSenderBuilder("gateway_matcher")
.WithCommandLineArgs(parsedArgs)
.With(new LogExceptionStrategy(Log.Logger))
.Build();
var matcher = new Matcher(analyticsSender);
var matcherTask = new Task(() => { matcher.Start(); });
var unixSignalTask = new Task<int>(() =>
{
return UnixSignal.WaitAny(new[]
{new UnixSignal(Signum.SIGINT), new UnixSignal(Signum.SIGTERM)});
});
matcherTask.Start();
Console.WriteLine("Matcher started up");
unixSignalTask.Start();
Task.WaitAny(matcherTask, unixSignalTask);
if (unixSignalTask.IsCompleted)
{
Console.WriteLine($"Received UNIX signal {unixSignalTask.Result}");
Console.WriteLine("Matcher shutting down...");
matcher.Stop();
matcherTask.Wait(TimeSpan.FromSeconds(10));
Console.WriteLine("Matcher stopped cleanly");
}
else
{
/* The matcher task has completed; we can just exit. */
Console.WriteLine("The matcher has stopped itself or encountered an unhandled exception.");
}
Environment.Exit(0);
});
}
}
}