From 18cb9715e463249dac793d616379b27efc4b8ef7 Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Tue, 22 Nov 2022 20:33:50 +0800 Subject: [PATCH] fix: handle exit signal properly --- main.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 17e1000..dbc00b8 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,8 @@ import ( "syscall" "go.uber.org/zap" + + "git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/utils" ) var logger *zap.SugaredLogger @@ -26,16 +28,21 @@ func runBot() { logger.Fatalw("Failed to initialize bot", "err", err) } - go bot.Start() - logger.Info("Bot started") + botFinCh := utils.WaitFor(bot.Start) + logger.Infow("Bot started", "username", bot.Me.Username) // listen for shutdown signal sigCh := make(chan os.Signal, 1) signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) - <-sigCh - logger.Info("Bot shutdown...") - bot.Stop() + select { + case sig := <-sigCh: + logger.Infow("Received signal", "signal", sig) + logger.Info("Bot shutdown...") + bot.Stop() + case <-botFinCh: + logger.Warn("Bot terminated") + } } func main() {