diff --git a/EmperorPalpatine/DiscordBot.cs b/EmperorPalpatine/DiscordBot.cs index f6794eb..7056993 100644 --- a/EmperorPalpatine/DiscordBot.cs +++ b/EmperorPalpatine/DiscordBot.cs @@ -65,4 +65,10 @@ public class DiscordBot Utilities.Log(LogLevel.INFO, "Connected to Discord"); reportChannel = await discord.GetChannelAsync(reportChannelId); } + + public async Task Disconnect() + { + await discord.DisconnectAsync(); + Utilities.Log(LogLevel.INFO, "Disconnected from Discord"); + } } \ No newline at end of file diff --git a/EmperorPalpatine/HTTPServer.cs b/EmperorPalpatine/HTTPServer.cs index 80c3bcf..e0b64fa 100644 --- a/EmperorPalpatine/HTTPServer.cs +++ b/EmperorPalpatine/HTTPServer.cs @@ -28,6 +28,12 @@ public class HTTPServer this.app.MapGet("/api/v1/mod/iptousername/{ip:required}", IPToUsernameHandler); this.app.MapGet("/api/v1/mod/usernametoip/{username:required}", UsernameToIPHandler); this.app.Lifetime.ApplicationStarted.Register(this.onServerStarted); + this.app.Lifetime.ApplicationStopping.Register(onServerStopping); + } + + private void onServerStopping() + { + Utilities.Log(LogLevel.INFO, "HTTP server is shutting down..."); } private async Task VMListHandler(HttpContext context) @@ -145,4 +151,9 @@ public class HTTPServer { return this.app.RunAsync(); } + + public Task StopAsync() + { + return this.app.StopAsync(); + } } \ No newline at end of file diff --git a/EmperorPalpatine/Program.cs b/EmperorPalpatine/Program.cs index fe591dd..7b5b0f2 100644 --- a/EmperorPalpatine/Program.cs +++ b/EmperorPalpatine/Program.cs @@ -69,9 +69,13 @@ class Program await t; } - public static void Exit() + public static async Task Exit() { - Task.WaitAll(VMs.Select(vm => vm.CloseAsync()).ToArray()); - cts.Cancel(); + var t = new List(); + t.Add(HTTP.StopAsync()); + t.Add(Discord.Disconnect()); + t.AddRange(VMs.Select(vm => vm.CloseAsync()).ToArray()); + await Task.WhenAll(t); + await cts.CancelAsync(); } } \ No newline at end of file