it should shut down properly now

This commit is contained in:
Elijah R 2024-06-02 13:19:22 -04:00
parent 0864abf309
commit 4a8549dbab
3 changed files with 24 additions and 3 deletions

View file

@ -65,4 +65,10 @@ public class DiscordBot
Utilities.Log(LogLevel.INFO, "Connected to Discord"); Utilities.Log(LogLevel.INFO, "Connected to Discord");
reportChannel = await discord.GetChannelAsync(reportChannelId); reportChannel = await discord.GetChannelAsync(reportChannelId);
} }
public async Task Disconnect()
{
await discord.DisconnectAsync();
Utilities.Log(LogLevel.INFO, "Disconnected from Discord");
}
} }

View file

@ -28,6 +28,12 @@ public class HTTPServer
this.app.MapGet("/api/v1/mod/iptousername/{ip:required}", IPToUsernameHandler); this.app.MapGet("/api/v1/mod/iptousername/{ip:required}", IPToUsernameHandler);
this.app.MapGet("/api/v1/mod/usernametoip/{username:required}", UsernameToIPHandler); this.app.MapGet("/api/v1/mod/usernametoip/{username:required}", UsernameToIPHandler);
this.app.Lifetime.ApplicationStarted.Register(this.onServerStarted); 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<IResult> VMListHandler(HttpContext context) private async Task<IResult> VMListHandler(HttpContext context)
@ -145,4 +151,9 @@ public class HTTPServer
{ {
return this.app.RunAsync(); return this.app.RunAsync();
} }
public Task StopAsync()
{
return this.app.StopAsync();
}
} }

View file

@ -69,9 +69,13 @@ class Program
await t; await t;
} }
public static void Exit() public static async Task Exit()
{ {
Task.WaitAll(VMs.Select(vm => vm.CloseAsync()).ToArray()); var t = new List<Task>();
cts.Cancel(); t.Add(HTTP.StopAsync());
t.Add(Discord.Disconnect());
t.AddRange(VMs.Select(vm => vm.CloseAsync()).ToArray());
await Task.WhenAll(t);
await cts.CancelAsync();
} }
} }