code: func (s *State) ensureTaskNumAndFlush(c context.Context, instanceID string, state *tsmon.State, settings *tsmonSettings) error {
  	var task target.Task
  	defTarget := state.Store().DefaultTarget()
  	if t, ok := defTarget.(*target.Task); ok {
  		task = *t
  	} else {
  		return fmt.Errorf("default tsmon target is not a Task (%T): %v", defTarget, defTarget)
  	}
  
  	// Notify the task number allocator that we are still alive and grab the
  	// TaskNum assigned to us.
  	assignedTaskNum, err := s.TaskNumAllocator.NotifyTaskIsAlive(c, &task, instanceID)
  	if err != nil && err != ErrNoTaskNumber {
  		return fmt.Errorf("failed to get task number assigned for %q - %s", instanceID, err)
  	}
  
  	// Don't do the flush if we still haven't got a task number.
  	if err == ErrNoTaskNumber {
  		if task.TaskNum >= 0 {
  			logging.Warningf(c, "The task was inactive for too long and lost its task number, clearing cumulative metrics")
  			state.ResetCumulativeMetrics(c)
  		}
  		task.TaskNum = -1
  		state.Store().SetDefaultTarget(&task)
  		return ErrNoTaskNumber
  	}
  
  	task.TaskNum = int32(assignedTaskNum)
  	state.Store().SetDefaultTarget(&task)
  	return s.doFlush(c, state, settings)
  }
docs: // ensureTaskNumAndFlush gets a task number assigned to the process and flushes
  // the metrics.
  //
  // Returns ErrNoTaskNumber if the task wasn't assigned a task number yet.