Add slogd package back into repository, continued...

Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
This commit is contained in:
Jan Tytgat
2025-04-29 22:26:54 +02:00
parent f8121b8ada
commit aa56895853
4 changed files with 30 additions and 28 deletions

View File

@ -7,8 +7,9 @@ import (
"log/slog" "log/slog"
"os/signal" "os/signal"
"git.flexabyte.io/flexabyte/go-slogd/slogd"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"git.flexabyte.io/flexabyte/go-kit/slogd"
) )
type Application interface { 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. // Alternatively, chExe will receive the response from the execution context if the application finishes.
case <-sigCtx.Done(): case <-sigCtx.Done():
sigCancel() sigCancel()
return a.handleShutdownSignal(ctx)
// 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
}
case err = <-chExe: case err = <-chExe:
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "application terminated successfully") a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "application terminated successfully")
return err return err
} }
}
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "application terminated unexpectedly") func (a *application) handleShutdownSignal(ctx context.Context) error {
return nil 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 { func (a *application) gracefulShutdown(ctx context.Context) error {
@ -93,7 +97,8 @@ func (a *application) gracefulShutdown(ctx context.Context) error {
case <-shutdownCtx.Done(): case <-shutdownCtx.Done():
return shutdownCtx.Err() return shutdownCtx.Err()
case <-sigCtx.Done(): case <-sigCtx.Done():
fmt.Println("exiting...")
sigCancel() sigCancel()
return nil return fmt.Errorf("process killed")
} }
} }

View File

@ -11,7 +11,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "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} var DefaultShutdownSignals = []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT}

View File

@ -3,8 +3,9 @@ package application
import ( import (
"log/slog" "log/slog"
"git.flexabyte.io/flexabyte/go-slogd/slogd"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"git.flexabyte.io/flexabyte/go-kit/slogd"
) )
const ( const (

View File

@ -5,14 +5,13 @@ import (
"log/slog" "log/slog"
"net/http" "net/http"
"os" "os"
"syscall"
"time" "time"
"git.flexabyte.io/flexabyte/go-slogd/slogd"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"git.flexabyte.io/flexabyte/go-kit/application" "git.flexabyte.io/flexabyte/go-kit/application"
"git.flexabyte.io/flexabyte/go-kit/httpd" "git.flexabyte.io/flexabyte/go-kit/httpd"
"git.flexabyte.io/flexabyte/go-kit/slogd"
) )
var ( var (
@ -55,18 +54,15 @@ func main() {
EnableGracefulShutdown: true, EnableGracefulShutdown: true,
Logger: slogd.Logger(), Logger: slogd.Logger(),
OverrideRunE: func(cmd *cobra.Command, args []string) error { OverrideRunE: func(cmd *cobra.Command, args []string) error {
// fmt.Println("overrideRunE")
// time.Sleep(1 * time.Second)
// fmt.Println("overrideRunE done")
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 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())) 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, PersistentPreRunE: nil,
PersistentPostRunE: nil, PersistentPostRunE: nil,
ShutdownSignals: []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT}, ShutdownSignals: application.DefaultShutdownSignals,
ShutdownTimeout: 5 * time.Second, ShutdownTimeout: 5 * time.Second,
SubCommands: nil, SubCommands: nil,
SubCommandInitializeFunc: nil, SubCommandInitializeFunc: nil,