mess around with being able to reload router

This commit is contained in:
Joel Wetzell
2026-02-07 13:42:55 -06:00
parent 2104d9f1a9
commit 914f6aa833

View File

@@ -9,6 +9,7 @@ import (
"os/signal" "os/signal"
"slices" "slices"
"sync" "sync"
"syscall"
"github.com/jwetzell/showbridge-go" "github.com/jwetzell/showbridge-go"
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
@@ -23,7 +24,8 @@ import (
) )
var ( var (
version = "dev" version = "dev"
sigHangup = make(chan os.Signal, 1)
) )
func main() { func main() {
@@ -71,6 +73,8 @@ func main() {
} }
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
signal.Notify(sigHangup, syscall.SIGHUP)
defer cancel() defer cancel()
err := cmd.Run(ctx, os.Args) err := cmd.Run(ctx, os.Args)
@@ -180,6 +184,40 @@ func run(ctx context.Context, c *cli.Command) error {
router.Start(context.Background()) router.Start(context.Background())
}) })
go func() {
for {
select {
case <-sigHangup:
commandLogger.Info("received SIGHUP, reloading configuration")
newConfig, err := readConfig(configPath)
if err != nil {
commandLogger.Error("error reading config file", "error", err)
continue
}
newRouter, moduleErrors, routeErrors := showbridge.NewRouter(newConfig, tracer)
for _, moduleError := range moduleErrors {
commandLogger.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
}
for _, routeError := range routeErrors {
commandLogger.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
}
if moduleErrors == nil && routeErrors == nil {
router.Stop()
routerRunner.Wait()
router = newRouter
routerRunner.Go(func() {
router.Start(context.Background())
})
}
case <-ctx.Done():
return
}
}
}()
<-ctx.Done() <-ctx.Done()
commandLogger.Debug("shutting down router") commandLogger.Debug("shutting down router")
router.Stop() router.Stop()