From 5d500b20e53731d48d84a198f2ab27d195b80c5d Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 29 Jan 2024 04:57:11 -0600 Subject: [PATCH] Handle commands during bot shutdown, move redis setup into initRedis func --- main.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 80af798..9cac4a7 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ var ( baseURL string // Base URL for all requests to the banner system environment string CentralTimeLocation *time.Location + isClosing bool = false ) const ( @@ -77,7 +78,7 @@ func init() { baseURL = os.Getenv("BANNER_BASE_URL") } -func main() { +func initRedis() { // Setup redis redisUrl := GetFirstEnv("REDIS_URL", "REDIS_PRIVATE_URL") if redisUrl == "" { @@ -122,8 +123,13 @@ func main() { log.Debug().Str("ping", pong).Msg("Redis connection successful") break } +} + +func main() { + initRedis() // Create cookie jar + var err error cookies, err = cookiejar.New(nil) if err != nil { log.Err(err).Msg("Cannot create cookie jar") @@ -150,6 +156,15 @@ func main() { // Setup command handlers session.AddHandler(func(internalSession *discordgo.Session, interaction *discordgo.InteractionCreate) { + // Handle commands during restart (highly unlikely, but just in case) + if isClosing { + err := RespondError(internalSession, interaction.Interaction, "Bot is currently restarting, try again later.", nil) + if err != nil { + log.Error().Err(err).Msg("Failed to respond with restart error feedback") + } + return + } + name := interaction.ApplicationCommandData().Name if handler, ok := commandHandlers[name]; ok { // Build dict of options for the log @@ -277,6 +292,8 @@ func main() { // Wait for signal (indefinite) <-stop + isClosing = true + // Defers are called after this log.Warn().Msg("Gracefully shutting down") }