fix: handle exit signal properly
This commit is contained in:
parent
b73e378de7
commit
18cb9715e4
17
main.go
17
main.go
|
@ -6,6 +6,8 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
|
"git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var logger *zap.SugaredLogger
|
var logger *zap.SugaredLogger
|
||||||
|
@ -26,16 +28,21 @@ func runBot() {
|
||||||
logger.Fatalw("Failed to initialize bot", "err", err)
|
logger.Fatalw("Failed to initialize bot", "err", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go bot.Start()
|
botFinCh := utils.WaitFor(bot.Start)
|
||||||
logger.Info("Bot started")
|
logger.Infow("Bot started", "username", bot.Me.Username)
|
||||||
|
|
||||||
// listen for shutdown signal
|
// listen for shutdown signal
|
||||||
sigCh := make(chan os.Signal, 1)
|
sigCh := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
||||||
<-sigCh
|
|
||||||
|
|
||||||
logger.Info("Bot shutdown...")
|
select {
|
||||||
bot.Stop()
|
case sig := <-sigCh:
|
||||||
|
logger.Infow("Received signal", "signal", sig)
|
||||||
|
logger.Info("Bot shutdown...")
|
||||||
|
bot.Stop()
|
||||||
|
case <-botFinCh:
|
||||||
|
logger.Warn("Bot terminated")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
Loading…
Reference in New Issue