cs-codex-dist-tests/TestNetRewarder/Program.cs

78 lines
2.4 KiB
C#
Raw Normal View History

2024-01-22 09:27:07 +00:00
using ArgsUniform;
using Logging;
using Utils;
namespace TestNetRewarder
{
public class Program
{
public static Configuration Config { get; private set; } = null!;
public static ILog Log { get; private set; } = null!;
public static CancellationToken CancellationToken { get; private set; }
2024-01-26 23:17:56 +00:00
public static BotClient BotClient { get; private set; } = null!;
2024-01-22 09:27:07 +00:00
public static Task Main(string[] args)
{
var cts = new CancellationTokenSource();
CancellationToken = cts.Token;
Console.CancelKeyPress += (sender, args) => cts.Cancel();
var uniformArgs = new ArgsUniform<Configuration>(PrintHelp, args);
Config = uniformArgs.Parse(true);
Log = new LogSplitter(
new FileLog(Path.Combine(Config.LogPath, "testnetrewarder")),
new ConsoleLog()
);
2024-01-26 23:17:56 +00:00
BotClient = new BotClient(Config, Log);
2024-01-22 09:27:07 +00:00
EnsurePath(Config.DataPath);
EnsurePath(Config.LogPath);
return new Program().MainAsync();
}
public async Task MainAsync()
{
Log.Log("Starting TestNet Rewarder...");
var segmenter = new TimeSegmenter(Log, Config);
while (!CancellationToken.IsCancellationRequested)
{
2024-01-26 23:17:56 +00:00
await EnsureBotOnline();
2024-01-22 09:27:07 +00:00
await segmenter.WaitForNextSegment(ProcessTimeSegment);
await Task.Delay(1000, CancellationToken);
}
}
2024-01-26 23:17:56 +00:00
private async Task EnsureBotOnline()
2024-01-22 09:27:07 +00:00
{
2024-01-26 23:17:56 +00:00
var start = DateTime.UtcNow;
while (! await BotClient.IsOnline() && !CancellationToken.IsCancellationRequested)
2024-01-22 09:27:07 +00:00
{
2024-01-26 23:17:56 +00:00
await Task.Delay(5000);
2024-01-26 22:29:57 +00:00
2024-01-26 23:17:56 +00:00
var elapsed = DateTime.UtcNow - start;
if (elapsed.TotalMinutes > 10)
{
var msg = "Unable to connect to bot for " + Time.FormatDuration(elapsed);
Log.Error(msg);
throw new Exception(msg);
}
2024-01-22 09:27:07 +00:00
}
}
private static void PrintHelp()
{
Log.Log("TestNet Rewarder");
}
private static void EnsurePath(string path)
{
if (Directory.Exists(path)) return;
Directory.CreateDirectory(path);
}
}
}