control flow of router running with better context handling and logging in cmd

This commit is contained in:
Joel Wetzell
2025-12-26 10:39:09 -06:00
parent 17106b5096
commit 18b307bca1

View File

@@ -6,6 +6,7 @@ import (
"log/slog" "log/slog"
"os" "os"
"os/signal" "os/signal"
"sync"
"github.com/jwetzell/showbridge-go" "github.com/jwetzell/showbridge-go"
"github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/config"
@@ -39,7 +40,37 @@ func main() {
Usage: "log using JSON", Usage: "log using JSON",
}, },
}, },
Action: func(ctx context.Context, c *cli.Command) error { Action: run,
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
err := cmd.Run(ctx, os.Args)
if err != nil {
panic(err)
}
}
func readConfig(configPath string) (config.Config, error) {
cfg := config.Config{}
configBytes, err := os.ReadFile(configPath)
if err != nil {
return config.Config{}, err
}
err = yaml.Unmarshal(configBytes, &cfg)
if err != nil {
return config.Config{}, err
}
return cfg, nil
}
func run(ctx context.Context, c *cli.Command) error {
configPath := c.String("config") configPath := c.String("config")
if configPath == "" { if configPath == "" {
return errors.New("config value cannot be empty") return errors.New("config value cannot be empty")
@@ -68,47 +99,32 @@ func main() {
logHandler = slog.NewJSONHandler(logOutput, logHandlerOptions) logHandler = slog.NewJSONHandler(logOutput, logHandlerOptions)
} }
logger := slog.New(logHandler) slog.SetDefault(slog.New(logHandler))
slog.SetDefault(logger) commandLogger := slog.Default().With("component", "cmd")
router, moduleErrors, routeErrors := showbridge.NewRouter(ctx, config) routerContext, routerCancel := context.WithCancel(context.Background())
router, moduleErrors, routeErrors := showbridge.NewRouter(routerContext, config)
for _, moduleError := range moduleErrors { for _, moduleError := range moduleErrors {
logger.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error) commandLogger.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
} }
for _, routeError := range routeErrors { for _, routeError := range routeErrors {
logger.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error) commandLogger.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
} }
routerRunner := sync.WaitGroup{}
routerRunner.Go(func() {
router.Run() router.Run()
})
<-ctx.Done()
commandLogger.Debug("shutting down router")
routerCancel()
commandLogger.Debug("waiting for router to exit")
routerRunner.Wait()
return nil return nil
},
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Interrupt)
defer cancel()
err := cmd.Run(ctx, os.Args)
if err != nil {
panic(err)
}
}
func readConfig(configPath string) (config.Config, error) {
cfg := config.Config{}
configBytes, err := os.ReadFile(configPath)
if err != nil {
return config.Config{}, err
}
err = yaml.Unmarshal(configBytes, &cfg)
if err != nil {
return config.Config{}, err
}
return cfg, nil
} }