From 68a9c4484b01d5c28972eab2d01a7291a2011f07 Mon Sep 17 00:00:00 2001 From: Bruno Bressi Date: Wed, 25 Dec 2024 10:23:56 +0100 Subject: [PATCH] chore: shutdown message on end --- cmd/run.go | 7 ++++--- pkg/sparrow/run.go | 5 +++-- pkg/sparrow/run_errors.go | 3 +++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 1c1bffd0..413300e0 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -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 } diff --git a/pkg/sparrow/run.go b/pkg/sparrow/run.go index 4ef56c68..2586a991 100644 --- a/pkg/sparrow/run.go +++ b/pkg/sparrow/run.go @@ -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") } } @@ -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) @@ -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 diff --git a/pkg/sparrow/run_errors.go b/pkg/sparrow/run_errors.go index e1f983ba..36630b4d 100644 --- a/pkg/sparrow/run_errors.go +++ b/pkg/sparrow/run_errors.go @@ -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 }