Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

taskreaper: Wait for tasks to stop running #1948

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions manager/orchestrator/taskreaper/task_reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,13 @@ func (tr *TaskReaper) tick() {
}

defer func() {
tr.dirty = make(map[instanceTuple]struct{})
tr.orphaned = nil
}()

deleteTasks := tr.orphaned
deleteTasks := make(map[string]struct{})
for _, tID := range tr.orphaned {
deleteTasks[tID] = struct{}{}
}
tr.store.View(func(tx store.ReadTx) {
for dirty := range tr.dirty {
service := store.GetService(tx, dirty.serviceID)
Expand Down Expand Up @@ -180,26 +182,31 @@ func (tr *TaskReaper) tick() {
// instead of sorting the whole slice.
sort.Sort(tasksByTimestamp(historicTasks))

runningTasks := 0
for _, t := range historicTasks {
if t.DesiredState <= api.TaskStateRunning {
if t.DesiredState <= api.TaskStateRunning || t.Status.State <= api.TaskStateRunning {
// Don't delete running tasks
runningTasks++
continue
}

deleteTasks = append(deleteTasks, t.ID)
deleteTasks[t.ID] = struct{}{}

taskHistory++
if int64(len(historicTasks)) <= taskHistory {
break
}
}

if runningTasks <= 1 {
delete(tr.dirty, dirty)
}
}
})

if len(deleteTasks) > 0 {
tr.store.Batch(func(batch *store.Batch) error {
for _, taskID := range deleteTasks {
for taskID := range deleteTasks {
batch.Update(func(tx store.Tx) error {
return store.DeleteTask(tx, taskID)
})
Expand Down