mirror of
https://github.com/NoiTheCat/WorldTime.git
synced 2024-11-21 14:34:36 +00:00
Merge pull request #21 from NoiTheCat/dev/graceful-shutdown
Better handling of program shutdown
This commit is contained in:
commit
2861b579b4
1 changed files with 17 additions and 30 deletions
47
Program.cs
47
Program.cs
|
@ -1,7 +1,7 @@
|
||||||
namespace WorldTime;
|
namespace WorldTime;
|
||||||
|
|
||||||
class Program {
|
class Program {
|
||||||
private static WorldTime? _bot;
|
private static WorldTime _bot = null!;
|
||||||
private static readonly DateTimeOffset _botStartTime = DateTimeOffset.UtcNow;
|
private static readonly DateTimeOffset _botStartTime = DateTimeOffset.UtcNow;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -15,14 +15,15 @@ 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 WorldTime(cfg);
|
_bot = new WorldTime(cfg);
|
||||||
await _bot.StartAsync().ConfigureAwait(false);
|
AppDomain.CurrentDomain.ProcessExit += OnCancelEvent;
|
||||||
|
Console.CancelKeyPress += OnCancelEvent;
|
||||||
|
|
||||||
await Task.Delay(-1).ConfigureAwait(false);
|
await _bot.StartAsync();
|
||||||
|
await Task.Delay(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -35,33 +36,19 @@ 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();
|
|
||||||
}
|
if (_shutdownRequested) return;
|
||||||
|
_shutdownRequested = true;
|
||||||
|
Log("Shutdown", "Shutting down...");
|
||||||
|
|
||||||
private static bool _stopping = false;
|
var dispose = Task.Run(_bot.Dispose);
|
||||||
public static void ProgramStop() {
|
if (!dispose.Wait(15000)) {
|
||||||
if (_stopping) return;
|
Log("Shutdown", "Normal shutdown is taking too long. Will force quit.");
|
||||||
_stopping = true;
|
Environment.ExitCode = 1;
|
||||||
Log("Shutdown", "Commencing shutdown...");
|
|
||||||
|
|
||||||
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 &= (int)ExitCodes.ForcedExit;
|
|
||||||
}
|
}
|
||||||
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