-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
63 lines (58 loc) · 2.36 KB
/
Program.cs
File metadata and controls
63 lines (58 loc) · 2.36 KB
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
using ResourceMonitorCli.Models;
using ResourceMonitorCli.Services;
namespace ResourceMonitorCli
{
internal static class Program
{
private static async Task Main(string?[] args)
{
var config = ConfigurationService.ParseCommandLineArguments(args);
if (config == null) return;
var metricsService = new MetricsService();
if (await metricsService.Initialize() == false) return;
using var cancellationTokenSource = new CancellationTokenSource();
await RunMonitor(config, metricsService, cancellationTokenSource);
}
private static async Task RunMonitor(MonitorConfig config, MetricsService metricsService, CancellationTokenSource cancellationTokenSource)
{
// If Telegram mode is enabled, start a background task to send messages.
var telegramTask = Task.CompletedTask;
if (config.TelegramMode && !string.IsNullOrWhiteSpace(config.TelegramToken) && !string.IsNullOrWhiteSpace(config.ChatId))
{
var telegramService = new TelegramService(config.TelegramToken!, config.ChatId!, metricsService);
telegramTask = Task.Run(() => telegramService.StartSenderAsync(config.IntervalMinutes, cancellationTokenSource.Token),
cancellationTokenSource.Token);
}
try
{
if (!config.TelegramMode)
{
var consoleService = new ConsoleService(metricsService);
await consoleService.RunConsoleMode(cancellationTokenSource.Token);
}
else
{
// In Telegram mode, simply wait (the background task sends messages at the set interval).
await telegramTask;
}
}
catch (TaskCanceledException)
{
// Graceful shutdown.
}
catch (Exception ex)
{
await Console.Error.WriteLineAsync($"Unhandled error in main loop: {ex.Message}");
}
finally
{
await cancellationTokenSource.CancelAsync();
try
{
await telegramTask;
}
catch (TaskCanceledException) { }
}
}
}
}