Skip to content

Commit

Permalink
handle signals better
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Siwiec <rizzza@users.noreply.github.com>
  • Loading branch information
rizzza committed Sep 14, 2023
1 parent 9ca236a commit 7977335
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
8 changes: 2 additions & 6 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/rand"
"os"
"os/signal"
"syscall"
"time"

"go.infratographer.com/x/events"
Expand Down Expand Up @@ -84,7 +85,7 @@ func run(cmdCtx context.Context, v *viper.Viper) error {
}

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

ctx, cancel := context.WithCancel(cmdCtx)

Expand Down Expand Up @@ -155,11 +156,6 @@ func run(cmdCtx context.Context, v *viper.Viper) error {
}

defer func() {
const shutdownTimeout = 10 * time.Second
ctx, cancel := context.WithTimeout(ctx, shutdownTimeout)

defer cancel()

_ = events.Shutdown(ctx)
}()

Expand Down
16 changes: 11 additions & 5 deletions internal/dataplaneapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,19 @@ func (c *Client) PostConfig(ctx context.Context, config string) error {
// WaitForDataPlaneReady waits for the DataPlane API to be ready
func (c Client) WaitForDataPlaneReady(ctx context.Context, retries int, sleep time.Duration) error {
for i := 0; i < retries; i++ {
if c.APIIsReady(ctx) {
c.logger.Info("dataplaneapi is ready")
select {
case <-ctx.Done():
c.logger.Info("context done")
return nil
default:
if c.APIIsReady(ctx) {
c.logger.Info("dataplaneapi is ready")
return nil
}

c.logger.Info("waiting for dataplaneapi to become ready")
time.Sleep(sleep)
}

c.logger.Info("waiting for dataplaneapi to become ready")
time.Sleep(sleep)
}

return ErrDataPlaneNotReady
Expand Down
19 changes: 12 additions & 7 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,19 @@ func (m *Manager) Run() error {
m.Logger.Fatal("unable to reach dataplaneapi. is it running?")
}

// use desired config on start
if err := m.updateConfigToLatest(); err != nil {
m.Logger.Fatalw("failed to initialize the config", zap.Error(err))
}
select {
case <-m.Context.Done():
return nil
default:
// use desired config on start
if err := m.updateConfigToLatest(); err != nil {
m.Logger.Fatalw("failed to initialize the config", zap.Error(err))
}

// listen for event messages on subject(s)
if err := m.Subscriber.Listen(); err != nil {
return err
// listen for event messages on subject(s)
if err := m.Subscriber.Listen(); err != nil {
return err
}
}

return nil
Expand Down

0 comments on commit 7977335

Please sign in to comment.