bingobot/main.go
Ava Affine d818e8c158 Handle interrupts and panics to mitigate data loss
This commit introduces a deferred call to a state teardown function
This is needed to properly flush data to the backing documentbuffer
as well as to truncate its underlying store. In doing so we make
sure data loss from process termination is minimal to nil.

when ever a panic happens or a signal is thrown the call to Teardown() is made

Signed-off-by: Ava Affine <ava@sunnypup.io>
2024-12-05 16:44:18 -08:00

55 lines
865 B
Go

package main
import (
"flag"
"log"
"gitlab.com/whom/bingobot/internal/config"
"gitlab.com/whom/bingobot/internal/discord"
"gitlab.com/whom/bingobot/internal/logging"
"gitlab.com/whom/bingobot/internal/state"
)
var (
token = flag.String("token", "", "Bot authentication token")
)
func main() {
var err error
err = config.Init()
if err != nil {
log.Fatal(err)
}
logging.Init()
flag.Parse()
if err := state.Init(
config.Get().InMemoryEventCacheSize,
config.Get().PersistentCacheStore,
); err != nil {
log.Fatalf("couldn't initialize state engine: %s", err.Error())
}
defer state.Teardown()
err = startBot()
if err != nil {
log.Fatal(err)
}
}
func startBot() error {
err := discord.Connect(*token)
if err != nil {
return err
}
logging.Info("shutting down gracefully", "type", "shutdown")
discord.Close()
return nil
}