-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
118 lines (100 loc) · 4.59 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* unified/ban - Management and protection systems
© fabricators SRL, https://fabricators.ltd , https://unifiedban.solutions
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License with our addition
to Section 7 as published in unified/ban's the GitHub repository.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License and the
additional terms along with this program.
If not, see <https://docs.fabricators.ltd/docs/licenses/unifiedban>.
For more information, see Licensing FAQ:
https://docs.fabricators.ltd/docs/licenses/faq */
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Unifiedban.Next.Common;
using Unifiedban.Next.Models;
using Timer = System.Timers.Timer;
namespace Unifiedban.Next.Terminal.Telegram;
internal static class Program
{
private static bool _manualShutdown;
private static readonly Timer Heartbeat = new();
private static readonly TelegramManager _telegramManager = new();
private static readonly RabbitManager _rabbitManager = new();
private static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
AppDomain.CurrentDomain.ProcessExit += CurrentDomainOnProcessExit;
Common.Utils.WriteLine($"== {AppDomain.CurrentDomain.FriendlyName} Startup ==");
var builder = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appsettings.json", false, false);
CacheData.Configuration = builder.Build();
_ = new UBContext(CacheData.Configuration["Database"]);
Common.Utils.WriteLine("Registering instance");
Utils.RegisterInstance();
Common.Utils.WriteLine("***************************************");
//SetHeartbeat();
Common.Utils.WriteLine("***************************************");
Utils.GetModulesQueues();
Common.Utils.WriteLine("***************************************");
CacheData.Load();
Common.Utils.WriteLine("***************************************");
_telegramManager.Init();
Common.Utils.WriteLine("***************************************");
_rabbitManager.Init();
Common.Utils.WriteLine("***************************************");
Task.Run(_telegramManager.Start);
Common.Utils.WriteLine("***************************************");
Task.Run(_rabbitManager.Start);
Common.Utils.WriteLine("***************************************");
Utils.SetInstanceStatus(Enums.States.Operational);
Common.Utils.WriteLine("Startup completed.\n");
Console.ReadLine();
Common.Utils.WriteLine("Manual shutdown started.\n");
_manualShutdown = true;
DoShutdown();
}
private static void SetHeartbeat()
{
Common.Utils.WriteLine($"Starting Hearthbeat " +
$"(every {CacheData.Configuration["UptimeMonitor:Seconds"]} seconds)");
Heartbeat.AutoReset = true;
Heartbeat.Interval = 1000 * int.Parse(CacheData.Configuration["UptimeMonitor:Seconds"]);
Heartbeat.Elapsed += (sender, eventArgs) =>
{
var http = new HttpClient();
http.GetStringAsync(CacheData.Configuration["UptimeMonitor:URL"]);
};
Heartbeat.Start();
}
private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var ex = (e.ExceptionObject as Exception);
Console.WriteLine(ex?.Message);
}
private static void CurrentDomainOnProcessExit(object? sender, EventArgs e)
{
if (_manualShutdown) return;
Common.Utils.WriteLine("SIGTERM shutdown started.\n");
DoShutdown();
}
private static void DoShutdown()
{
Common.Utils.WriteLine("Stopping Heartbeat");
Heartbeat.Stop();
Common.Utils.WriteLine("Stopping Telegram client");
_telegramManager.Stop();
Common.Utils.WriteLine("Closing RabbitMQ connection");
_rabbitManager.Shutdown();
Common.Utils.WriteLine("Deregistering instance");
Utils.DeregisterInstance();
Common.Utils.WriteLine("***************************************");
Common.Utils.WriteLine("Shutdown completed.");
}
}