fix for bug 1162887

Users no longer need to disable the plugin to connect to
a different gearman server.

Along with this commit I changed the 'launchWorker' plugin config name to
'enablePlugin' because enabling plugin is distinct from running gearman workers.

Change-Id: Ia81d78da3dbdc83fd46dd7f5d40ccb9aca3af97f
This commit is contained in:
zaro0508 2013-04-29 14:00:25 -07:00
parent 07026b7b71
commit 0de925f6d8
8 changed files with 85 additions and 65 deletions

View File

@ -47,7 +47,7 @@ public class ComputerListenerImpl extends ComputerListener {
+ " onConfigurationChange"); + " onConfigurationChange");
// update functions only when gearman-plugin is enabled // update functions only when gearman-plugin is enabled
if (!GearmanPluginConfig.get().launchWorker()) { if (!GearmanPluginConfig.get().enablePlugin()) {
return; return;
} }
@ -63,7 +63,7 @@ public class ComputerListenerImpl extends ComputerListener {
+ " onOffline"); + " onOffline");
// update functions only when gearman-plugin is enabled // update functions only when gearman-plugin is enabled
if (!GearmanPluginConfig.get().launchWorker()) { if (!GearmanPluginConfig.get().enablePlugin()) {
return; return;
} }
@ -92,7 +92,7 @@ public class ComputerListenerImpl extends ComputerListener {
+ " onOnline"); + " onOnline");
// update functions only when gearman-plugin is enabled // update functions only when gearman-plugin is enabled
if (!GearmanPluginConfig.get().launchWorker()) { if (!GearmanPluginConfig.get().enablePlugin()) {
return; return;
} }

View File

