diff --git a/application/application.go b/application/application.go index de76de1..7287c98 100644 --- a/application/application.go +++ b/application/application.go @@ -7,8 +7,9 @@ import ( "log/slog" "os/signal" - "git.flexabyte.io/flexabyte/go-slogd/slogd" "github.com/spf13/cobra" + + "git.flexabyte.io/flexabyte/go-kit/slogd" ) type Application interface { @@ -55,28 +56,31 @@ func (a *application) ExecuteContext(ctx context.Context) error { // Alternatively, chExe will receive the response from the execution context if the application finishes. case <-sigCtx.Done(): sigCancel() - - // Adapt the shutdown scenario if a graceful shutdown period is configured - switch a.config.EnableGracefulShutdown && a.config.ShutdownTimeout > 0 { - case true: - a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "gracefully shutting down application") - if err = a.gracefulShutdown(ctx); !errors.Is(err, context.DeadlineExceeded) { - a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "graceful shutdown completed with error", slog.Any("error", err)) - return err - } - a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "graceful shutdown completed") - return nil - case false: - a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "immediately shutting down application") - return nil - } + return a.handleShutdownSignal(ctx) case err = <-chExe: a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "application terminated successfully") return err } +} - a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "application terminated unexpectedly") - return nil +func (a *application) handleShutdownSignal(ctx context.Context) error { + var err error + // Adapt the shutdown scenario if a graceful shutdown period is configured + switch a.config.EnableGracefulShutdown && a.config.ShutdownTimeout > 0 { + case true: + a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "gracefully shutting down application") + if err = a.gracefulShutdown(ctx); !errors.Is(err, context.DeadlineExceeded) { + a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "graceful shutdown failed", slog.Any("error", err)) + return nil + } + a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "graceful shutdown completed") + return nil + case false: + a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "immediately shutting down application") + return nil + default: + panic("cannot handle shutdown signal") + } } func (a *application) gracefulShutdown(ctx context.Context) error { @@ -93,7 +97,8 @@ func (a *application) gracefulShutdown(ctx context.Context) error { case <-shutdownCtx.Done(): return shutdownCtx.Err() case <-sigCtx.Done(): + fmt.Println("exiting...") sigCancel() - return nil + return fmt.Errorf("process killed") } } diff --git a/application/globals.go b/application/globals.go index 4c2ff0d..034a8bc 100644 --- a/application/globals.go +++ b/application/globals.go @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" - "git.flexabyte.io/flexabyte/go-slogd/slogd" + "git.flexabyte.io/flexabyte/go-kit/slogd" ) var DefaultShutdownSignals = []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT} diff --git a/application/logging.go b/application/logging.go index 341ff63..0ee8c2b 100644 --- a/application/logging.go +++ b/application/logging.go @@ -3,8 +3,9 @@ package application import ( "log/slog" - "git.flexabyte.io/flexabyte/go-slogd/slogd" "github.com/spf13/cobra" + + "git.flexabyte.io/flexabyte/go-kit/slogd" ) const ( diff --git a/examples/simple/main.go b/examples/simple/main.go index 6325284..b18b98b 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -5,14 +5,13 @@ import ( "log/slog" "net/http" "os" - "syscall" "time" - "git.flexabyte.io/flexabyte/go-slogd/slogd" "github.com/spf13/cobra" "git.flexabyte.io/flexabyte/go-kit/application" "git.flexabyte.io/flexabyte/go-kit/httpd" + "git.flexabyte.io/flexabyte/go-kit/slogd" ) var ( @@ -55,18 +54,15 @@ func main() { EnableGracefulShutdown: true, Logger: slogd.Logger(), OverrideRunE: func(cmd *cobra.Command, args []string) error { - // fmt.Println("overrideRunE") - // time.Sleep(1 * time.Second) - // fmt.Println("overrideRunE done") mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { slogd.FromContext(r.Context()).LogAttrs(r.Context(), slogd.LevelInfo, "request received", slog.String("method", r.Method), slog.String("url", r.URL.String()), slog.String("user-agent", r.UserAgent())) }) - return httpd.RunHttpServer(cmd.Context(), slogd.FromContext(cmd.Context()), "127.0.0.1", 28000, mux, 1*time.Second) + return httpd.RunHttpServer(cmd.Context(), slogd.Logger(), "127.0.0.1", 28000, mux, 5*time.Second) }, PersistentPreRunE: nil, PersistentPostRunE: nil, - ShutdownSignals: []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT}, + ShutdownSignals: application.DefaultShutdownSignals, ShutdownTimeout: 5 * time.Second, SubCommands: nil, SubCommandInitializeFunc: nil,