2020-10-29 07:53:18 +00:00
|
|
|
|
using BirthdayBot.Data;
|
|
|
|
|
using System;
|
2020-04-02 18:27:55 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2021-10-18 07:26:25 +00:00
|
|
|
|
namespace BirthdayBot;
|
2020-04-02 18:27:55 +00:00
|
|
|
|
|
2021-10-18 07:26:25 +00:00
|
|
|
|
class Program {
|
|
|
|
|
private static ShardManager _bot;
|
|
|
|
|
public static DateTimeOffset BotStartTime { get; } = DateTimeOffset.UtcNow;
|
|
|
|
|
|
|
|
|
|
static async Task Main() {
|
|
|
|
|
var cfg = new Configuration();
|
|
|
|
|
try {
|
2020-10-29 07:53:18 +00:00
|
|
|
|
await Database.DoInitialDatabaseSetupAsync();
|
2021-10-18 07:26:25 +00:00
|
|
|
|
} catch (Npgsql.NpgsqlException e) {
|
|
|
|
|
Console.WriteLine("Error when attempting to connect to database: " + e.Message);
|
|
|
|
|
Environment.Exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.CancelKeyPress += OnCancelKeyPressed;
|
|
|
|
|
_bot = new ShardManager(cfg);
|
2020-10-29 07:53:18 +00:00
|
|
|
|
|
2021-10-18 07:26:25 +00:00
|
|
|
|
await Task.Delay(-1);
|
|
|
|
|
}
|
2020-04-02 18:27:55 +00:00
|
|
|
|
|
2021-10-18 07:26:25 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a formatted message to console.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void Log(string source, string message) {
|
|
|
|
|
var ts = DateTime.UtcNow;
|
|
|
|
|
var ls = new string[] { "\r\n", "\n" };
|
|
|
|
|
foreach (var item in message.Split(ls, StringSplitOptions.None))
|
|
|
|
|
Console.WriteLine($"{ts:u} [{source}] {item}");
|
|
|
|
|
}
|
2020-04-02 18:27:55 +00:00
|
|
|
|
|
2021-10-18 07:26:25 +00:00
|
|
|
|
private static void OnCancelKeyPressed(object sender, ConsoleCancelEventArgs e) {
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
Log("Shutdown", "Captured cancel key; sending shutdown.");
|
|
|
|
|
ProgramStop();
|
|
|
|
|
}
|
2020-04-02 18:27:55 +00:00
|
|
|
|
|
2021-10-18 07:26:25 +00:00
|
|
|
|
private static bool _stopping = false;
|
|
|
|
|
public static void ProgramStop() {
|
|
|
|
|
if (_stopping) return;
|
|
|
|
|
_stopping = true;
|
|
|
|
|
Log("Shutdown", "Commencing shutdown...");
|
2021-05-30 17:05:33 +00:00
|
|
|
|
|
2021-10-18 07:26:25 +00:00
|
|
|
|
var dispose = Task.Run(_bot.Dispose);
|
|
|
|
|
if (!dispose.Wait(90000)) {
|
|
|
|
|
Log("Shutdown", "Normal shutdown has not concluded after 90 seconds. Will force quit.");
|
|
|
|
|
Environment.ExitCode += 0x200;
|
2020-04-02 18:27:55 +00:00
|
|
|
|
}
|
2021-10-18 07:26:25 +00:00
|
|
|
|
Environment.Exit(Environment.ExitCode);
|
2020-04-02 18:27:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|