Refactor reload handling

This commit is contained in:
Martin Hebnes Pedersen 2025-06-22 20:22:37 +02:00
parent 93fc985420
commit 9bc6ac22b1
2 changed files with 7 additions and 16 deletions

View file

@ -70,7 +70,7 @@ type Options struct {
type App struct {
options Options
config cfg.Config
OnReload func()
OnReload func() error
mbox *mailbox.DirHandler
formsMgr *forms.Manager
@ -123,13 +123,7 @@ func (a *App) Options() Options { return a.options }
func (a *App) PromptHub() *PromptHub { return a.promptHub }
func (a *App) Reload() error {
if a.OnReload == nil {
return fmt.Errorf("reload not supported")
}
a.OnReload()
return nil
}
func (a *App) Reload() error { return a.OnReload() }
func (a *App) VFOForRig(rig string) (hamlib.VFO, bool) { r, ok := a.rigs[rig]; return r, ok }

13
main.go
View file

@ -85,14 +85,14 @@ func runApp(opts app.Options, cmd app.Command, args []string, sig <-chan os.Sign
// Graceful shutdown/reload handling.
shouldReload := make(chan bool, 1)
done := make(chan struct{})
a.OnReload = func() {
a.OnReload = func() error {
// Avoid reloading of bad config
if _, err := app.LoadConfig(opts.ConfigPath, cfg.DefaultConfig); err != nil {
log.Printf("Ignoring live reload due to config error: %v", err)
return
return fmt.Errorf("bad config: %v", err)
}
cancel()
shouldReload <- true
return nil
}
go func() {
defer close(shouldReload)
@ -102,13 +102,10 @@ func runApp(opts app.Options, cmd app.Command, args []string, sig <-chan os.Sign
case s := <-sig:
switch {
case isSIGHUP(s):
// Avoid reloading of bad config
if _, err := app.LoadConfig(opts.ConfigPath, cfg.DefaultConfig); err != nil {
log.Printf("Ignoring live reload due to config error: %v", err)
if err := a.Reload(); err != nil {
log.Printf("Ignoring live reload due to error: %v", err)
continue
}
cancel()
shouldReload <- true
return
default:
if ok := a.AbortActiveConnection(dirtyDisconnectNext); ok {