Skip to content

Commit

Permalink
client: don't emit task shutdown delay event if not waiting (#16281)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgfa29 committed Mar 3, 2023
1 parent b24dddc commit b07af57
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/16281.txt
@@ -0,0 +1,3 @@
```release-note:bug
client: Don't emit shutdown delay task event when the shutdown operation is configured to skip the delay
```
14 changes: 10 additions & 4 deletions client/allocrunner/taskrunner/task_runner.go
Expand Up @@ -985,10 +985,16 @@ func (tr *TaskRunner) handleKill(resultCh <-chan *drivers.ExitResult) *drivers.E
// This allows for things like service de-registration to run
// before waiting to kill task
if delay := tr.Task().ShutdownDelay; delay != 0 {
tr.logger.Debug("waiting before killing task", "shutdown_delay", delay)

ev := structs.NewTaskEvent(structs.TaskWaitingShuttingDownDelay).
SetDisplayMessage(fmt.Sprintf("Waiting for shutdown_delay of %s before killing the task.", delay))
var ev *structs.TaskEvent
if tr.alloc.DesiredTransition.ShouldIgnoreShutdownDelay() {
tr.logger.Debug("skipping shutdown_delay", "shutdown_delay", delay)
ev = structs.NewTaskEvent(structs.TaskSkippingShutdownDelay).
SetDisplayMessage(fmt.Sprintf("Skipping shutdown_delay of %s before killing the task.", delay))
} else {
tr.logger.Debug("waiting before killing task", "shutdown_delay", delay)
ev = structs.NewTaskEvent(structs.TaskWaitingShuttingDownDelay).
SetDisplayMessage(fmt.Sprintf("Waiting for shutdown_delay of %s before killing the task.", delay))
}
tr.UpdateState(structs.TaskStatePending, ev)

select {
Expand Down
12 changes: 11 additions & 1 deletion client/allocrunner/taskrunner/task_runner_test.go
Expand Up @@ -1107,7 +1107,17 @@ func TestTaskRunner_NoShutdownDelay(t *testing.T) {
}

err := <-killed
require.NoError(t, err, "killing task returned unexpected error")
must.NoError(t, err)

// Check that we only emit the expected events.
hasEvent := false
for _, ev := range tr.state.Events {
must.NotEq(t, structs.TaskWaitingShuttingDownDelay, ev.Type)
if ev.Type == structs.TaskSkippingShutdownDelay {
hasEvent = true
}
}
must.True(t, hasEvent)
}

// TestTaskRunner_Dispatch_Payload asserts that a dispatch job runs and the
Expand Down
4 changes: 4 additions & 0 deletions nomad/structs/structs.go
Expand Up @@ -8348,6 +8348,10 @@ const (
// TaskWaitingShuttingDownDelay indicates that the task is waiting for
// shutdown delay before being TaskKilled
TaskWaitingShuttingDownDelay = "Waiting for shutdown delay"

// TaskSkippingShutdownDelay indicates that the task operation was
// configured to ignore the shutdown delay value set for the tas.
TaskSkippingShutdownDelay = "Skipping shutdown delay"
)

// TaskEvent is an event that effects the state of a task and contains meta-data
Expand Down

0 comments on commit b07af57

Please sign in to comment.