Refactoring
- logging interface - log levels - application interface Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
This commit is contained in:
@ -2,15 +2,17 @@ package application
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
|
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Application interface {
|
type Application interface {
|
||||||
Start(ctx context.Context) error
|
ExecuteContext(ctx context.Context) error
|
||||||
Shutdown() error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(c Config) (Application, error) {
|
func New(c Config) (Application, error) {
|
||||||
@ -31,53 +33,67 @@ type application struct {
|
|||||||
config Config
|
config Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *application) Start(ctx context.Context) error {
|
func (a *application) ExecuteContext(ctx context.Context) error {
|
||||||
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "configuring application shutdown signals", slog.Any("signals", a.config.ShutdownSignals))
|
||||||
|
|
||||||
sigCtx, sigStop := signal.NotifyContext(ctx, a.config.ShutdownSignals...)
|
sigCtx, sigCancel := signal.NotifyContext(ctx, a.config.ShutdownSignals...)
|
||||||
defer sigStop() // Ensure that this gets called.
|
defer sigCancel() // Ensure that this gets called.
|
||||||
|
|
||||||
// Result channel for command
|
// Result channel for command output
|
||||||
chExe := make(chan error)
|
chExe := make(chan error)
|
||||||
go a.execute(sigCtx, chExe)
|
|
||||||
|
|
||||||
|
// Run the application command
|
||||||
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "executing application context")
|
||||||
|
go func(ctx context.Context, chErr chan error) {
|
||||||
|
chErr <- a.cmd.ExecuteContext(ctx)
|
||||||
|
}(sigCtx, chExe)
|
||||||
|
|
||||||
|
// Wait for command output or a shutdown signal
|
||||||
|
var err error
|
||||||
select {
|
select {
|
||||||
// sigCtx.Done() returns a channel that will have a message
|
// sigCtx.Done() returns a channel that will have a message when the context is canceled.
|
||||||
// when the context is cancelled. We wait for that signal, which means
|
// Alternatively, chExe will receive the response from the execution context if the application finishes.
|
||||||
// we received the signal, or our context was cancelled for some other reason.
|
|
||||||
case <-sigCtx.Done():
|
case <-sigCtx.Done():
|
||||||
sigStop()
|
sigCancel()
|
||||||
return a.Shutdown()
|
|
||||||
case err := <-chExe:
|
// 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
|
return err
|
||||||
}
|
}
|
||||||
}
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "graceful shutdown completed")
|
||||||
|
return nil
|
||||||
func (a *application) Shutdown() error {
|
|
||||||
switch a.config.EnableGracefulShutdown {
|
|
||||||
case true:
|
|
||||||
return a.gracefulShutdown()
|
|
||||||
case false:
|
case false:
|
||||||
return a.shutdown()
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "immediately shutting down application")
|
||||||
// default:
|
return nil
|
||||||
// return a.shutdown()
|
|
||||||
}
|
}
|
||||||
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *application) execute(ctx context.Context, chErr chan error) {
|
func (a *application) gracefulShutdown(ctx context.Context) error {
|
||||||
chErr <- a.cmd.ExecuteContext(ctx)
|
fmt.Printf("waiting %s for graceful application shutdown... PRESS CTRL+C again to quit now!\n", a.config.ShutdownTimeout)
|
||||||
}
|
|
||||||
|
|
||||||
func (a *application) gracefulShutdown() error {
|
shutdownCtx, shutdownCancel := context.WithTimeout(ctx, a.config.ShutdownTimeout)
|
||||||
fmt.Println("graceful shutdown")
|
|
||||||
|
|
||||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), a.config.ShutdownTimeout)
|
|
||||||
defer shutdownCancel()
|
defer shutdownCancel()
|
||||||
<-shutdownCtx.Done()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *application) shutdown() error {
|
// Wait for the shutdown timeout or a hard exit signal
|
||||||
fmt.Println("shutdown")
|
sigCtx, sigCancel := signal.NotifyContext(shutdownCtx, a.config.ShutdownSignals...)
|
||||||
|
defer sigCancel() // Ensure that this gets called.
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-shutdownCtx.Done():
|
||||||
|
return shutdownCtx.Err()
|
||||||
|
case <-sigCtx.Done():
|
||||||
|
sigCancel()
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package application
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -15,8 +16,9 @@ type Config struct {
|
|||||||
Name string
|
Name string
|
||||||
Title string
|
Title string
|
||||||
Banner string
|
Banner string
|
||||||
Version string
|
Version Version
|
||||||
EnableGracefulShutdown bool
|
EnableGracefulShutdown bool
|
||||||
|
Logger *slog.Logger
|
||||||
OverrideRunE func(cmd *cobra.Command, args []string) error
|
OverrideRunE func(cmd *cobra.Command, args []string) error
|
||||||
PersistentPreRunE []func(cmd *cobra.Command, args []string) error // collection of PreRunE functions
|
PersistentPreRunE []func(cmd *cobra.Command, args []string) error // collection of PreRunE functions
|
||||||
PersistentPostRunE []func(cmd *cobra.Command, args []string) error // collection of PostRunE functions
|
PersistentPostRunE []func(cmd *cobra.Command, args []string) error // collection of PostRunE functions
|
||||||
@ -59,12 +61,7 @@ func (c Config) getRootCommand() (*cobra.Command, error) {
|
|||||||
cmd.AddCommand(subcommand.Initialize(c.SubCommandInitializeFunc))
|
cmd.AddCommand(subcommand.Initialize(c.SubCommandInitializeFunc))
|
||||||
}
|
}
|
||||||
|
|
||||||
var v semver.Version
|
configureVersionFlag(cmd, c.Version) // Configure app for version information
|
||||||
if v, err = c.ParseVersion(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
configureVersionFlag(cmd, v) // Configure app for version information
|
|
||||||
configureOutputFlags(cmd) // Configure verbosity
|
configureOutputFlags(cmd) // Configure verbosity
|
||||||
configureLoggingFlags(cmd) // Configure logging
|
configureLoggingFlags(cmd) // Configure logging
|
||||||
cmd.PersistentFlags().SetNormalizeFunc(normalizeFunc) // normalize persistent flags
|
cmd.PersistentFlags().SetNormalizeFunc(normalizeFunc) // normalize persistent flags
|
||||||
@ -73,7 +70,7 @@ func (c Config) getRootCommand() (*cobra.Command, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c Config) ParseVersion() (semver.Version, error) {
|
func (c Config) ParseVersion() (semver.Version, error) {
|
||||||
return semver.Parse(c.Version)
|
return semver.Parse(c.Version.Full)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Config) RegisterCommand(cmd Commander, f func(*cobra.Command)) {
|
func (c Config) RegisterCommand(cmd Commander, f func(*cobra.Command)) {
|
||||||
@ -103,7 +100,7 @@ func (c Config) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if _, err = semver.Parse(c.Version); err != nil {
|
if _, err = semver.Parse(c.Version.Full); err != nil {
|
||||||
return fmt.Errorf("invalid version: %s", c.Version)
|
return fmt.Errorf("invalid version: %s", c.Version)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -66,7 +66,7 @@ func persistentPreRunFuncE(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
// TODO move to front??
|
// TODO move to front??
|
||||||
if quietFlag {
|
if quietFlag {
|
||||||
slogd.FromContext(cmd.Context()).LogAttrs(cmd.Context(), slogd.LevelDebug, "activating quiet mode")
|
slogd.FromContext(cmd.Context()).LogAttrs(cmd.Context(), slogd.LevelTrace, "activating quiet mode")
|
||||||
outWriter = io.Discard
|
outWriter = io.Discard
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package application
|
package application
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log/slog"
|
||||||
|
|
||||||
|
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -10,20 +13,25 @@ const (
|
|||||||
LogOutputFile = "file"
|
LogOutputFile = "file"
|
||||||
)
|
)
|
||||||
|
|
||||||
var logLevelFlag string
|
var (
|
||||||
var logOutputFlag string
|
logLevelFlagName string = "log-level"
|
||||||
var logTypeFlag string
|
logLevelFlag string
|
||||||
|
logOutputFlagName string = "log-output"
|
||||||
|
logOutputFlag string
|
||||||
|
logTypeFlagName = "log-type"
|
||||||
|
logTypeFlag string
|
||||||
|
)
|
||||||
|
|
||||||
func addLogLevelFlag(cmd *cobra.Command) {
|
func addLogLevelFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().StringVarP(&logLevelFlag, "log-level", "", "info", "Set log level (trace, debug, info, warn, error, fatal)")
|
cmd.PersistentFlags().StringVarP(&logLevelFlag, logLevelFlagName, "", "info", "Set log level (trace, debug, info, warn, error, fatal)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func addLogOutputFlag(cmd *cobra.Command) {
|
func addLogOutputFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().StringVarP(&logOutputFlag, "log-outWriter", "", "stderr", "Set log outWriter (stdout, stderr, file)")
|
cmd.PersistentFlags().StringVarP(&logOutputFlag, logOutputFlagName, "", "stderr", "Set log output (stdout, stderr, file)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func addLogTypeFlag(cmd *cobra.Command) {
|
func addLogTypeFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().StringVarP(&logTypeFlag, "log-type", "", "text", "Set log type (text, json, color)")
|
cmd.PersistentFlags().StringVarP(&logTypeFlag, logTypeFlagName, "", "text", "Set log type (text, json, color)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureLoggingFlags(cmd *cobra.Command) {
|
func configureLoggingFlags(cmd *cobra.Command) {
|
||||||
@ -33,3 +41,12 @@ func configureLoggingFlags(cmd *cobra.Command) {
|
|||||||
|
|
||||||
cmd.MarkFlagsMutuallyExclusive("no-color", "log-type")
|
cmd.MarkFlagsMutuallyExclusive("no-color", "log-type")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetLogLevelFromArgs(args []string) slog.Level {
|
||||||
|
for i, arg := range args {
|
||||||
|
if arg == "--log-level" && i+1 < len(args) {
|
||||||
|
return slogd.Level(args[i+1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return slogd.LevelDefault
|
||||||
|
}
|
||||||
|
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module git.flexabyte.io/flexabyte/go-kit
|
|||||||
go 1.24.2
|
go 1.24.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250422113213-08cde7c3729c
|
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250428200220-8e65f81d9450
|
||||||
github.com/spf13/cobra v1.9.1
|
github.com/spf13/cobra v1.9.1
|
||||||
github.com/spf13/pflag v1.0.6
|
github.com/spf13/pflag v1.0.6
|
||||||
)
|
)
|
||||||
|
2
go.sum
2
go.sum
@ -2,6 +2,8 @@ git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250416190113-64c1cac4d274 h1:aR9nV8
|
|||||||
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250416190113-64c1cac4d274/go.mod h1:rL08OHw4aycfjkZOS8pBfLapeG3IZHxIInW29hVVSrI=
|
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250416190113-64c1cac4d274/go.mod h1:rL08OHw4aycfjkZOS8pBfLapeG3IZHxIInW29hVVSrI=
|
||||||
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250422113213-08cde7c3729c h1:8ZVNJJ/LNbu4aWozJ6wj0ZOZFeuqRh1xSyQRh+Cq1yM=
|
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250422113213-08cde7c3729c h1:8ZVNJJ/LNbu4aWozJ6wj0ZOZFeuqRh1xSyQRh+Cq1yM=
|
||||||
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250422113213-08cde7c3729c/go.mod h1:rL08OHw4aycfjkZOS8pBfLapeG3IZHxIInW29hVVSrI=
|
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250422113213-08cde7c3729c/go.mod h1:rL08OHw4aycfjkZOS8pBfLapeG3IZHxIInW29hVVSrI=
|
||||||
|
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250428200220-8e65f81d9450 h1:VCstITW9pMgR5EnSLU83UiK7llLOguFLAo26VOOnzrI=
|
||||||
|
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250428200220-8e65f81d9450/go.mod h1:rL08OHw4aycfjkZOS8pBfLapeG3IZHxIInW29hVVSrI=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
Reference in New Issue
Block a user