mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-10-16 07:59:57 +00:00
Better handling of program shutdown
It now knows what a SIGTERM is. Additionally, made some small tweaks to the file's structure and removed some unnecessary details.
This commit is contained in:
parent
fa8bb0abd2
commit
c727bead98
1 changed files with 12 additions and 25 deletions
37
Program.cs
37
Program.cs
|
@ -1,6 +1,6 @@
|
||||||
namespace BirthdayBot;
|
namespace BirthdayBot;
|
||||||
class Program {
|
class Program {
|
||||||
private static ShardManager? _bot;
|
private static ShardManager _bot = null!;
|
||||||
private static readonly DateTimeOffset _botStartTime = DateTimeOffset.UtcNow;
|
private static readonly DateTimeOffset _botStartTime = DateTimeOffset.UtcNow;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -14,11 +14,12 @@ class Program {
|
||||||
cfg = new Configuration();
|
cfg = new Configuration();
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
Console.WriteLine(ex);
|
Console.WriteLine(ex);
|
||||||
Environment.Exit((int)ExitCodes.ConfigError);
|
Environment.Exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.CancelKeyPress += OnCancelKeyPressed;
|
|
||||||
_bot = new ShardManager(cfg);
|
_bot = new ShardManager(cfg);
|
||||||
|
AppDomain.CurrentDomain.ProcessExit += OnCancelEvent;
|
||||||
|
Console.CancelKeyPress += OnCancelEvent;
|
||||||
|
|
||||||
await Task.Delay(-1);
|
await Task.Delay(-1);
|
||||||
}
|
}
|
||||||
|
@ -33,34 +34,20 @@ class Program {
|
||||||
Console.WriteLine($"{ts:s} [{source}] {item}");
|
Console.WriteLine($"{ts:s} [{source}] {item}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void OnCancelKeyPressed(object? sender, ConsoleCancelEventArgs e) {
|
private static bool _shutdownRequested = false;
|
||||||
e.Cancel = true;
|
private static void OnCancelEvent(object? sender, EventArgs e) {
|
||||||
Log("Shutdown", "Captured cancel key; sending shutdown.");
|
if (e is ConsoleCancelEventArgs ce) ce.Cancel = true;
|
||||||
ProgramStop();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool _stopping = false;
|
if (_shutdownRequested) return;
|
||||||
public static void ProgramStop() {
|
_shutdownRequested = true;
|
||||||
if (_stopping) return;
|
|
||||||
_stopping = true;
|
|
||||||
Log(nameof(Program), "Shutting down...");
|
Log(nameof(Program), "Shutting down...");
|
||||||
|
|
||||||
var dispose = Task.Run(_bot!.Dispose);
|
var dispose = Task.Run(_bot.Dispose);
|
||||||
if (!dispose.Wait(30000)) {
|
if (!dispose.Wait(15000)) {
|
||||||
Log(nameof(Program), "Disconnection is taking too long. Will force exit.");
|
Log(nameof(Program), "Disconnection is taking too long. Will force exit.");
|
||||||
Environment.ExitCode &= (int)ExitCodes.ForcedExit;
|
Environment.ExitCode = 1;
|
||||||
}
|
}
|
||||||
Log(nameof(Program), $"Uptime: {BotUptime}");
|
Log(nameof(Program), $"Uptime: {BotUptime}");
|
||||||
Environment.Exit(Environment.ExitCode);
|
Environment.Exit(Environment.ExitCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Flags]
|
|
||||||
public enum ExitCodes {
|
|
||||||
Normal = 0x0,
|
|
||||||
ForcedExit = 0x1,
|
|
||||||
ConfigError = 0x2,
|
|
||||||
DatabaseError = 0x4,
|
|
||||||
DeadShardThreshold = 0x8,
|
|
||||||
BadCommand = 0x10,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue