Skip to content

Commit

Permalink
Fix JENKINS-12912
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Mar 5, 2012
1 parent 676ef76 commit 092bff9
Showing 1 changed file with 101 additions and 13 deletions.
114 changes: 101 additions & 13 deletions src/main/java/org/jenkinsci/lib/xtrigger/AbstractTrigger.java
Expand Up @@ -27,6 +27,8 @@ public abstract class AbstractTrigger extends Trigger<BuildableItem> implements

protected static Logger LOGGER = Logger.getLogger(AbstractTrigger.class.getName());

private String triggerLabel;

protected transient boolean offlineSlaveOnStartup = false;

/**
Expand All @@ -40,6 +42,25 @@ public AbstractTrigger(String cronTabSpec) throws ANTLRException {
super(cronTabSpec);
}

/**
* Builds a trigger object
* Calls an implementation trigger
*
* @param cronTabSpec the scheduler value
* @param triggerLabel the trigger label to restrictbox where the poll to run
* @throws antlr.ANTLRException
*/

public AbstractTrigger(String cronTabSpec, String triggerLabel) throws ANTLRException {
super(cronTabSpec);
this.triggerLabel = Util.fixEmpty(triggerLabel);
}

@SuppressWarnings("unused")
public String getTriggerLabel() {
return triggerLabel;
}

/**
* Gets the triggering log file
*
Expand Down Expand Up @@ -260,17 +281,59 @@ private List<Node> getPollingNodeListWithExecutors(XTriggerLog log) {
}

private List<Node> getPollingNodeList(XTriggerLog log) {
log.info("Looking nodes where the polling can be run.");

List<Node> nodes;
if (requiresWorkspaceForPolling()) {
AbstractProject project = (AbstractProject) job;
Node lastBuildOnNode = project.getLastBuiltOn();
if (lastBuildOnNode == null) {
return candidatePollingNode(log);
}
return Arrays.asList(lastBuildOnNode);
nodes = getPollingNodeListRequiredWS(log);
} else {
nodes = getPollingNodeListRequiredNoWS(log);
}

if (nodes == null || nodes.size() == 0) {
log.info("Trying to poll on master node.");
nodes = Arrays.asList(getMasterNode());
}

return nodes;
}

private List<Node> getPollingNodeListRequiredNoWS(XTriggerLog log) {
log.info("Poll doesn't requires a workspace.");

AbstractProject project = (AbstractProject) job;

//The specified trigger node must be considered first
if (triggerLabel != null) {
log.info(String.format("Looking for a polling node to the restricted label %s.", triggerLabel));
Label targetLabel = Hudson.getInstance().getLabel(triggerLabel);
return getNodesLabel(project, targetLabel);
}

return candidatePollingNode(log);
}

private List<Node> getPollingNodeListRequiredWS(XTriggerLog log) {
log.info("Poll requires a workspace.");

AbstractProject project = (AbstractProject) job;

//The specified trigger node must be considered first
if (triggerLabel != null) {
log.info(String.format("Looking for a polling node to the restricted label %s.", triggerLabel));
Label targetLabel = Hudson.getInstance().getLabel(triggerLabel);
return getNodesLabel(project, targetLabel);
}

//Search for the last built on
log.info("Looking for the last built on node.");
Node lastBuildOnNode = project.getLastBuiltOn();
if (lastBuildOnNode == null) {
return getPollingNodeNoPreviousBuild(log);
}
return Arrays.asList(lastBuildOnNode);
}

private boolean eligibleNode(Node node) {
if (node == null) {
return false;
Expand All @@ -283,22 +346,47 @@ private boolean eligibleNode(Node node) {
return node.getNumExecutors() != 0;
}

private List<Node> getPollingNodeNoPreviousBuild(XTriggerLog log) {
AbstractProject project = (AbstractProject) job;
Label targetLabel = getTargetLabel(log);
if (targetLabel != null) {
return getNodesLabel(project, targetLabel);
}
return null;
}

private List<Node> candidatePollingNode(XTriggerLog log) {
AbstractProject p = (AbstractProject) job;
Label label = p.getAssignedLabel();
if (label == null) {
AbstractProject project = (AbstractProject) job;
log.info("Looking for a candidate node to run the poll.");
AbstractProject project = (AbstractProject) job;
Label targetLabel = getTargetLabel(log);
if (targetLabel != null) {
return getNodesLabel(project, targetLabel);
} else {
log.info("Looking for a polling node with no predefined label.");
log.info("Trying to poll with the last built on node.");
Node lastBuildOnNode = project.getLastBuiltOn();
if (lastBuildOnNode == null) {
log.info("Trying to poll on master node.");
return Arrays.asList(getMasterNode());
}
return Arrays.asList(lastBuildOnNode);
} else {
log.info(String.format("Searching a node to run the polling for the label '%s'.", label));
return getNodesLabel(p, label);
}
}

/**
* Returns the label if any to poll
*/
private Label getTargetLabel(XTriggerLog log) {
AbstractProject p = (AbstractProject) job;
Label assignedLabel = p.getAssignedLabel();
if (assignedLabel != null) {
log.info(String.format("Looking for a polling node with the assigned project label %s.", assignedLabel));
return assignedLabel;
}

return null;
}

private Node getMasterNode() {
Computer computer = Hudson.getInstance().toComputer();
if (computer != null) {
Expand Down

0 comments on commit 092bff9

Please sign in to comment.