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>
This commit is contained in:
Ava Apples Affine 2024-11-30 10:49:16 -08:00
parent 9d3ca54ad4
commit d818e8c158
2 changed files with 22 additions and 8 deletions

10
main.go
View file

@ -3,8 +3,6 @@ package main
import (
"flag"
"log"
"os"
"os/signal"
"gitlab.com/whom/bingobot/internal/config"
"gitlab.com/whom/bingobot/internal/discord"
@ -17,8 +15,6 @@ var (
)
func main() {
flag.Parse()
var err error
err = config.Init()
@ -28,6 +24,7 @@ func main() {
}
logging.Init()
flag.Parse()
if err := state.Init(
config.Get().InMemoryEventCacheSize,
@ -35,6 +32,7 @@ func main() {
); err != nil {
log.Fatalf("couldn't initialize state engine: %s", err.Error())
}
defer state.Teardown()
err = startBot()
@ -50,10 +48,6 @@ func startBot() error {
return err
}
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt)
<-sigch
logging.Info("shutting down gracefully", "type", "shutdown")
discord.Close()