diff --git a/src/main/java/hudson/plugins/gearman/Constants.java b/src/main/java/hudson/plugins/gearman/Constants.java
index c51aaaa..60a0296 100644
--- a/src/main/java/hudson/plugins/gearman/Constants.java
+++ b/src/main/java/hudson/plugins/gearman/Constants.java
@@ -26,6 +26,7 @@ public interface Constants {
public static final String GEARMAN_DEFAULT_EXECUTOR_NAME = "anonymous";
public static final boolean GEARMAN_DEFAULT_ENABLE_PLUGIN = false;
+ public static final boolean GEARMAN_DEFAULT_SMOOTH_DISABLE = false;
public static final String GEARMAN_DEFAULT_TCP_HOST = "127.0.0.1";
public static final int GEARMAN_DEFAULT_TCP_PORT = 4730;
diff --git a/src/main/java/hudson/plugins/gearman/GearmanPluginConfig.java b/src/main/java/hudson/plugins/gearman/GearmanPluginConfig.java
index e2a6cbd..59c4b62 100644
--- a/src/main/java/hudson/plugins/gearman/GearmanPluginConfig.java
+++ b/src/main/java/hudson/plugins/gearman/GearmanPluginConfig.java
@@ -24,6 +24,7 @@ import hudson.util.FormValidation;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
+import java.util.List;
import javax.servlet.ServletException;
@@ -51,6 +52,7 @@ public class GearmanPluginConfig extends GlobalConfiguration {
private boolean enablePlugin; // config to enable and disable plugin
private String host; // gearman server host
private int port; // gearman server port
+ private boolean smoothDisable; // If set, a disable of the plugin should be done when all workers are idled
/**
* Constructor.
@@ -91,17 +93,28 @@ public class GearmanPluginConfig extends GlobalConfiguration {
// save current plugin config so we can compare to new user settings
String prevHost = this.host;
int prevPort = this.port;
+ boolean prevSmoothDisable = this.smoothDisable;
boolean prevEnablePlugin = this.enablePlugin;
+ // get the new gearman smooth disable mode
+ smoothDisable = json.getBoolean("smoothDisable");
+
// get the new gearman plugin configs from jenkins config page settings
enablePlugin = json.getBoolean("enablePlugin");
host = json.getString("host");
port = json.getInt("port");
if (!enablePlugin && prevEnablePlugin) { // gearman-plugin goes from ON to OFF state
- GearmanProxy.getInstance().stopAll();
+ if (!smoothDisable) {
+ GearmanProxy.getInstance().stopAll();
+ }
} else if (enablePlugin && !prevEnablePlugin) { // gearman-plugin goes from OFF to ON state
+ // Cleanup if smooth disable was set
+ if (prevSmoothDisable) {
+ GearmanProxy.getInstance().stopAll();
+ }
+
// check for a valid connection to server
if (!connectionIsAvailable(host, port, 5000)) {
enablePlugin = false;
@@ -117,7 +130,8 @@ public class GearmanPluginConfig extends GlobalConfiguration {
// update connection for a plugin config change
if (!host.equals(prevHost) || port != prevPort) {
- // stop the workers on the current connected
+ // stop the workers on the current connected whatever is the smoothDisable setting
+ // The use case in not yet identified
GearmanProxy.getInstance().stopAll();
// check for a valid connection to server
@@ -132,6 +146,8 @@ public class GearmanPluginConfig extends GlobalConfiguration {
GearmanProxy.getInstance().initWorkers();
}
+ } else if (!smoothDisable && prevSmoothDisable && !enablePlugin && !prevEnablePlugin){ // Smooth disable was unset while plugin is disabled
+ GearmanProxy.getInstance().stopAll();
}
req.bindJSON(this, json);
@@ -148,6 +164,14 @@ public class GearmanPluginConfig extends GlobalConfiguration {
return Objects.firstNonNull(enablePlugin, Constants.GEARMAN_DEFAULT_ENABLE_PLUGIN);
}
+ /**
+ * This method returns true if the global configuration says we should
+ * enable the smooth disable feature.
+ */
+ public boolean smoothDisable() {
+ return Objects.firstNonNull(smoothDisable, Constants.GEARMAN_DEFAULT_SMOOTH_DISABLE);
+ }
+
/**
* This method returns the value from the server host text box
*/
diff --git a/src/main/java/hudson/plugins/gearman/MyGearmanWorkerImpl.java b/src/main/java/hudson/plugins/gearman/MyGearmanWorkerImpl.java
index 6200369..ea72b95 100644
--- a/src/main/java/hudson/plugins/gearman/MyGearmanWorkerImpl.java
+++ b/src/main/java/hudson/plugins/gearman/MyGearmanWorkerImpl.java
@@ -451,9 +451,13 @@ public class MyGearmanWorkerImpl implements GearmanSessionEventHandler {
LOG.info("---- Worker " + this + " received unique job assignment");
return addNewJob(event);
case NOOP:
- LOG.debug("---- Worker " + this + " sending grab job after wakeup");
try {
- sendGrabJob(s);
+ if (GearmanPluginConfig.get().smoothDisable() && !GearmanPluginConfig.get().enablePlugin()) {
+ LOG.info("---- Worker " + this + " do not send grab while in smooth disabling");
+ } else {
+ LOG.debug("---- Worker " + this + " sending grab job after wakeup");
+ sendGrabJob(s);
+ }
} catch (InterruptedException e) {
LOG.warn("---- Worker " + this + " interrupted while waiting for okay to send " +
"grab job", e);
diff --git a/src/main/resources/hudson/plugins/gearman/GearmanPluginConfig/config.jelly b/src/main/resources/hudson/plugins/gearman/GearmanPluginConfig/config.jelly
index 1b45669..00c97c0 100644
--- a/src/main/resources/hudson/plugins/gearman/GearmanPluginConfig/config.jelly
+++ b/src/main/resources/hudson/plugins/gearman/GearmanPluginConfig/config.jelly
@@ -13,5 +13,9 @@
description="Select to enable Gearman plugin, Unselect to disable">
+ If set, let a chance to ongoing job to finish when gearman is disabled. No more job are accepted while gearman is disabled. +
+