appservice: start presence heartbeat before plugin init so bot shows online from boot

This commit is contained in:
prosolis
2026-07-04 10:36:44 -07:00
parent d3f42009f7
commit 0f82a088f9
3 changed files with 45 additions and 18 deletions

39
main.go
View File

@@ -77,6 +77,30 @@ func main() {
}
client := sess.Client
// ---- Process lifecycle + graceful shutdown ----
// Set up the run context and signal handling before the (slow ~2min) plugin
// init so the presence heartbeat can start immediately. scheduler is wired
// later; the signal handler nil-checks it.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var scheduler *cron.Cron
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigCh
slog.Info("shutting down", "signal", sig)
if scheduler != nil {
scheduler.Stop()
}
sess.Stop()
cancel()
}()
// Show the bot online from boot (appservice mode; no-op under masdevice, where
// /sync refreshes presence implicitly). Safe before the listener — outbound only.
sess.StartPresence(ctx)
// Create plugin registry
registry := bot.NewRegistry()
@@ -330,7 +354,7 @@ func main() {
})
// ---- Set up cron scheduler ----
scheduler := cron.New(cron.WithChain(cron.Recover(cronLogger{})))
scheduler = cron.New(cron.WithChain(cron.Recover(cronLogger{})))
setupScheduledJobs(scheduler, client, wotdPlugin, holidaysPlugin, gamingPlugin, birthdayPlugin, animePlugin, moviesPlugin, concertsPlugin, esteemedPlugin, forexPlugin, minifluxPlugin, marketPlugin)
scheduler.Start()
@@ -340,19 +364,6 @@ func main() {
// ---- Start receiving events ----
slog.Info("GogoBee starting", "auth_mode", envOr("AUTH_MODE", "masdevice"))
ctx, cancel := context.WithCancel(context.Background())
// Graceful shutdown
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigCh
slog.Info("shutting down", "signal", sig)
scheduler.Stop()
sess.Stop()
cancel()
}()
if err := sess.Run(ctx); err != nil {
slog.Error("event loop stopped", "err", err)
}