Have program attempt to stop itself after 5 failures

This commit is contained in:
Noi 2021-05-30 10:05:33 -07:00
parent 57fc207bba
commit 5fcf69136d
2 changed files with 16 additions and 3 deletions

View file

@ -33,12 +33,18 @@ namespace BirthdayBot
Console.WriteLine($"{ts:u} [{source}] {item}"); Console.WriteLine($"{ts:u} [{source}] {item}");
} }
private static bool _dispose = false;
private static void OnCancelKeyPressed(object sender, ConsoleCancelEventArgs e) private static void OnCancelKeyPressed(object sender, ConsoleCancelEventArgs e)
{ {
e.Cancel = true; e.Cancel = true;
if (_dispose) return; ProgramStop();
_dispose = true; }
private static bool _stopping = false;
public static void ProgramStop()
{
if (_stopping) return;
_stopping = true;
var dispose = Task.Run(_bot.Dispose); var dispose = Task.Run(_bot.Dispose);
if (!dispose.Wait(90000)) if (!dispose.Wait(90000))
{ {

View file

@ -25,6 +25,11 @@ namespace BirthdayBot
/// </summary> /// </summary>
private const int WatchdogInterval = 90; private const int WatchdogInterval = 90;
/// <summary>
/// Number of shards allowed to be destroyed before forcing the program to close.
/// </summary>
private const int MaxDestroyedShards = 5;
/// <summary> /// <summary>
/// A dictionary with shard IDs as its keys and shard instances as its values. /// A dictionary with shard IDs as its keys and shard instances as its values.
/// When initialized, all keys will be created as configured. If an instance is removed, /// When initialized, all keys will be created as configured. If an instance is removed,
@ -215,7 +220,9 @@ namespace BirthdayBot
{ {
_shards[dead].Dispose(); _shards[dead].Dispose();
_shards[dead] = null; _shards[dead] = null;
_destroyedShards++;
} }
if (_destroyedShards > MaxDestroyedShards) Program.ProgramStop();
// Start up any missing shards // Start up any missing shards
int startAllowance = 4; int startAllowance = 4;