Ejemplo n.º 1
0
func (as *AgentState) GetReplacedUnit(j *job.Job) (string, error) {
	cExists, replaced := as.hasReplace(j.Name, j.Replaces())
	if !cExists {
		return "", fmt.Errorf("cannot find units to be replaced for Unit(%s)", j.Name)
	}
	return replaced, nil
}
Ejemplo n.º 2
0
// AbleToRun determines if an Agent can run the provided Job based on
// the Agent's current state. A boolean indicating whether this is the
// case or not is returned. The following criteria is used:
//   - Agent must meet the Job's machine target requirement (if any)
//   - Agent must have all of the Job's required metadata (if any)
//   - Agent must have all required Peers of the Job scheduled locally (if any)
//   - Job must not conflict with any other Units scheduled to the agent
//   - Job must specially handle replaced units to be rescheduled
func (as *AgentState) AbleToRun(j *job.Job) (jobAction job.JobAction, errstr string) {
	if tgt, ok := j.RequiredTarget(); ok && !as.MState.MatchID(tgt) {
		return job.JobActionUnschedule, fmt.Sprintf("agent ID %q does not match required %q", as.MState.ID, tgt)
	}

	metadata := j.RequiredTargetMetadata()
	if len(metadata) != 0 {
		if !machine.HasMetadata(as.MState, metadata) {
			return job.JobActionUnschedule, "local Machine metadata insufficient"
		}
	}

	peers := j.Peers()
	if len(peers) != 0 {
		for _, peer := range peers {
			if !as.unitScheduled(peer) {
				return job.JobActionUnschedule, fmt.Sprintf("required peer Unit(%s) is not scheduled locally", peer)
			}
		}
	}

	if cExists, cJobName := as.HasConflict(j.Name, j.Conflicts()); cExists {
		return job.JobActionUnschedule, fmt.Sprintf("found conflict with locally-scheduled Unit(%s)", cJobName)
	}

	// Handle Replace option specially for rescheduling the unit
	if cExists, cJobName := as.hasReplace(j.Name, j.Replaces()); cExists {
		return job.JobActionReschedule, fmt.Sprintf("found replace with locally-scheduled Unit(%s)", cJobName)
	}

	return job.JobActionSchedule, ""
}