threads all plumbed thru
Threads all setup and plumbed thru to get gearman executors and management works running Change-Id: I0d6534c18b1dea23c02021afe2f325b03d3e9cb0
This commit is contained in:
parent
03a6f97c37
commit
38788bdf69
14
pom.xml
14
pom.xml
|
@ -60,6 +60,10 @@
|
|||
<id>repo.jenkins-ci.org</id>
|
||||
<url>http://repo.jenkins-ci.org/public/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>dev.nightlabs.org</id>
|
||||
<url>http://dev.nightlabs.org/maven-repository/repo/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<pluginRepositories>
|
||||
|
@ -334,5 +338,15 @@
|
|||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gearman</groupId>
|
||||
<artifactId>gearman-java</artifactId>
|
||||
<version>0.6-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.2.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -19,57 +19,74 @@
|
|||
package hudson.plugins.gearman;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gearman.common.GearmanNIOJobServerConnection;
|
||||
import org.gearman.worker.GearmanWorker;
|
||||
import org.gearman.worker.GearmanWorkerImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class AbstractWorkerThread implements Runnable {
|
||||
/*
|
||||
* Thread to run gearman worker
|
||||
*/
|
||||
|
||||
public static final String DEFAULT_NAME = "anonymous";
|
||||
public static final String DEFAULT_HOST = "localhost";
|
||||
public static final int DEFAULT_PORT = 4730;
|
||||
public abstract class AbstractWorkerThread implements Runnable {
|
||||
|
||||
public static final String DEFAULT_EXECUTOR_NAME = "anonymous";
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(AbstractWorkerThread.class);
|
||||
.getLogger(Constants.PLUGIN_EXECTUOR_LOGGER_NAME);
|
||||
|
||||
protected String host;
|
||||
protected int port;
|
||||
protected String name;
|
||||
protected GearmanWorker worker;
|
||||
private GearmanNIOJobServerConnection conn;
|
||||
private Thread thread;
|
||||
|
||||
public AbstractWorkerThread() {
|
||||
this(DEFAULT_HOST, DEFAULT_PORT, DEFAULT_NAME);
|
||||
}
|
||||
|
||||
public AbstractWorkerThread(String host, int port) {
|
||||
this(host, port, DEFAULT_NAME);
|
||||
}
|
||||
|
||||
public AbstractWorkerThread(String host, int port, String name) {
|
||||
this.name = name;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
worker = new GearmanWorkerImpl();
|
||||
conn = new GearmanNIOJobServerConnection(host, port);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Register jobs with the gearman worker. This method should be overriden.
|
||||
*/
|
||||
public void registerJobs() {
|
||||
|
||||
logger.info("----- AbstractorWorker registerJobs function ----");
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Start the thread
|
||||
*/
|
||||
public void start() {
|
||||
thread = new Thread(this);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
/*
|
||||
* Stop the thread
|
||||
*/
|
||||
public void stop() {
|
||||
// Interrupt the thread so it unblocks any blocking call
|
||||
|
||||
if (worker.isRunning()) {
|
||||
logger.info("Stopping " + name + " (" + new Date().toString() + ")");
|
||||
worker.stop();
|
||||
logger.info("Stopped " + name + " (" + new Date().toString() + ")");
|
||||
}
|
||||
|
||||
thread.interrupt();
|
||||
|
||||
// Wait until the thread exits
|
||||
try {
|
||||
|
||||
thread.join();
|
||||
} catch (InterruptedException ex) {
|
||||
// Unexpected interruption
|
||||
|
@ -77,14 +94,24 @@ public class AbstractWorkerThread implements Runnable {
|
|||
System.exit(1);
|
||||
}
|
||||
|
||||
logger.info("Stopped " + name + " (" + new Date().toString() + ")");
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Execute the thread (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Runnable#run()
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
logger.info("Starting Worker "+ name +" ("+new Date().toString()+")");
|
||||
if (!worker.isRunning()) {
|
||||
logger.info("Starting Worker " + name + " ("
|
||||
+ new Date().toString() + ")");
|
||||
worker.setWorkerID(UUID.randomUUID().toString());
|
||||
worker.addServer(conn);
|
||||
// blocking call.. https://answers.launchpad.net/gearman-java/+question/219175
|
||||
worker.work();
|
||||
}
|
||||
|
||||
while (!Thread.interrupted()) {
|
||||
|
||||
|
@ -92,7 +119,7 @@ public class AbstractWorkerThread implements Runnable {
|
|||
logger.info("Running Worker "+ name +" ("+new Date().toString()+")");
|
||||
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
*
|
||||
* 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;
|
||||
|
||||
/*
|
||||
* Constants for the Gearman Plugin
|
||||
*/
|
||||
public interface Constants {
|
||||
/* Defines. */
|
||||
|
||||
public static final String GEARMAN_DEFAULT_TCP_HOST = "127.0.0.1";
|
||||
public static final int GEARMAN_DEFAULT_TCP_PORT = 4730;
|
||||
|
||||
public static final String PLUGIN_LOGGER_NAME = "hudson.plugins.gearman.logger";
|
||||
public static final String PLUGIN_EXECTUOR_LOGGER_NAME = "hudson.plugins.gearman.executor.logger";
|
||||
}
|
|
@ -18,26 +18,61 @@
|
|||
|
||||
package hudson.plugins.gearman;
|
||||
|
||||
import java.util.Date;
|
||||
import hudson.model.Node;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import jenkins.model.Jenkins;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/*
|
||||
* This is thread to run gearman executors
|
||||
* Executors are used to initiate jenkins builds
|
||||
*/
|
||||
public class ExecutorWorkerThread extends AbstractWorkerThread{
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(AbstractWorkerThread.class);
|
||||
|
||||
public ExecutorWorkerThread(String host, int port, String name) {
|
||||
private Node node;
|
||||
|
||||
public ExecutorWorkerThread(String host, int port, String nodeName){
|
||||
super(host, port, nodeName);
|
||||
this.node = findNode(nodeName);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* This function finds the node with the corresponding node name Returns the
|
||||
* node if found, otherwise returns null
|
||||
*/
|
||||
private Node findNode(String nodeName){
|
||||
|
||||
Jenkins jenkins = Jenkins.getInstance();
|
||||
List<Node> nodes = jenkins.getNodes();
|
||||
Node myNode = null;
|
||||
|
||||
for (Node node : nodes) {
|
||||
if (node.getNodeName().equals(nodeName)){
|
||||
myNode = node;
|
||||
}
|
||||
}
|
||||
|
||||
return myNode;
|
||||
}
|
||||
|
||||
public ExecutorWorkerThread(String host, int port, String name, Node node){
|
||||
super(host, port, name);
|
||||
this.node = node;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerJobs() {
|
||||
|
||||
logger.info("----- ExecutorWorkerThread registerJobs function ----");
|
||||
logger.info("----- Register executor jobs here ----");
|
||||
logger.info("----- Registering executor jobs on " + name + " ----");
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -18,11 +18,16 @@
|
|||
|
||||
package hudson.plugins.gearman;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
import hudson.Launcher;
|
||||
import hudson.Extension;
|
||||
import hudson.model.Build;
|
||||
import hudson.model.BuildListener;
|
||||
import hudson.model.AbstractBuild;
|
||||
import hudson.model.Computer;
|
||||
import hudson.model.Node;
|
||||
import hudson.tasks.Builder;
|
||||
import hudson.tasks.BuildStepDescriptor;
|
||||
|
||||
|
@ -34,6 +39,8 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
import javax.management.Descriptor;
|
||||
|
||||
import jenkins.model.Jenkins;
|
||||
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
/**
|
||||
|
@ -49,7 +56,7 @@ import net.sf.json.JSONObject;
|
|||
public class GearmanPlugin extends Builder {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(GearmanPlugin.class);
|
||||
.getLogger(Constants.PLUGIN_LOGGER_NAME);
|
||||
private final String name;
|
||||
|
||||
@DataBoundConstructor
|
||||
|
@ -79,13 +86,25 @@ public class GearmanPlugin extends Builder {
|
|||
BuildStepDescriptor<Builder> {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(DescriptorImpl.class);
|
||||
.getLogger(Constants.PLUGIN_LOGGER_NAME);
|
||||
private boolean launchWorker; // launchWorker state (from UI checkbox)
|
||||
private String host; // gearman server host
|
||||
private int port; // gearman server port
|
||||
private Jenkins jenkins;
|
||||
public static Stack<AbstractWorkerThread> gewtHandles; // handles to
|
||||
// executor
|
||||
// workers
|
||||
public static Stack<AbstractWorkerThread> gmwtHandles; // handles to
|
||||
// management
|
||||
// workers
|
||||
|
||||
public DescriptorImpl() {
|
||||
logger.info("--- DescriptorImpl Constructor ---");
|
||||
|
||||
jenkins = Jenkins.getInstance();
|
||||
gewtHandles = new Stack<AbstractWorkerThread>();
|
||||
gmwtHandles = new Stack<AbstractWorkerThread>();
|
||||
|
||||
logger.info("--- DescriptorImpl Constructor ---" + host);
|
||||
logger.info("--- DescriptorImpl Constructor ---" + port);
|
||||
|
||||
|
@ -125,7 +144,7 @@ public class GearmanPlugin extends Builder {
|
|||
"Error getting the gearman host name");
|
||||
}
|
||||
|
||||
// user input vaerification
|
||||
// user input verification
|
||||
if (StringUtils.isEmpty(host) || StringUtils.isBlank(host))
|
||||
throw new RuntimeException("Invalid gearman host name");
|
||||
|
||||
|
@ -146,6 +165,52 @@ public class GearmanPlugin extends Builder {
|
|||
logger.info("--- DescriptorImpl Configure function ---"
|
||||
+ this.getPort());
|
||||
|
||||
/*
|
||||
* Purpose here is to create a 1:1 mapping of 'gearman
|
||||
* worker':'jenkins node' then use the gearman worker to execute
|
||||
* builds on that jenkins node
|
||||
*/
|
||||
List<Node> nodes = jenkins.getNodes();
|
||||
|
||||
if (launchWorker && !nodes.isEmpty()) {
|
||||
|
||||
AbstractWorkerThread gwt = null;
|
||||
|
||||
for (Node node : nodes) {
|
||||
Computer c = node.toComputer();
|
||||
if (c.isOnline()) {
|
||||
// create a gearman executor for every node
|
||||
gwt = new ExecutorWorkerThread(host, port,
|
||||
node.getNodeName());
|
||||
gwt.registerJobs();
|
||||
gwt.start();
|
||||
gewtHandles.push(gwt);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Create one additional worker as a management node. This
|
||||
* worker will be used to abort builds.
|
||||
*/
|
||||
if (!gewtHandles.isEmpty()) {
|
||||
gwt = new ManagementWorkerThread(host, port, host);
|
||||
gwt.registerJobs();
|
||||
gwt.start();
|
||||
gmwtHandles.push(gwt);
|
||||
|
||||
}
|
||||
|
||||
} else if (!launchWorker) { // stop worker threads
|
||||
while (!gewtHandles.isEmpty()) { // stop executors
|
||||
AbstractWorkerThread task = gewtHandles.pop();
|
||||
task.stop();
|
||||
}
|
||||
while (!gmwtHandles.isEmpty()) { // stop management
|
||||
AbstractWorkerThread task = gmwtHandles.pop();
|
||||
task.stop();
|
||||
}
|
||||
}
|
||||
|
||||
save();
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -18,11 +18,15 @@
|
|||
|
||||
package hudson.plugins.gearman;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.gearman.worker.DefaultGearmanFunctionFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/*
|
||||
* This is a thread to manage gearman executors
|
||||
* This is used to abort/delete jenkins jobs.
|
||||
*/
|
||||
|
||||
public class ManagementWorkerThread extends AbstractWorkerThread {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
|
@ -37,8 +41,7 @@ public class ManagementWorkerThread extends AbstractWorkerThread {
|
|||
@Override
|
||||
public void registerJobs() {
|
||||
|
||||
logger.info("----- ManagementWorkerThread registerJobs function ----");
|
||||
logger.info("----- Register management jobs here ----");
|
||||
logger.info("----- Registering management jobs on " + name + " ----");
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue