diff --git a/client/allocrunner/interfaces/task_lifecycle.go b/client/allocrunner/interfaces/task_lifecycle.go index 2b3ed91d5..01d17dfbf 100644 --- a/client/allocrunner/interfaces/task_lifecycle.go +++ b/client/allocrunner/interfaces/task_lifecycle.go @@ -114,14 +114,14 @@ type TaskPoststartHook interface { Poststart(context.Context, *TaskPoststartRequest, *TaskPoststartResponse) error } -type TaskKillRequest struct{} -type TaskKillResponse struct{} +type TaskPreKillRequest struct{} +type TaskPreKillResponse struct{} -type TaskKillHook interface { +type TaskPreKillHook interface { TaskHook - // Killing is called when a task is going to be Killed or Restarted. - Killing(context.Context, *TaskKillRequest, *TaskKillResponse) error + // PreKilling is called right before a task is going to be killed or restarted. + PreKilling(context.Context, *TaskPreKillRequest, *TaskPreKillResponse) error } type TaskExitedRequest struct{} diff --git a/client/allocrunner/taskrunner/lifecycle.go b/client/allocrunner/taskrunner/lifecycle.go index ac208e723..e4c84506e 100644 --- a/client/allocrunner/taskrunner/lifecycle.go +++ b/client/allocrunner/taskrunner/lifecycle.go @@ -22,8 +22,8 @@ func (tr *TaskRunner) Restart(ctx context.Context, event *structs.TaskEvent, fai // Emit the event since it may take a long time to kill tr.EmitEvent(event) - // Run the hooks prior to restarting the task - tr.killing() + // Run the pre-kill hooks prior to restarting the task + tr.preKill() // Tell the restart tracker that a restart triggered the exit tr.restartTracker.SetRestartTriggered(failure) diff --git a/client/allocrunner/taskrunner/service_hook.go b/client/allocrunner/taskrunner/service_hook.go index 55bd0b60e..f9e4ebb24 100644 --- a/client/allocrunner/taskrunner/service_hook.go +++ b/client/allocrunner/taskrunner/service_hook.go @@ -140,7 +140,7 @@ func (h *serviceHook) Update(ctx context.Context, req *interfaces.TaskUpdateRequ return h.consul.UpdateTask(oldTaskServices, newTaskServices) } -func (h *serviceHook) Killing(ctx context.Context, req *interfaces.TaskKillRequest, resp *interfaces.TaskKillResponse) error { +func (h *serviceHook) Killing(ctx context.Context, req *interfaces.TaskPreKillRequest, resp *interfaces.TaskPreKillResponse) error { h.mu.Lock() defer h.mu.Unlock() @@ -200,7 +200,7 @@ func (h *serviceHook) getTaskServices() *agentconsul.TaskServices { // values from the task's environment. func interpolateServices(taskEnv *taskenv.TaskEnv, services []*structs.Service) []*structs.Service { // Guard against not having a valid taskEnv. This can be the case if the - // Killing or Exited hook is run before post-run. + // PreKilling or Exited hook is run before Poststart. if taskEnv == nil || len(services) == 0 { return nil } diff --git a/client/allocrunner/taskrunner/task_runner.go b/client/allocrunner/taskrunner/task_runner.go index 5d22a6965..aff53994d 100644 --- a/client/allocrunner/taskrunner/task_runner.go +++ b/client/allocrunner/taskrunner/task_runner.go @@ -704,11 +704,11 @@ func (tr *TaskRunner) initDriver() error { } // handleKill is used to handle the a request to kill a task. It will return -// the handle exit result if one is available and store any error in the task -// runner killErr value. +//// the handle exit result if one is available and store any error in the task +//// runner killErr value. func (tr *TaskRunner) handleKill() *drivers.ExitResult { - // Run the hooks prior to killing the task - tr.killing() + // Run the pre killing hooks + tr.preKill() // Tell the restart tracker that the task has been killed so it doesn't // attempt to restart it. diff --git a/client/allocrunner/taskrunner/task_runner_hooks.go b/client/allocrunner/taskrunner/task_runner_hooks.go index 88b10cd57..f26390faf 100644 --- a/client/allocrunner/taskrunner/task_runner_hooks.go +++ b/client/allocrunner/taskrunner/task_runner_hooks.go @@ -437,36 +437,38 @@ func (tr *TaskRunner) updateHooks() { } } -// killing is used to run the runners kill hooks. -func (tr *TaskRunner) killing() { +// preKill is used to run the runners preKill hooks +// preKill hooks contain logic that must be executed before +// a task is killed or restarted +func (tr *TaskRunner) preKill() { if tr.logger.IsTrace() { start := time.Now() - tr.logger.Trace("running kill hooks", "start", start) + tr.logger.Trace("running pre kill hooks", "start", start) defer func() { end := time.Now() - tr.logger.Trace("finished kill hooks", "end", end, "duration", end.Sub(start)) + tr.logger.Trace("finished pre kill hooks", "end", end, "duration", end.Sub(start)) }() } for _, hook := range tr.runnerHooks { - killHook, ok := hook.(interfaces.TaskKillHook) + killHook, ok := hook.(interfaces.TaskPreKillHook) if !ok { continue } name := killHook.Name() - // Time the update hook + // Time the pre kill hook var start time.Time if tr.logger.IsTrace() { start = time.Now() tr.logger.Trace("running kill hook", "name", name, "start", start) } - // Run the kill hook - req := interfaces.TaskKillRequest{} - var resp interfaces.TaskKillResponse - if err := killHook.Killing(context.Background(), &req, &resp); err != nil { + // Run the pre kill hook + req := interfaces.TaskPreKillRequest{} + var resp interfaces.TaskPreKillResponse + if err := killHook.PreKilling(context.Background(), &req, &resp); err != nil { tr.emitHookError(err, name) tr.logger.Error("kill hook failed", "name", name, "error", err) }