This commit introduces the ability to reconfigure modules/event subscribers by replaying previously stored events at startup. This way modules/subscribers can always come up with the latest activity and data that was present at close at the last run of the program. This process also includes the disposal of unneeded events to minimize disk use over time. The changes include the following: 1. Event interface is extended with the Disposable() function unimplemented stubs for existing interface implementations 2. state.Init() is split into Init() and Start() Init() now initialized the file used by eventStream, and puts in place a 0 size memory cache as a temporary measure on top of the file. Start() reads Pop()s documents one by one from the temp nil-cache eventStream, it attempts to dispose of each event and if the event is not disposable it is pushed onto a second temporary no-memory-cache buffer which is then reverse ordered. The underlying file is truncated and reopened. Finally, the real eventStream is allocated, with in memory cache. All events in the second buffer are published (sent to subscribers as well as added to the new eventStream). 3. Updates to main() to support Init() vs Start() Signed-off-by: Ava Affine <ava@sunnypup.io>
60 lines
1,017 B
Go
60 lines
1,017 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())
|
|
}
|
|
|
|
// TODO: start modules HERE and not elsewhere
|
|
err = startBot()
|
|
|
|
if err := state.Start(); err != nil {
|
|
log.Fatal("failed to start state machine: %s", err.Error())
|
|
}
|
|
defer state.Teardown()
|
|
|
|
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
|
|
}
|