Skip to content

Commit

Permalink
Stoplogic fix for default signal and shutdown console printing (#47)
Browse files Browse the repository at this point in the history
* start with this

* fix signal arg

* Make `^C` send SIGINT and not SIGTERM
  • Loading branch information
QuintenQVD0 authored Oct 27, 2024
1 parent 2508f85 commit 66f2c5f
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions environment/docker/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,17 @@ func (e *Environment) Stop(ctx context.Context) error {
// logic and end up only executing the container stop command (which may or
// may not work as expected).
if s.Type == "" || s.Type == remote.ProcessStopSignal {
if s.Type == "" {
log.WithField("container_id", e.Id).Warn("no stop configuration detected for environment, using termination procedure")
}

log.WithField("signal_value", s.Value).Debug("stopping server using signal")

var signal string
// Handle a few common cases, otherwise just fall through and use the default SIGKILL.
switch strings.ToUpper(s.Value) {
case "SIGABRT":
signal = "SIGABRT"
case "SIGINT":
case "SIGINT", "C":
signal = "SIGINT"
case "SIGTERM", "C":
case "SIGTERM":
signal = "SIGTERM"
default:
signal = "SIGKILL"
Expand All @@ -164,6 +163,7 @@ func (e *Environment) Stop(ctx context.Context) error {

// If the process is already offline don't switch it back to stopping. Just leave it how
// it is and continue through to the stop handling for the process.
// I'm not certain if this should still be here but it seems to work with it and without it so I leave it here.
if e.st.Load() != environment.ProcessOfflineState {
e.SetState(environment.ProcessStoppingState)
}
Expand All @@ -174,6 +174,10 @@ func (e *Environment) Stop(ctx context.Context) error {
return e.SendCommand(s.Value)
}

if s.Type == "" {
log.WithField("container_id", e.Id).Warn("no stop configuration detected for environment, using termination procedure")
}

// Allow the stop action to run for however long it takes, similar to executing a command
// and using a different logic pathway to wait for the container to stop successfully.
//
Expand Down Expand Up @@ -289,29 +293,10 @@ func (e *Environment) Terminate(ctx context.Context, signal string) error {
return nil
}

// Timeout (optional) is the timeout (in seconds) to wait for the container
// to stop gracefully before forcibly terminating it with SIGKILL.
//
// - Use nil to use the default timeout (10 seconds).
// - Use '-1' to wait indefinitely.
// - Use '0' to not wait for the container to exit gracefully, and
// immediately proceeds to forcibly terminating the container.
// - Other positive values are used as timeout (in seconds).
var noWaitTimeout int

// For every signal wait at max 10 seconds before SIGKILL is send (server did not stop in time)
// for SIGKILL just kill it without waiting
switch signal {
case "SIGKILL":
noWaitTimeout = 0
default:
noWaitTimeout = 10
}

// We set it to stopping then offline to prevent crash detection from being triggered.
e.SetState(environment.ProcessStoppingState)

if err := e.client.ContainerStop(ctx, e.Id, container.StopOptions{Timeout: &noWaitTimeout, Signal: signal}); err != nil && !client.IsErrNotFound(err) {
if err := e.client.ContainerKill(ctx, e.Id, signal); err != nil && !client.IsErrNotFound(err) {
return errors.WithStack(err)
}

Expand Down

0 comments on commit 66f2c5f

Please sign in to comment.