Refactor application
Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
This commit is contained in:
@ -43,46 +43,23 @@ func (a *application) ExecuteContext(ctx context.Context) error {
|
|||||||
// Result channel for command output
|
// Result channel for command output
|
||||||
chExe := make(chan error)
|
chExe := make(chan error)
|
||||||
|
|
||||||
// Run the application command
|
// Run the application command using the signal context and output channel
|
||||||
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "executing application context")
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "executing application context")
|
||||||
go func(ctx context.Context, chErr chan error) {
|
go func(ctx context.Context, chErr chan error) {
|
||||||
chErr <- a.cmd.ExecuteContext(ctx)
|
chErr <- a.cmd.ExecuteContext(ctx)
|
||||||
}(sigCtx, chExe)
|
}(sigCtx, chExe)
|
||||||
|
|
||||||
// Wait for command output or a shutdown signal
|
// Wait for command output or a shutdown signal
|
||||||
var err error
|
|
||||||
select {
|
select {
|
||||||
// sigCtx.Done() returns a channel that will have a message when the context is canceled.
|
case <-sigCtx.Done(): // 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():
|
|
||||||
sigCancel()
|
sigCancel()
|
||||||
return a.handleShutdownSignal(ctx)
|
return a.handleShutdownSignal(ctx)
|
||||||
case err = <-chExe:
|
case err := <-chExe: // Alternatively, chExe will receive the response from the execution context if the application finishes.
|
||||||
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "application terminated successfully")
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "application terminated successfully")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
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)
|
fmt.Printf("waiting %s for graceful application shutdown... PRESS CTRL+C again to quit now!\n", a.config.ShutdownTimeout)
|
||||||
|
|
||||||
@ -94,11 +71,37 @@ func (a *application) gracefulShutdown(ctx context.Context) error {
|
|||||||
defer sigCancel() // Ensure that this gets called.
|
defer sigCancel() // Ensure that this gets called.
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-shutdownCtx.Done():
|
case <-shutdownCtx.Done(): // Timeout exceeded
|
||||||
return shutdownCtx.Err()
|
return shutdownCtx.Err()
|
||||||
case <-sigCtx.Done():
|
case <-sigCtx.Done(): // Received additional shutdown signal to forcefully exit
|
||||||
fmt.Println("exiting...")
|
fmt.Println("exiting...")
|
||||||
sigCancel()
|
sigCancel()
|
||||||
return fmt.Errorf("process killed")
|
return fmt.Errorf("process killed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *application) handleGracefulShutdown(ctx context.Context) error {
|
||||||
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "gracefully shutting down application")
|
||||||
|
|
||||||
|
var err error
|
||||||
|
if err = a.gracefulShutdown(ctx); !errors.Is(err, context.DeadlineExceeded) {
|
||||||
|
a.config.Logger.LogAttrs(ctx, slogd.LevelWarn, "graceful shutdown failed", slog.Any("error", err))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "graceful shutdown completed")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *application) handleShutdownSignal(ctx context.Context) error {
|
||||||
|
// Adapt the shutdown scenario if a graceful shutdown period is configured
|
||||||
|
switch a.config.EnableGracefulShutdown && a.config.ShutdownTimeout > 0 {
|
||||||
|
case true:
|
||||||
|
return a.handleGracefulShutdown(ctx)
|
||||||
|
case false:
|
||||||
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "immediately shutting down application")
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
panic("cannot handle shutdown signal")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user