@ -24,7 +24,7 @@ package hudson.plugins.gearman;
public interface Constants { public interface Constants {
/* Defines. */ /* Defines. */
public static final boolean GEARMAN_DEFAULT_LAUNCH_WORKER = false; public static final boolean GEARMAN_DEFAULT_ENABLE_PLUGIN = false;
public static final String GEARMAN_DEFAULT_TCP_HOST = "127.0.0.1"; public static final String GEARMAN_DEFAULT_TCP_HOST = "127.0.0.1";
public static final int GEARMAN_DEFAULT_TCP_PORT = 4730; public static final int GEARMAN_DEFAULT_TCP_PORT = 4730;

View File

@ -48,7 +48,7 @@ public class GearmanPluginConfig extends GlobalConfiguration {
private static final Logger logger = LoggerFactory private static final Logger logger = LoggerFactory
.getLogger(Constants.PLUGIN_LOGGER_NAME); .getLogger(Constants.PLUGIN_LOGGER_NAME);
private boolean launchWorker; // enable/disable plugin private boolean enablePlugin; // config to enable and disable plugin
private String host; // gearman server host private String host; // gearman server host
private int port; // gearman server port private int port; // gearman server port
@ -56,8 +56,6 @@ public class GearmanPluginConfig extends GlobalConfiguration {
* Constructor. * Constructor.
*/ */
public GearmanPluginConfig() { public GearmanPluginConfig() {
logger.info("---- GearmanPluginConfig Constructor ---");
load(); load();
} }
@ -65,6 +63,7 @@ public class GearmanPluginConfig extends GlobalConfiguration {
return GlobalConfiguration.all().get(GearmanPluginConfig.class); return GlobalConfiguration.all().get(GearmanPluginConfig.class);
} }
/* /*
* This method runs when user clicks Test Connection button. * This method runs when user clicks Test Connection button.
* *
@ -89,28 +88,50 @@ public class GearmanPluginConfig extends GlobalConfiguration {
public boolean configure(StaplerRequest req, JSONObject json) public boolean configure(StaplerRequest req, JSONObject json)
throws Descriptor.FormException { throws Descriptor.FormException {
// set the gearman config from user entered values in jenkins config // save current plugin config so we can compare to new user settings
// page String prevHost = this.host;
launchWorker = json.getBoolean("launchWorker"); int prevPort = this.port;
boolean prevEnablePlugin = this.enablePlugin;
// get the new gearman plugin configs from jenkins config page settings
enablePlugin = json.getBoolean("enablePlugin");
host = json.getString("host"); host = json.getString("host");
port = json.getInt("port"); port = json.getInt("port");
if (launchWorker) { if (!enablePlugin && prevEnablePlugin) { // gearman-plugin goes from ON to OFF state
GearmanProxy.getInstance().stopAll();
// check for a valid connection to gearman server } else if (enablePlugin && !prevEnablePlugin) { // gearman-plugin goes from OFF to ON state
logger.info("---- Check connection to Gearman Server " + host + ":" // check for a valid connection to server
+ port);
if (!connectionIsAvailable(host, port, 5000)) { if (!connectionIsAvailable(host, port, 5000)) {
launchWorker = false; enablePlugin = false;
throw new FormException("Unable to connect to Gearman server. " throw new FormException("Unable to connect to Gearman server. "
+ "Please check the server connection settings and retry.", + "Please check the server connection settings and retry.",
"host"); "host");
} }
// run workers
GearmanProxy.getInstance().initWorkers(); GearmanProxy.getInstance().initWorkers();
} else { } else if (enablePlugin && prevEnablePlugin) { // gearman-plugin stays in the ON state
GearmanProxy.getInstance().stopAll(); // update connection for a plugin config change
if (!host.equals(prevHost) || port != prevPort) {
// stop the workers on the current connected
GearmanProxy.getInstance().stopAll();
// check for a valid connection to server
if (!connectionIsAvailable(host, port, 5000)) {
enablePlugin = false;
throw new FormException("Unable to connect to Gearman server. "
+ "Please check the server connection settings and retry.",
"host");
}
// run workers with new connection
GearmanProxy.getInstance().initWorkers();
}
} }
req.bindJSON(this, json); req.bindJSON(this, json);
@ -118,12 +139,13 @@ public class GearmanPluginConfig extends GlobalConfiguration {
return true; return true;
} }
/** /**
* This method returns true if the global configuration says we should * This method returns true if the global configuration says we should
* launch worker. * enable the plugin.
*/ */
public boolean launchWorker() { public boolean enablePlugin() {
return Objects.firstNonNull(launchWorker, Constants.GEARMAN_DEFAULT_LAUNCH_WORKER); return Objects.firstNonNull(enablePlugin, Constants.GEARMAN_DEFAULT_ENABLE_PLUGIN);
} }
/** /**
@ -146,7 +168,7 @@ public class GearmanPluginConfig extends GlobalConfiguration {
} }
/* /*
* This method checks whether a connection open and available * This method checks whether a connection is open and available
* on $host:$port * on $host:$port
* *
* @param host * @param host

View File

@ -82,50 +82,48 @@ public class GearmanProxy {
* executor' then use the gearman worker to execute builds on that * executor' then use the gearman worker to execute builds on that
* jenkins nodes * jenkins nodes
*/ */
if (getNumExecutors() == 0) {
/* /*
* Spawn management executor worker. This worker does not need any * Spawn management executor worker. This worker does not need any
* executors. It only needs to work with gearman. * executors. It only needs to work with gearman.
*/ */
createManagementWorker(); createManagementWorker();
/* /*
* Spawn executors for the jenkins master Need to treat the master * Spawn executors for the jenkins master Need to treat the master
* differently than slaves because the master is not the same as a * differently than slaves because the master is not the same as a
* slave * slave
*/ */
// first make sure master is enabled (or has executors) // first make sure master is enabled (or has executors)
Node masterNode = null; Node masterNode = null;
try { try {
masterNode = Computer.currentComputer().getNode(); masterNode = Computer.currentComputer().getNode();
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
logger.info("---- Master is offline"); logger.info("---- Master is offline");
} catch (Exception e) { } catch (Exception e) {
logger.info("---- Can't get Master"); logger.info("---- Can't get Master");
e.printStackTrace(); e.printStackTrace();
}
if (masterNode != null) {
Computer computer = masterNode.toComputer();
if (computer != null) {
createExecutorWorkersOnNode(computer);
} }
}
if (masterNode != null) { /*
Computer computer = masterNode.toComputer(); * Spawn executors for the jenkins slaves
*/
List<Node> nodes = Jenkins.getInstance().getNodes();
if (!nodes.isEmpty()) {
for (Node node : nodes) {
Computer computer = node.toComputer();
if (computer != null) { if (computer != null) {
// create a gearman worker for every executor on the slave
createExecutorWorkersOnNode(computer); createExecutorWorkersOnNode(computer);
} }
} }
/*
* Spawn executors for the jenkins slaves
*/
List<Node> nodes = Jenkins.getInstance().getNodes();
if (!nodes.isEmpty()) {
for (Node node : nodes) {
Computer computer = node.toComputer();
if (computer != null) {
// create a gearman worker for every executor on the slave
createExecutorWorkersOnNode(computer);
}
}
}
} }
logger.info("---- Num of executors running = " + getNumExecutors()); logger.info("---- Num of executors running = " + getNumExecutors());

View File

@ -54,7 +54,7 @@ public class SaveableListenerImpl extends SaveableListener {
+ " onChange"); + " onChange");
// update functions only when gearman-plugin is enabled // update functions only when gearman-plugin is enabled
if (!GearmanPluginConfig.get().launchWorker()) { if (!GearmanPluginConfig.get().enablePlugin()) {
return; return;
} }

View File

@ -9,9 +9,9 @@
<f:validateButton <f:validateButton
title="${%Test Connection}" progress="${%Testing...}" title="${%Test Connection}" progress="${%Testing...}"
method="testConnection" with="host,port"/> method="testConnection" with="host,port"/>
<f:entry title="Launch Worker" field="launchWorker" <f:entry title="Enable Gearman" field="enablePlugin"
description="Select to run Gearman workers, Unselect to stop"> description="Select to enable Gearman plugin, Unselect to disable">
<f:checkbox checked="${descriptor.launchWorker()}"/> <f:checkbox checked="${descriptor.enablePlugin()}"/>
</f:entry> </f:entry>
</f:section> </f:section>
</j:jelly> </j:jelly>

View File

@ -69,7 +69,7 @@ public class GearmanPluginConfigTest {
@Test @Test
public void testDefaultLaunchWorker() { public void testDefaultLaunchWorker() {
assertEquals(Constants.GEARMAN_DEFAULT_LAUNCH_WORKER, assertEquals(Constants.GEARMAN_DEFAULT_ENABLE_PLUGIN,
gpc.launchWorker()); gpc.enablePlugin());
} }
} }