Compare commits
3 Commits
b9a06f5fd4
...
19304a8d8e
Author | SHA1 | Date | |
---|---|---|---|
19304a8d8e | |||
a04a01f6ca | |||
4cea479d42 |
@ -2,15 +2,17 @@ package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os/signal"
|
||||
|
||||
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type Application interface {
|
||||
Start(ctx context.Context) error
|
||||
Shutdown() error
|
||||
ExecuteContext(ctx context.Context) error
|
||||
}
|
||||
|
||||
func New(c Config) (Application, error) {
|
||||
@ -31,53 +33,67 @@ type application struct {
|
||||
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...)
|
||||
defer sigStop() // Ensure that this gets called.
|
||||
sigCtx, sigCancel := signal.NotifyContext(ctx, a.config.ShutdownSignals...)
|
||||
defer sigCancel() // Ensure that this gets called.
|
||||
|
||||
// Result channel for command
|
||||
// Result channel for command output
|
||||
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 {
|
||||
// sigCtx.Done() returns a channel that will have a message
|
||||
// when the context is cancelled. We wait for that signal, which means
|
||||
// we received the signal, or our context was cancelled for some other reason.
|
||||
// sigCtx.Done() returns a channel that will have a message when the context is canceled.
|
||||
// Alternatively, chExe will receive the response from the execution context if the application finishes.
|
||||
case <-sigCtx.Done():
|
||||
sigStop()
|
||||
return a.Shutdown()
|
||||
case err := <-chExe:
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func (a *application) Shutdown() error {
|
||||
switch a.config.EnableGracefulShutdown {
|
||||
case true:
|
||||
return a.gracefulShutdown()
|
||||
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "graceful shutdown completed")
|
||||
return nil
|
||||
case false:
|
||||
return a.shutdown()
|
||||
// default:
|
||||
// return a.shutdown()
|
||||
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "immediately shutting down application")
|
||||
return nil
|
||||
}
|
||||
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) execute(ctx context.Context, chErr chan error) {
|
||||
chErr <- a.cmd.ExecuteContext(ctx)
|
||||
}
|
||||
func (a *application) gracefulShutdown(ctx context.Context) error {
|
||||
fmt.Printf("waiting %s for graceful application shutdown... PRESS CTRL+C again to quit now!\n", a.config.ShutdownTimeout)
|
||||
|
||||
func (a *application) gracefulShutdown() error {
|
||||
fmt.Println("graceful shutdown")
|
||||
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), a.config.ShutdownTimeout)
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(ctx, a.config.ShutdownTimeout)
|
||||
defer shutdownCancel()
|
||||
<-shutdownCtx.Done()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *application) shutdown() error {
|
||||
fmt.Println("shutdown")
|
||||
// Wait for the shutdown timeout or a hard exit signal
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package application
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@ -15,8 +16,9 @@ type Config struct {
|
||||
Name string
|
||||
Title string
|
||||
Banner string
|
||||
Version string
|
||||
Version Version
|
||||
EnableGracefulShutdown bool
|
||||
Logger *slog.Logger
|
||||
OverrideRunE func(cmd *cobra.Command, args []string) error
|
||||
PersistentPreRunE []func(cmd *cobra.Command, args []string) error // collection of PreRunE 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))
|
||||
}
|
||||
|
||||
var v semver.Version
|
||||
if v, err = c.ParseVersion(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
configureVersionFlag(cmd, v) // Configure app for version information
|
||||
configureVersionFlag(cmd, c.Version) // Configure app for version information
|
||||
configureOutputFlags(cmd) // Configure verbosity
|
||||
configureLoggingFlags(cmd) // Configure logging
|
||||
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) {
|
||||
return semver.Parse(c.Version)
|
||||
return semver.Parse(c.Version.Full)
|
||||
}
|
||||
|
||||
func (c Config) RegisterCommand(cmd Commander, f func(*cobra.Command)) {
|
||||
@ -103,7 +100,7 @@ func (c Config) Validate() 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 nil
|
||||
|
@ -66,7 +66,7 @@ func persistentPreRunFuncE(cmd *cobra.Command, args []string) error {
|
||||
|
||||
// TODO move to front??
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,9 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -10,20 +13,25 @@ const (
|
||||
LogOutputFile = "file"
|
||||
)
|
||||
|
||||
var logLevelFlag string
|
||||
var logOutputFlag string
|
||||
var logTypeFlag string
|
||||
var (
|
||||
logLevelFlagName string = "log-level"
|
||||
logLevelFlag string
|
||||
logOutputFlagName string = "log-output"
|
||||
logOutputFlag string
|
||||
logTypeFlagName = "log-type"
|
||||
logTypeFlag string
|
||||
)
|
||||
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
@ -33,3 +41,12 @@ func configureLoggingFlags(cmd *cobra.Command) {
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
version semver.Version
|
||||
version Version
|
||||
versionFlag bool
|
||||
versionCmd = &cobra.Command{
|
||||
Use: versionName,
|
||||
@ -25,11 +25,24 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
type Version struct {
|
||||
Full string
|
||||
Branch string
|
||||
Tag string
|
||||
Commit string
|
||||
CommitDate string
|
||||
BuildDate string
|
||||
Major string
|
||||
Minor string
|
||||
Patch string
|
||||
PreRelease string
|
||||
}
|
||||
|
||||
func addVersionFlag(cmd *cobra.Command) {
|
||||
cmd.PersistentFlags().BoolVarP(&versionFlag, versionName, versionShortHand, false, versionUsage)
|
||||
}
|
||||
|
||||
func configureVersionFlag(cmd *cobra.Command, v semver.Version) {
|
||||
func configureVersionFlag(cmd *cobra.Command, v Version) {
|
||||
version = v
|
||||
cmd.AddCommand(versionCmd)
|
||||
addVersionFlag(cmd)
|
||||
@ -50,19 +63,28 @@ func printVersion(v semver.Version) string {
|
||||
if output != "" {
|
||||
return output
|
||||
}
|
||||
|
||||
return fmt.Sprintf(
|
||||
"Full: %s\nVersion: %s\nChannel: %s\nCommit: %s\nDate: %s",
|
||||
v.String(),
|
||||
v.Number(),
|
||||
v.Release(),
|
||||
v.Commit(),
|
||||
v.Date(),
|
||||
"Full: %s\nBranch: %s\nTag: %s\nCommit: %s\nCommit date: %s\nBuild date: %s\nMajor: %s\nMinor: %s\nPatch: %s\nPreRelease: %s\n",
|
||||
version.Full,
|
||||
version.Branch,
|
||||
version.Tag,
|
||||
version.Commit,
|
||||
version.CommitDate,
|
||||
version.BuildDate,
|
||||
version.Major,
|
||||
version.Minor,
|
||||
version.Patch,
|
||||
version.PreRelease,
|
||||
)
|
||||
}
|
||||
|
||||
func versionRunFuncE(cmd *cobra.Command, args []string) error {
|
||||
if _, err := fmt.Fprintln(outWriter, printVersion(version)); err != nil {
|
||||
var v semver.Version
|
||||
var err error
|
||||
if v, err = semver.Parse(version.Full); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = fmt.Fprintln(outWriter, printVersion(v)); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -2,47 +2,82 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
||||
|
||||
"git.flexabyte.io/flexabyte/go-kit/application"
|
||||
"git.flexabyte.io/flexabyte/go-kit/httpd"
|
||||
)
|
||||
|
||||
var (
|
||||
version string = "0.1.0-alpha.0+metadata.20101112"
|
||||
branch string = "0.1.0-dev"
|
||||
tag string = "0.1.0-dev.0"
|
||||
commit string = "aabbccddee"
|
||||
commitDate string = time.Now().String()
|
||||
buildDate string = time.Now().String()
|
||||
|
||||
major string = "0"
|
||||
minor string = "1"
|
||||
patch string = "0"
|
||||
prerelease string = "dev"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
slogd.Init(slogd.LevelTrace, true)
|
||||
slogd.Init(application.GetLogLevelFromArgs(os.Args), false)
|
||||
slogd.RegisterSink(slogd.HandlerText, slog.NewTextHandler(os.Stdout, slogd.HandlerOptions()), true)
|
||||
ctx := slogd.WithContext(context.Background())
|
||||
|
||||
config := application.Config{
|
||||
Name: "main",
|
||||
Title: "Main Test",
|
||||
Banner: "",
|
||||
Version: "0.1.0-alpha.0+metadata.20101112",
|
||||
// Version: "0.1.0-alpha.0+metadata.20101112",
|
||||
Version: application.Version{
|
||||
Full: version,
|
||||
Branch: branch,
|
||||
Tag: tag,
|
||||
Commit: commit,
|
||||
CommitDate: commitDate,
|
||||
BuildDate: buildDate,
|
||||
Major: major,
|
||||
Minor: minor,
|
||||
Patch: patch,
|
||||
PreRelease: prerelease,
|
||||
},
|
||||
EnableGracefulShutdown: true,
|
||||
Logger: slogd.Logger(),
|
||||
OverrideRunE: func(cmd *cobra.Command, args []string) error {
|
||||
fmt.Println("overrideRunE")
|
||||
time.Sleep(5 * time.Second)
|
||||
fmt.Println("overrideRunE done")
|
||||
return nil
|
||||
// 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)
|
||||
},
|
||||
PersistentPreRunE: nil,
|
||||
PersistentPostRunE: nil,
|
||||
// ShutdownSignals: []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT},
|
||||
ShutdownTimeout: 1 * time.Second,
|
||||
ShutdownSignals: []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT},
|
||||
ShutdownTimeout: 5 * time.Second,
|
||||
SubCommands: nil,
|
||||
SubCommandInitializeFunc: nil,
|
||||
ValidArgs: nil,
|
||||
}
|
||||
|
||||
var app application.Application
|
||||
if app, err = application.New(config); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err = app.Start(context.Background()); err != nil {
|
||||
if err = app.ExecuteContext(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module git.flexabyte.io/flexabyte/go-kit
|
||||
go 1.24.2
|
||||
|
||||
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/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-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-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/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=
|
||||
|
62
httpd/httpd.go
Normal file
62
httpd/httpd.go
Normal file
@ -0,0 +1,62 @@
|
||||
package httpd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
||||
)
|
||||
|
||||
func RunHttpServer(ctx context.Context, log *slog.Logger, listenAddress string, port int, h http.Handler, shutdownTimeout time.Duration) error {
|
||||
s := &http.Server{
|
||||
Addr: listenAddress + ":" + strconv.Itoa(port),
|
||||
Handler: h}
|
||||
|
||||
return run(ctx, s, log, shutdownTimeout)
|
||||
}
|
||||
|
||||
func RunSocketHttpServer(ctx context.Context, log *slog.Logger, socketPath string, h http.Handler, shutdownTimeout time.Duration) error {
|
||||
s := &http.Server{
|
||||
Handler: h}
|
||||
|
||||
return run(ctx, s, log, shutdownTimeout)
|
||||
}
|
||||
|
||||
func run(ctx context.Context, s *http.Server, log *slog.Logger, shutdownTimeout time.Duration) error {
|
||||
log.LogAttrs(ctx, slogd.LevelTrace, "starting http server", slog.String("listenAddress", s.Addr))
|
||||
idleConnectionsClosed := make(chan struct{})
|
||||
|
||||
runCtx, runCancel := context.WithCancel(ctx)
|
||||
defer runCancel()
|
||||
|
||||
go func(ctx context.Context) {
|
||||
log.LogAttrs(ctx, slogd.LevelTrace, "awaiting shutdown signal for http server", slog.String("listenAddress", s.Addr))
|
||||
<-ctx.Done()
|
||||
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
||||
defer shutdownCancel()
|
||||
|
||||
log.LogAttrs(shutdownCtx, slogd.LevelTrace, "shutting down http server", slog.String("listenAddress", s.Addr))
|
||||
// We received an interrupt signal, shut down.
|
||||
if err := s.Shutdown(shutdownCtx); err != nil {
|
||||
// Error from closing listeners, or context timeout:
|
||||
log.LogAttrs(ctx, slogd.LevelTrace, "shutting down http server failed", slog.String("listenAddress", s.Addr), slog.Any("error", err))
|
||||
}
|
||||
log.LogAttrs(shutdownCtx, slogd.LevelTrace, "shutting down http server completed", slog.String("listenAddress", s.Addr))
|
||||
close(idleConnectionsClosed)
|
||||
}(runCtx)
|
||||
|
||||
var err error
|
||||
if err = s.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
|
||||
// Error starting or closing listener:
|
||||
log.LogAttrs(ctx, slogd.LevelError, "http server start failed", slog.String("error", err.Error()))
|
||||
return err
|
||||
}
|
||||
|
||||
<-idleConnectionsClosed
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user