Skip to content

Commit

Permalink
task lifecycle: fix poststop functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzyfresh committed Aug 26, 2020
1 parent 810ca4b commit 13ad551
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
2 changes: 2 additions & 0 deletions client/allocrunner/alloc_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ func (ar *allocRunner) handleTaskStateUpdates() {
killEvent = structs.NewTaskEvent(structs.TaskMainDead)
}



// If there's a kill event set and live runners, kill them
if killEvent != nil && len(liveRunners) > 0 {

Expand Down
25 changes: 6 additions & 19 deletions client/allocrunner/task_hook_coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type taskHookCoordinator struct {
prestartEphemeral map[string]struct{}
mainTasksRunning map[string]struct{} // poststop: main tasks running -> finished
mainTasksPending map[string]struct{} // poststart: main tasks pending -> running
poststopTasks map[string]struct{}
}

func newTaskHookCoordinator(logger hclog.Logger, tasks []*structs.Task) *taskHookCoordinator {
Expand All @@ -51,7 +50,6 @@ func newTaskHookCoordinator(logger hclog.Logger, tasks []*structs.Task) *taskHoo
prestartEphemeral: map[string]struct{}{},
mainTasksRunning: map[string]struct{}{},
mainTasksPending: map[string]struct{}{},
poststopTasks: map[string]struct{}{},
poststartTaskCtx: poststartTaskCtx,
poststartTaskCtxCancel: poststartCancelFn,
poststopTaskCtx: poststopTaskCtx,
Expand Down Expand Up @@ -80,8 +78,7 @@ func (c *taskHookCoordinator) setTasks(tasks []*structs.Task) {
case structs.TaskLifecycleHookPoststart:
// Poststart hooks don't need to be tracked.
case structs.TaskLifecycleHookPoststop:
// TODO: we don't need to track poststop tasks
c.poststopTasks[task.Name] = struct{}{}
// Poststop hooks don't need to be tracked.
default:
c.logger.Error("invalid lifecycle hook", "task", task.Name, "hook", task.Lifecycle.Hook)
}
Expand All @@ -90,10 +87,6 @@ func (c *taskHookCoordinator) setTasks(tasks []*structs.Task) {
if !c.hasPrestartTasks() {
c.mainTaskCtxCancel()
}
if !c.hasRunningMainTasks() {
c.poststopTaskCtxCancel()
}

}

func (c *taskHookCoordinator) hasPrestartTasks() bool {
Expand Down Expand Up @@ -122,6 +115,8 @@ func (c *taskHookCoordinator) startConditionForTask(task *structs.Task) <-chan s
case structs.TaskLifecycleHookPoststop:
return c.poststopTaskCtx.Done()
default:
// it should never have a lifecycle stanza w/o a hook, so report an error but allow the task to start normally
c.logger.Error("invalid lifecycle hook", "task", task.Name, "hook", task.Lifecycle.Hook)
return c.mainTaskCtx.Done()
}
}
Expand All @@ -134,6 +129,7 @@ func (c *taskHookCoordinator) taskStateUpdated(states map[string]*structs.TaskSt
continue
}


delete(c.prestartSidecar, task)
}

Expand All @@ -148,23 +144,14 @@ func (c *taskHookCoordinator) taskStateUpdated(states map[string]*structs.TaskSt

for task := range c.mainTasksRunning {
st := states[task]
if st == nil || !st.Successful() || st.State == structs.TaskStateDead {

if st == nil || st.State != structs.TaskStateDead {
continue
}

delete(c.mainTasksRunning, task)
}

// TODO: delete
//for task := range c.poststopTasks {
// st := states[task]
// if st == nil || !st.Successful() {
// continue
// }
//
// delete(c.poststopTasks, task)
//}

for task := range c.mainTasksPending {
st := states[task]
if st == nil || st.StartedAt.IsZero() {
Expand Down

0 comments on commit 13ad551

Please sign in to comment.