From b07af5761846a59ac91e4826c9a0f8e38c91f70b Mon Sep 17 00:00:00 2001 From: Luiz Aoqui Date: Fri, 3 Mar 2023 18:22:06 -0500 Subject: [PATCH] client: don't emit task shutdown delay event if not waiting (#16281) --- .changelog/16281.txt | 3 +++ client/allocrunner/taskrunner/task_runner.go | 14 ++++++++++---- client/allocrunner/taskrunner/task_runner_test.go | 12 +++++++++++- nomad/structs/structs.go | 4 ++++ 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 .changelog/16281.txt diff --git a/.changelog/16281.txt b/.changelog/16281.txt new file mode 100644 index 00000000000..6f75fa15cc4 --- /dev/null +++ b/.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 +``` diff --git a/client/allocrunner/taskrunner/task_runner.go b/client/allocrunner/taskrunner/task_runner.go index e8107f19b4f..fd3bc1dd8fb 100644 --- a/client/allocrunner/taskrunner/task_runner.go +++ b/client/allocrunner/taskrunner/task_runner.go @@ -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 { diff --git a/client/allocrunner/taskrunner/task_runner_test.go b/client/allocrunner/taskrunner/task_runner_test.go index b3064f3af6e..94321472414 100644 --- a/client/allocrunner/taskrunner/task_runner_test.go +++ b/client/allocrunner/taskrunner/task_runner_test.go @@ -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 diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 71a9f4dd332..52d1eb0cc94 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -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