code: func (u *Updater) removeOldTasks(ctx context.Context, batch *store.Batch, removeTasks []*api.Task) (*api.Task, error) {
  	var (
  		lastErr     error
  		removedTask *api.Task
  	)
  	for _, original := range removeTasks {
  		if original.DesiredState > api.TaskStateRunning {
  			continue
  		}
  		err := batch.Update(func(tx store.Tx) error {
  			t := store.GetTask(tx, original.ID)
  			if t == nil {
  				return fmt.Errorf("task %s not found while trying to shut it down", original.ID)
  			}
  			if t.DesiredState > api.TaskStateRunning {
  				return fmt.Errorf("task %s was already shut down when reached by updater", original.ID)
  			}
  			t.DesiredState = api.TaskStateShutdown
  			return store.UpdateTask(tx, t)
  		})
  		if err != nil {
  			lastErr = err
  		} else {
  			removedTask = original
  		}
  	}
  
  	if removedTask == nil {
  		return nil, lastErr
  	}
  	return removedTask, nil
  }
docs: // removeOldTasks shuts down the given tasks and returns one of the tasks that
  // was shut down, or an error.