refactor main, and fix bugs from initial manual run

Signed-off-by: Ava Affine <ava@sunnypup.io>
This commit is contained in:
Ava Apples Affine 2025-01-13 15:45:07 -08:00
parent 55f9725af1
commit fed49ba3cb
6 changed files with 66 additions and 35 deletions

48
main.go
View file

@ -3,8 +3,11 @@ package main
import (
"flag"
"log"
"os"
"os/signal"
"gitlab.com/whom/bingobot/internal/activity"
"gitlab.com/whom/bingobot/internal/confession"
"gitlab.com/whom/bingobot/internal/config"
"gitlab.com/whom/bingobot/internal/discord"
"gitlab.com/whom/bingobot/internal/logging"
@ -28,6 +31,7 @@ func main() {
logging.Init()
flag.Parse()
logging.Info("startup: initializing state engine")
if err := state.Init(
config.Get().InMemoryEventCacheSize,
config.Get().PersistentCacheStore,
@ -35,35 +39,45 @@ func main() {
log.Fatalf("couldn't initialize state engine: %s", err.Error())
}
logging.Info("startup: starting activity module")
if err := activity.Start(); err != nil {
// TODO: handle gracefully and continue?
log.Fatalf("failed to start activity module: %s", err.Error())
}
if err := web.Start(); err != nil {
log.Fatalf("failed to start local web server: %s", err.Error())
logging.Info("startup: starting confession module")
if err := confession.Start(); err != nil {
// TODO: handle gracefully and continue?
log.Fatalf("failed to start confession module: %s", err.Error())
}
logging.Info("startup: starting web module")
go func() {
if err := web.Start(); err != nil {
log.Fatalf("failed to start local web server: %s", err.Error())
}
}()
// start this LAST
logging.Info("startup: starting state engine")
if err := state.Start(); err != nil {
log.Fatalf("failed to start state machine: %s", err.Error())
}
defer state.Teardown()
if err := startBot(); err != nil {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func(){
for _ = range c {
state.Teardown()
discord.Close()
os.Exit(1)
}
}()
logging.Info("startup: connecting to discord")
if err := discord.Connect(*token); 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
for {}
}