fix gearman function registration bug
The gearman function registration was registering functions on nodes multiple times. Added a check to make sure nodes on the label matched the node in the executor before registering the function. Also added better documentation and helper functions. Change-Id: Id13bbfe0c003a82e1e311736c5ff7a682e5efca1
This commit is contained in:
parent
e6d78ad75f
commit
bac87b1bf2
|
@ -18,19 +18,21 @@
|
||||||
|
|
||||||
package hudson.plugins.gearman;
|
package hudson.plugins.gearman;
|
||||||
|
|
||||||
import hudson.model.Computer;
|
|
||||||
import hudson.model.Label;
|
import hudson.model.Label;
|
||||||
import hudson.model.Node;
|
import hudson.model.Node;
|
||||||
import hudson.model.Project;
|
import hudson.model.Project;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import jenkins.model.Jenkins;
|
import jenkins.model.Jenkins;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This is thread to run gearman executors
|
* This is thread to run gearman executors
|
||||||
* Executors are used to initiate jenkins builds
|
* Executors are used to initiate jenkins builds
|
||||||
|
@ -48,9 +50,15 @@ public class ExecutorWorkerThread extends AbstractWorkerThread{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
|
/**
|
||||||
* This function finds the node with the corresponding node name Returns the
|
* This function finds the node with the corresponding node name Returns the
|
||||||
* node if found, otherwise returns null
|
* node if found, otherwise returns null
|
||||||
|
*
|
||||||
|
* @param name
|
||||||
|
* The name of the jenkins node.
|
||||||
|
* @return
|
||||||
|
* The jenkins node if found, otherwise null
|
||||||
*/
|
*/
|
||||||
private Node findNode(String nodeName){
|
private Node findNode(String nodeName){
|
||||||
|
|
||||||
|
@ -70,9 +78,79 @@ public class ExecutorWorkerThread extends AbstractWorkerThread{
|
||||||
public ExecutorWorkerThread(String host, int port, String name, Node node){
|
public ExecutorWorkerThread(String host, int port, String name, Node node){
|
||||||
super(host, port, name);
|
super(host, port, name);
|
||||||
this.node = node;
|
this.node = node;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function tokenizes the labels in a label string
|
||||||
|
* that is set in the jenkins projects
|
||||||
|
*
|
||||||
|
* @param label
|
||||||
|
* The label string.
|
||||||
|
* @param pattern
|
||||||
|
* The pattern for tokenizing the label.
|
||||||
|
* @return
|
||||||
|
* A list of labels, the list can be empty
|
||||||
|
*/
|
||||||
|
private Set<String> tokenizeLabelString(String label, String pattern) {
|
||||||
|
|
||||||
|
Set<String> labelSet = new HashSet<String>();
|
||||||
|
|
||||||
|
if (pattern == null) {
|
||||||
|
return labelSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pattern.isEmpty()) {
|
||||||
|
return labelSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (label != null) {
|
||||||
|
|
||||||
|
// String projectLabelString = label.getExpression();
|
||||||
|
Scanner slabel = new Scanner(label);
|
||||||
|
try {
|
||||||
|
slabel.useDelimiter(pattern);
|
||||||
|
while (slabel.hasNext()) {
|
||||||
|
String newLabel = slabel.next();
|
||||||
|
labelSet.add(newLabel);
|
||||||
|
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
slabel.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return labelSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register gearman functions on this node. This will unregister all
|
||||||
|
* functions before registering new functions.
|
||||||
|
*
|
||||||
|
* How functions are registered:
|
||||||
|
* - If the project has no label then we register the project with all
|
||||||
|
* nodes
|
||||||
|
*
|
||||||
|
* build:pep8 on precise-123
|
||||||
|
* build:pep8 on precise-129
|
||||||
|
* build:pep8 on oneiric-456
|
||||||
|
*
|
||||||
|
* - If the project contains one label then we register with the node
|
||||||
|
* that contains the corresponding label. Labels with '&&' is
|
||||||
|
* considered just one label
|
||||||
|
*
|
||||||
|
* build:pep8:precise on precise-123
|
||||||
|
* build:pep8:precise on precise-129
|
||||||
|
* build:pep8:precise on precise-134
|
||||||
|
*
|
||||||
|
* - If the project contains multiple labels seperated by '||' then
|
||||||
|
* we register with the nodes that contain the corresponding labels
|
||||||
|
*
|
||||||
|
* build:pep8:precise on precise-123
|
||||||
|
* build:pep8:precise on precise-129
|
||||||
|
* build:pep8:oneiric on oneiric-456
|
||||||
|
* build:pep8:oneiric on oneiric-459
|
||||||
|
*
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void registerJobs() {
|
public void registerJobs() {
|
||||||
|
|
||||||
|
@ -93,49 +171,41 @@ public class ExecutorWorkerThread extends AbstractWorkerThread{
|
||||||
// List<AbstractProject> projects =
|
// List<AbstractProject> projects =
|
||||||
// jenkins.getAllItems(AbstractProject.class);
|
// jenkins.getAllItems(AbstractProject.class);
|
||||||
for (Project<?, ?> project : allProjects) {
|
for (Project<?, ?> project : allProjects) {
|
||||||
|
|
||||||
|
if (project.isDisabled()) { // ignore all disabled projects
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
String projectName = project.getName();
|
String projectName = project.getName();
|
||||||
|
Label label = project.getAssignedLabel();
|
||||||
|
|
||||||
// ignore all disabled projects
|
if (label == null) { // project has no label -> so register
|
||||||
if (!project.isDisabled()) {
|
// "build:projectName" on all nodes
|
||||||
|
String jobFunctionName = "build:" + projectName;
|
||||||
|
logger.info("Registering job " + jobFunctionName + " on "
|
||||||
|
+ this.node.getNodeName());
|
||||||
|
worker.registerFunctionFactory(new CustomGearmanFunctionFactory(
|
||||||
|
jobFunctionName, StartJobWorker.class.getName(),
|
||||||
|
project, this.node));
|
||||||
|
|
||||||
Label label = project.getAssignedLabel();
|
} else { // register "build:projectName:nodeName" on the
|
||||||
|
// node that has a matching label
|
||||||
|
|
||||||
if (label == null) { // project has no label -> so register
|
Set<Node> projectLabelNodes = label.getNodes();
|
||||||
// "build:projectName"
|
String projectLabelString = label.getExpression();
|
||||||
String jobFunctionName = "build:" + projectName;
|
Set<String> projectLabels = tokenizeLabelString(
|
||||||
logger.info("Registering job " + jobFunctionName + " on "
|
projectLabelString, "\\|\\|");
|
||||||
+ node.getNodeName());
|
|
||||||
worker.registerFunctionFactory(new CustomGearmanFunctionFactory(
|
|
||||||
jobFunctionName, StartJobWorker.class.getName(),
|
|
||||||
project, node));
|
|
||||||
|
|
||||||
} else { // register "build:projectName:nodeName"
|
// iterate thru all project labels and find matching nodes
|
||||||
|
for (String projectLabel : projectLabels) {
|
||||||
// make sure node is online
|
if (projectLabelNodes.contains(this.node)) {
|
||||||
Computer c = node.toComputer();
|
String jobFunctionName = "build:" + projectName
|
||||||
boolean nodeOnline = c.isOnline();
|
+ ":" + projectLabel;
|
||||||
|
logger.info("Registering job " + jobFunctionName
|
||||||
if (nodeOnline) {
|
+ " on " + this.node.getNodeName());
|
||||||
// get and iterate thru them to build the functions
|
worker.registerFunctionFactory(new CustomGearmanFunctionFactory(
|
||||||
// to register with the worker
|
jobFunctionName, StartJobWorker.class
|
||||||
String projectLabelString = label.getExpression();
|
.getName(), project, this.node));
|
||||||
Scanner projectLabels = new Scanner(projectLabelString);
|
|
||||||
try {
|
|
||||||
projectLabels.useDelimiter("\\|\\|");
|
|
||||||
while (projectLabels.hasNext()) {
|
|
||||||
String projectLabel = projectLabels.next();
|
|
||||||
String jobFunctionName = "build:" + projectName
|
|
||||||
+ ":" + projectLabel;
|
|
||||||
logger.info("Registering job "
|
|
||||||
+ jobFunctionName + " on "
|
|
||||||
+ node.getNodeName());
|
|
||||||
worker.registerFunctionFactory(new CustomGearmanFunctionFactory(
|
|
||||||
jobFunctionName, StartJobWorker.class
|
|
||||||
.getName(), project, node));
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
projectLabels.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue