Improved reliability of the shutdown process.

This commit is contained in:
flash 2025-04-28 10:46:26 +00:00
commit d94b1cb813
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E
3 changed files with 47 additions and 63 deletions
SharpChat/ClientCommands

View file

@ -2,10 +2,7 @@ using SharpChat.SockChat.S2CPackets;
namespace SharpChat.ClientCommands;
public class ShutdownRestartClientCommand(ManualResetEvent waitHandle, Func<bool> shutdownCheck) : ClientCommand {
private readonly ManualResetEvent WaitHandle = waitHandle ?? throw new ArgumentNullException(nameof(waitHandle));
private readonly Func<bool> ShutdownCheck = shutdownCheck ?? throw new ArgumentNullException(nameof(shutdownCheck));
public class ShutdownRestartClientCommand(CancellationTokenSource cancellationTokenSource) : ClientCommand {
public bool IsMatch(ClientCommandContext ctx) {
return ctx.NameEquals("shutdown")
|| ctx.NameEquals("restart");
@ -18,14 +15,16 @@ public class ShutdownRestartClientCommand(ManualResetEvent waitHandle, Func<bool
return;
}
if(!ShutdownCheck())
if(cancellationTokenSource.IsCancellationRequested)
return;
Logger.Write("Shutdown requested through Sock Chat command...");
if(ctx.NameEquals("restart"))
foreach(Connection conn in ctx.Chat.Connections)
conn.PrepareForRestart();
await ctx.Chat.Update();
WaitHandle?.Set();
await cancellationTokenSource.CancelAsync();
}
}