gearman-plugin/src/main/java/hudson/plugins/gearman/ExecutorWorkerThread.java

200 lines
6.6 KiB
Java

/*
*
* Copyright 2013 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package hudson.plugins.gearman;
import hudson.model.AbstractProject;
import hudson.model.Label;
import hudson.model.Node;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import jenkins.model.Jenkins;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This is the thread to run gearman executors
* Executors are used to initiate jenkins builds
*
* @author Khai Do
*
*/
public class ExecutorWorkerThread extends AbstractWorkerThread{
private static final Logger logger = LoggerFactory
.getLogger(Constants.PLUGIN_LOGGER_NAME);
private final Node node;
// constructor
public ExecutorWorkerThread(String host, int port, Node node) {
super(host, port);
this.node = node;
}
public ExecutorWorkerThread(String host, int port, String name, Node node) {
super(host, port, name);
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) {
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. Works for free-style
* and maven projects but does not work for multi-config projects
*
* 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 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 on precise-123
* build:pep8:precise on precise-129
* build:pep8 on precise-129
*
* - If the project contains multiple labels separated by '||' then
* we register with the nodes that contain the corresponding labels
*
* build:pep8:precise on precise-123
* build:pep8 on precise-123
* build:pep8:precise on precise-129
* build:pep8 on precise-129
* build:pep8:oneiric on oneiric-456
* build:pep8 on oneiric-456
* build:pep8:oneiric on oneiric-459
* build:pep8 on oneiric-459
*
*/
@Override
public void registerJobs() {
logger.info("---- Registering executor jobs on " + name + " ----");
/*
* We start with an empty worker.
*/
worker.unregisterAll();
if (this.node.toComputer().isOffline()) {
return;
}
/*
* Now register or re-register all functions.
*/
List<AbstractProject> allProjects = Jenkins.getInstance().getAllItems(AbstractProject.class);
for (AbstractProject<?, ?> project : allProjects) {
if (project.isDisabled()) { // ignore all disabled projects
continue;
}
String projectName = project.getName();
Label label = project.getAssignedLabel();
if (label == null) { // project has no label -> so register
// "build:projectName" on all nodes
String jobFunctionName = "build:" + projectName;
logger.info("---- Registering job " + jobFunctionName + " on "
+ name);
worker.registerFunctionFactory(new CustomGearmanFunctionFactory(
jobFunctionName, StartJobWorker.class.getName(),
project, this.node));
} else { // register "build:$projectName:$projectLabel" if this
// node matches a node from the project label
Set<Node> projectLabelNodes = label.getNodes();
String projectLabelString = label.getExpression();
Set<String> projectLabels = tokenizeLabelString(
projectLabelString, "\\|\\|");
// iterate thru all project labels and find matching nodes
for (String projectLabel : projectLabels) {
if (projectLabelNodes.contains(this.node)) {
String jobFunctionName = "build:" + projectName
+ ":" + projectLabel;
logger.info("---- Registering job " + jobFunctionName
+ " on " + this.node.getNodeName());
// register with label (i.e. "build:$projectName:$projectLabel")
worker.registerFunctionFactory(new CustomGearmanFunctionFactory(
jobFunctionName, StartJobWorker.class
.getName(), project, this.node));
// also register without label (i.e. "build:$projectName")
worker.registerFunctionFactory(new CustomGearmanFunctionFactory(
"build:" + projectName, StartJobWorker.class
.getName(), project, this.node));
}
}
}
}
}
}