Skip to content

Commit

Permalink
kill taskrunners if all tasks are dead
Browse files Browse the repository at this point in the history
  • Loading branch information
lgfa29 committed Aug 16, 2022
1 parent 464279e commit 4081bc7
Showing 1 changed file with 50 additions and 29 deletions.
79 changes: 50 additions & 29 deletions client/allocrunner/alloc_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,40 +546,61 @@ func (ar *allocRunner) handleTaskStateUpdates() {
}
}

// if all live runners are sidecars - kill alloc
if killEvent == nil && hasSidecars && !hasNonSidecarTasks(liveRunners) {
killEvent = structs.NewTaskEvent(structs.TaskMainDead)
}
if len(liveRunners) == 0 {
// If there are not live runners left kill all task runners to
// unblock them from the alloc restart block.
for _, tr := range ar.tasks {
if tr.IsPoststopTask() {
continue
}

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

// Log kill reason
switch killEvent.Type {
case structs.TaskLeaderDead:
ar.logger.Debug("leader task dead, destroying all tasks", "leader_task", killTask)
case structs.TaskMainDead:
ar.logger.Debug("main tasks dead, destroying all sidecar tasks")
default:
ar.logger.Debug("task failure, destroying all tasks", "failed_task", killTask)
select {
case <-tr.WaitCh():
default:
taskEvent := structs.NewTaskEvent(structs.TaskKilling)
taskEvent.SetKillTimeout(tr.Task().KillTimeout, ar.clientConfig.MaxKillTimeout)
err := tr.Kill(context.TODO(), taskEvent)
if err != nil {
ar.logger.Warn("failed to kill task", "task", tr.Task().Name, "error", err)
}
}
}

// Emit kill event for live runners
for _, tr := range liveRunners {
tr.EmitEvent(killEvent)
} else {
// if all live runners are sidecars - kill alloc
if killEvent == nil && hasSidecars && !hasNonSidecarTasks(liveRunners) {
killEvent = structs.NewTaskEvent(structs.TaskMainDead)
}

// Kill 'em all
states = ar.killTasks()
// If there's a kill event set and live runners, kill them
if killEvent != nil {

// Log kill reason
switch killEvent.Type {
case structs.TaskLeaderDead:
ar.logger.Debug("leader task dead, destroying all tasks", "leader_task", killTask)
case structs.TaskMainDead:
ar.logger.Debug("main tasks dead, destroying all sidecar tasks")
default:
ar.logger.Debug("task failure, destroying all tasks", "failed_task", killTask)
}

// Wait for TaskRunners to exit before continuing to
// prevent looping before TaskRunners have transitioned
// to Dead.
for _, tr := range liveRunners {
ar.logger.Info("killing task", "task", tr.Task().Name)
select {
case <-tr.WaitCh():
case <-ar.waitCh:
// Emit kill event for live runners
for _, tr := range liveRunners {
tr.EmitEvent(killEvent)
}

// Kill 'em all
states = ar.killTasks()

// Wait for TaskRunners to exit before continuing to
// prevent looping before TaskRunners have transitioned
// to Dead.
for _, tr := range liveRunners {
ar.logger.Info("killing task", "task", tr.Task().Name)
select {
case <-tr.WaitCh():
case <-ar.waitCh:
}
}
}
}
Expand Down

0 comments on commit 4081bc7

Please sign in to comment.