bail on error immediately

This commit is contained in:
Basique Evangelist 2021-10-17 14:46:51 +03:00
parent 6ff22680b9
commit 58f7e17ba6
Signed by untrusted user: BasiqueEvangelist
GPG Key ID: B370219149301706
2 changed files with 11 additions and 11 deletions

View File

@ -59,14 +59,15 @@ namespace JetKarmaBot
public async Task Stop() public async Task Stop()
{ {
if (stopped) return; if (stopped) return;
Client.StopReceiving(); Client?.StopReceiving();
timeoutWaitTaskToken.Cancel(); timeoutWaitTaskToken?.Cancel();
try try
{ {
if (timeoutWaitTask != null)
await timeoutWaitTask; await timeoutWaitTask;
} }
catch (OperationCanceledException) { } catch (OperationCanceledException) { }
await Timeout.Save(); await Timeout?.Save();
Dispose(); Dispose();
stopped = true; stopped = true;
} }
@ -119,8 +120,8 @@ namespace JetKarmaBot
public void Dispose() public void Dispose()
{ {
timeoutWaitTaskToken.Dispose(); timeoutWaitTaskToken?.Dispose();
timeoutWaitTask.Dispose(); timeoutWaitTask?.Dispose();
} }
#endregion #endregion

View File

@ -42,14 +42,13 @@ namespace JetKarmaBot
try try
{ {
bot.Init().Wait(); bot.Init().GetAwaiter().GetResult();
log.Info("JetKarmaBot started. Press Ctrl-C to exit..."); log.Info("JetKarmaBot started. Press Ctrl-C to exit...");
Environment.ExitCode = (int)ExitCode.ErrorRunning;
} }
catch (Exception ex) catch (Exception ex)
{ {
log.Error(ex); log.Error(ex);
Environment.ExitCode = (int)ExitCode.ErrorException; return (int)ExitCode.ErrorException;
} }
ManualResetEvent quitEvent = new ManualResetEvent(false); ManualResetEvent quitEvent = new ManualResetEvent(false);
try try
@ -62,14 +61,14 @@ namespace JetKarmaBot
AppDomain.CurrentDomain.ProcessExit += (sender, args) => AppDomain.CurrentDomain.ProcessExit += (sender, args) =>
{ {
log.Info("Received stop request, waiting for exit..."); log.Info("Received stop request, waiting for exit...");
bot?.Stop()?.Wait(); bot?.Stop()?.GetAwaiter().GetResult();
}; };
} }
catch { } catch { }
quitEvent.WaitOne(Timeout.Infinite); quitEvent.WaitOne(Timeout.Infinite);
log.Info("Waiting for exit..."); log.Info("Waiting for exit...");
bot?.Stop()?.Wait(); bot?.Stop()?.GetAwaiter().GetResult();
return (int)ExitCode.Ok; return (int)ExitCode.Ok;
} }