code: func (s *Scheduler) findJobToStop(f *Formation, typ string) (*Job, error) {
  	var found *Job
  	for _, job := range s.jobs.WithFormationAndType(f, typ) {
  		switch job.State {
  		case JobStatePending:
  			return job, nil
  		case JobStateStarting, JobStateRunning:
  			// if the job is on a host which is shutting down,
  			// return it (it is about to stop anyway, and this
  			// avoids a race where modifying the omni counts to
  			// remove a shut down host could cause a subsequent
  			// rectify to stop a job on an active host before the
  			// shutting down host's job has stopped)
  			if host, ok := s.hosts[job.HostID]; ok && host.Shutdown {
  				return job, nil
  			}
  
  			// return the most recent job (which is the first in
  			// the slice we are iterating over) if none of the
  			// above cases match, preferring starting jobs to
  			// running ones
  			if found == nil || found.State == JobStateRunning && job.State == JobStateStarting {
  				found = job
  			}
  		}
  	}
  	if found == nil {
  		return nil, fmt.Errorf("no %s jobs running", typ)
  	}
  	return found, nil
  }
docs: // findJobToStop finds a job from the given formation and type which should be
  // stopped, choosing pending jobs if present, and the most recently started job
  // otherwise