Skip to content

Commit

Permalink
chore: shutdown message on end
Browse files Browse the repository at this point in the history
  • Loading branch information
puffitos committed Dec 25, 2024
1 parent 832df3e commit 68a9c44
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
7 changes: 4 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,18 @@ func run() func(cmd *cobra.Command, args []string) error {

s := sparrow.New(cfg)
cErr := make(chan error, 1)
log.Info("Running sparrow")
log.InfoContext(ctx, "Running sparrow")
go func() {
cErr <- s.Run(ctx)
}()

select {
case <-sigChan:
log.Info("Signal received, shutting down")
log.InfoContext(ctx, "Signal received, shutting down")
cancel()
<-cErr
case err := <-cErr:
case err = <-cErr:
log.InfoContext(ctx, "Sparrow was shut down")
return err
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/sparrow/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (s *Sparrow) Run(ctx context.Context) error {
s.shutdown(ctx)
}
case <-s.cDone:
log.InfoContext(ctx, "Sparrow was shut down")
return fmt.Errorf("sparrow was shut down")
}
}
Expand Down Expand Up @@ -181,7 +182,7 @@ func (s *Sparrow) shutdown(ctx context.Context) {
defer cancel()

s.shutOnce.Do(func() {
log.Info("Shutting down sparrow gracefully")
log.InfoContext(ctx, "Shutting down sparrow")
var sErrs ErrShutdown
if s.tarMan != nil {
sErrs.errTarMan = s.tarMan.Shutdown(ctx)
Expand All @@ -192,7 +193,7 @@ func (s *Sparrow) shutdown(ctx context.Context) {
s.controller.Shutdown(ctx)

if sErrs.HasError() {
log.Error("Failed to shutdown gracefully", "contextError", errC, "errors", sErrs)
log.ErrorContext(ctx, "Failed to shutdown gracefully", "contextError", errC, "errors", sErrs)
}

// Signal that shutdown is complete
Expand Down
3 changes: 3 additions & 0 deletions pkg/sparrow/run_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

package sparrow

// ErrShutdown holds any errors that may
// have occurred during shutdown of the Sparrow
type ErrShutdown struct {
errAPI error
errTarMan error
errMetrics error
}

// HasError returns true if any of the errors are set
func (e ErrShutdown) HasError() bool {
return e.errAPI != nil || e.errTarMan != nil || e.errMetrics != nil
}

0 comments on commit 68a9c44

Please sign in to comment.