diff --git a/src/main/java/hudson/plugins/gearman/ComputerListenerImpl.java b/src/main/java/hudson/plugins/gearman/ComputerListenerImpl.java index 06bb961..95bf8cc 100644 --- a/src/main/java/hudson/plugins/gearman/ComputerListenerImpl.java +++ b/src/main/java/hudson/plugins/gearman/ComputerListenerImpl.java @@ -1,3 +1,20 @@ +/* + * + * 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.Extension; @@ -61,7 +78,7 @@ public class ComputerListenerImpl extends ComputerListener { @Override public void onConfigurationChange() { - // gets called on any configuration change + // gets called on any configuration change // includes new slave and delete slave logger.info("---- " + ComputerListenerImpl.class.getName() + ":" + " onConfigurationChange"); @@ -127,8 +144,9 @@ public class ComputerListenerImpl extends ComputerListener { @Override public void onOnline(Computer c, TaskListener listener) throws IOException, InterruptedException { + // gets called when master goes into online state // gets called when existing slave re-connects - // gets called when new slave goes into online state. + // gets called when new slave goes into online state logger.info("---- " + ComputerListenerImpl.class.getName() + ":" + " onOnline"); @@ -137,6 +155,43 @@ public class ComputerListenerImpl extends ComputerListener { return; } + // on creation of master + if (Computer.currentComputer() == c) { //check to see if this is master + logger.info("---- This is master node, name is = "+c.getName()); + + /* + * Spawn management executor worker. This worker does not need any + * executors. It only needs to work with gearman. + */ + String host = GearmanPluginConfig.get().getHost(); + int port = GearmanPluginConfig.get().getPort(); + + AbstractWorkerThread gwt = new ManagementWorkerThread(host, port, host); + gwt.registerJobs(); + gwt.start(); + GearmanProxy.getGmwtHandles().add(gwt); + + /* + * Spawn executors for the jenkins master Need to treat the master + * differently than slaves because the master is not the same as a + * slave + */ + Node masterNode = c.getNode(); + int executors = c.getExecutors().size(); + for (int i = 0; i < executors; i++) { + // create a gearman worker for every executor on the master + gwt = new ExecutorWorkerThread(GearmanPluginConfig.get().getHost(), + GearmanPluginConfig.get().getPort(), + "master-exec"+ Integer.toString(i), + masterNode); + gwt.start(); + GearmanProxy.getGewtHandles().add(gwt); + } + GearmanProxy.setNumWorkerNodes(GearmanPluginUtil.getNumTotalNodes()); + logger.info("---- numWorkerNodes = " + + GearmanProxy.getNumWorkerNodes()); + } + // on re-connection of node // update gearman worker functions on existing threads List workers = GearmanProxy.getGewtHandles(); @@ -149,7 +204,7 @@ public class ComputerListenerImpl extends ComputerListener { @Override public void onTemporarilyOnline(Computer c) { - // gets called when existing slave is re-enabled + // gets called when existing slave is re-enabled (including master) logger.info("---- " + ComputerListenerImpl.class.getName() + ":" + " onTemporarilyOnline"); @@ -169,7 +224,7 @@ public class ComputerListenerImpl extends ComputerListener { @Override public void onTemporarilyOffline(Computer c, OfflineCause cause) { - // gets called when existing slave is dis-enabled + // gets called when existing slave is dis-enabled (including master) logger.info("---- " + ComputerListenerImpl.class.getName() + ":" + " onTemporarilyOffline"); @@ -187,4 +242,5 @@ public class ComputerListenerImpl extends ComputerListener { } } + } diff --git a/src/main/java/hudson/plugins/gearman/GearmanPluginConfig.java b/src/main/java/hudson/plugins/gearman/GearmanPluginConfig.java index 89b82ab..05b1b0d 100644 --- a/src/main/java/hudson/plugins/gearman/GearmanPluginConfig.java +++ b/src/main/java/hudson/plugins/gearman/GearmanPluginConfig.java @@ -57,14 +57,6 @@ public class GearmanPluginConfig extends GlobalConfiguration { gearmanProxy = new GearmanProxy(); load(); - - /* - * Not sure when to register gearman functions yet so for now always - * initialize the launch worker flag to disabled state at jenkins - * startup so we are always at a known state - */ - launchWorker = Constants.GEARMAN_DEFAULT_LAUNCH_WORKER; - save(); } public static GearmanPluginConfig get() { diff --git a/src/main/java/hudson/plugins/gearman/GearmanProxy.java b/src/main/java/hudson/plugins/gearman/GearmanProxy.java index 4d0cac4..a4bdf91 100644 --- a/src/main/java/hudson/plugins/gearman/GearmanProxy.java +++ b/src/main/java/hudson/plugins/gearman/GearmanProxy.java @@ -21,8 +21,8 @@ package hudson.plugins.gearman; import hudson.model.Computer; import hudson.model.Node; +import java.util.ArrayList; import java.util.List; -import java.util.Stack; import jenkins.model.Jenkins; @@ -44,7 +44,7 @@ public class GearmanProxy { private static List gewtHandles; private static List gmwtHandles; - // keep track of number of executor slaves in system + // keep track of number of computers that are tied to gearman workers private static int numWorkerNodes; @@ -52,8 +52,8 @@ public class GearmanProxy { public GearmanProxy() { logger.info("--- GearmanProxy Constructor ---"); - gewtHandles = new Stack(); - gmwtHandles = new Stack(); + gewtHandles = new ArrayList(); + gmwtHandles = new ArrayList(); numWorkerNodes = 0; } @@ -102,16 +102,18 @@ public class GearmanProxy { if (masterNode != null) { Computer computer = masterNode.toComputer(); - int executors = computer.getExecutors().size(); - for (int i = 0; i < executors; i++) { - // create a gearman worker for every executor on the master - gwt = new ExecutorWorkerThread(host, port, "master-exec" - + Integer.toString(i), masterNode); - gwt.registerJobs(); - gwt.start(); - gewtHandles.add(gwt); + if (computer != null) { + int executors = computer.getExecutors().size(); + for (int i = 0; i < executors; i++) { + // create a gearman worker for every executor on the master + gwt = new ExecutorWorkerThread(host, port, "master-exec" + + Integer.toString(i), masterNode); + gwt.registerJobs(); + gwt.start(); + gewtHandles.add(gwt); + } + numWorkerNodes++; } - numWorkerNodes++; } /* @@ -121,15 +123,17 @@ public class GearmanProxy { if (!nodes.isEmpty()) { for (Node node : nodes) { Computer computer = node.toComputer(); - // create a gearman worker for every executor on the slave - int slaveExecutors = computer.getExecutors().size(); - for (int i = 0; i < slaveExecutors; i++) { - gwt = new ExecutorWorkerThread(host, port, - node.getNodeName() + "-exec" - + Integer.toString(i), node); - gwt.registerJobs(); - gwt.start(); - gewtHandles.add(gwt); + if (computer != null) { + // create a gearman worker for every executor on the slave + int slaveExecutors = computer.getExecutors().size(); + for (int i = 0; i < slaveExecutors; i++) { + gwt = new ExecutorWorkerThread(host, port, + node.getNodeName() + "-exec" + + Integer.toString(i), node); + gwt.registerJobs(); + gwt.start(); + gewtHandles.add(gwt); + } } numWorkerNodes++; } @@ -173,6 +177,13 @@ public class GearmanProxy { return gewtHandles; } + /* + * This method returns the list of gearman management workers + */ + public static synchronized List getGmwtHandles() { + return gmwtHandles; + } + /* * This method returns the number of worker nodes */