Skip to content

Commit

Permalink
notfier: improve backend init handling
Browse files Browse the repository at this point in the history
  • Loading branch information
equinox0815 committed Oct 16, 2023
1 parent 2f73907 commit 44e7473
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cmd/whawty-alerts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func cmdRun(c *cli.Context) error {

s, err := store.Open(&conf.Store, wl, wdl)
if err != nil {
return cli.NewExitError(err.Error(), 3)
return cli.NewExitError(fmt.Sprintf("failed to initialize store: %v", err), 3)
}
defer s.Close()
n, err := notifier.NewNotifier(&conf.Notifier, s, wl, wdl)
if err != nil {
return cli.NewExitError(err.Error(), 3)
return cli.NewExitError(fmt.Sprintf("failed to initialize notifier: %v", err), 3)
}
defer n.Close()

Expand Down
24 changes: 15 additions & 9 deletions notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ package notifier

import (
"context"
"fmt"
"io"
"log"
"time"
Expand All @@ -51,6 +52,11 @@ type Notifier struct {

func (n *Notifier) Close() error {
n.cancel()
for _, backend := range n.backends {
if backend.Ready() {
backend.Close()
}
}
return nil
}

Expand All @@ -71,12 +77,12 @@ func NewNotifier(conf *Config, st *store.Store, infoLog, dbgLog *log.Logger) (n
n.backends = make(map[string]NotifierBackend)
for idx, backend := range n.conf.Backends {
if backend.Name == "" {
infoLog.Printf("notifier: ignoring unnamed backend at index %d", idx)
continue // make this an permanent error??
err = fmt.Errorf("found unnamed backend at config index %d", idx)
return
}
if _, ok := n.backends[backend.Name]; ok {
infoLog.Printf("notifier: ignoring duplicate backend name at index %d", idx)
continue // make this an permanent error??
if _, exists := n.backends[backend.Name]; exists {
err = fmt.Errorf("found duplicate backend name at config index %d", idx)
return
}

var b NotifierBackend
Expand All @@ -90,12 +96,12 @@ func NewNotifier(conf *Config, st *store.Store, infoLog, dbgLog *log.Logger) (n
cnt = cnt + 1
}
if cnt == 0 {
infoLog.Printf("notifier: no valid backend config found for backend '%s'", backend.Name)
continue
err = fmt.Errorf("no valid backend config found for backend '%s'", backend.Name)
return
}
if cnt > 1 {
infoLog.Printf("notifier: ambiguous backend config '%s'", backend.Name)
continue
err = fmt.Errorf("backend '%s' has ambiguous backend config", backend.Name)
return
}
n.backends[backend.Name] = b

Expand Down

0 comments on commit 44e7473

Please sign in to comment.