diff --git a/CHANGELOG.md b/CHANGELOG.md index 127f65b0882..63eb9ee1e1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ BUG FIXES: + * core: Fixed a bug that only ran a task `shutdown_delay` if the task had a registered service [[GH-7663](https://github.com/hashicorp/nomad/issues/7663)] * vault: Upgrade http2 library to fix Vault API calls that fail with `http2: no cached connection was available` [[GH-7673](https://github.com/hashicorp/nomad/issues/7673)] ## 0.11.0 (April 8, 2020) diff --git a/client/allocrunner/taskrunner/service_hook.go b/client/allocrunner/taskrunner/service_hook.go index eaf5cdb89d6..e329af93fd6 100644 --- a/client/allocrunner/taskrunner/service_hook.go +++ b/client/allocrunner/taskrunner/service_hook.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "sync" - "time" log "github.com/hashicorp/go-hclog" "github.com/hashicorp/nomad/client/allocrunner/interfaces" @@ -40,7 +39,6 @@ type serviceHook struct { logger log.Logger // The following fields may be updated - delay time.Duration driverExec tinterfaces.ScriptExecutor driverNet *drivers.DriverNetwork canary bool @@ -64,7 +62,6 @@ func newServiceHook(c serviceHookConfig) *serviceHook { taskName: c.task.Name, services: c.task.Services, restarter: c.restarter, - delay: c.task.ShutdownDelay, } if res := c.alloc.AllocatedResources.Tasks[c.task.Name]; res != nil { @@ -140,7 +137,6 @@ func (h *serviceHook) updateHookFields(req *interfaces.TaskUpdateRequest) error } // Update service hook fields - h.delay = task.ShutdownDelay h.taskEnv = req.TaskEnv h.services = task.Services h.networks = networks @@ -156,16 +152,6 @@ func (h *serviceHook) PreKilling(ctx context.Context, req *interfaces.TaskPreKil // Deregister before killing task h.deregister() - // If there's no shutdown delay, exit early - if h.delay == 0 { - return nil - } - - h.logger.Debug("waiting before killing task", "shutdown_delay", h.delay) - select { - case <-ctx.Done(): - case <-time.After(h.delay): - } return nil } diff --git a/client/allocrunner/taskrunner/task_runner.go b/client/allocrunner/taskrunner/task_runner.go index 43712f14094..592d70bed2d 100644 --- a/client/allocrunner/taskrunner/task_runner.go +++ b/client/allocrunner/taskrunner/task_runner.go @@ -844,6 +844,14 @@ func (tr *TaskRunner) handleKill() *drivers.ExitResult { // Run the pre killing hooks tr.preKill() + // Wait for task ShutdownDelay after running prekill hooks + // 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) + time.Sleep(delay) + } + // Tell the restart tracker that the task has been killed so it doesn't // attempt to restart it. tr.restartTracker.SetKilled()