From a5164d65a4c42e784d253c553396b61a24ae0d9c Mon Sep 17 00:00:00 2001 From: Khai Do Date: Wed, 2 Dec 2015 22:11:03 -0800 Subject: [PATCH] Update to Jenkins LTS 1.625.3 and fix function registration Using a newer Jenkins LTS (ver 1.625.3) did not work with the gearman-plugin. Gearman function registeration wasn't working correct because Computer.currentComputer()[1] and getInstance()[2] methods seemed to always return null on initialization of the plugin. The Jenkins.getActiveInstance method (introduced in Jenkins-1.589)[3] works more reliably. [1] http://javadoc.jenkins-ci.org/hudson/model/Node.html#getNodeName() [2] http://javadoc.jenkins-ci.org/jenkins/model/Jenkins.html#getActiveInstance() [3] https://github.com/jenkinsci/jenkins/commit/e6c2e16f7a1e2487a1d201aa11ae0c44e75a6ad7 Change-Id: I1534beda9c39a9d15da4d7b96f17e7b75699a631 --- pom.xml | 2 +- .../java/hudson/plugins/gearman/ExecutorWorkerThread.java | 2 +- src/main/java/hudson/plugins/gearman/GearmanPluginUtil.java | 4 ++-- src/main/java/hudson/plugins/gearman/GearmanProxy.java | 6 +++--- .../hudson/plugins/gearman/NodeAvailabilityMonitor.java | 2 +- .../hudson/plugins/gearman/GearmanPluginConfigTest.java | 2 +- .../java/hudson/plugins/gearman/GearmanPluginUtilTest.java | 3 ++- 7 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 2f34cbc..1c02d1c 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ org.jenkins-ci.plugins plugin - 1.565.3 + 1.625.3 gearman-plugin diff --git a/src/main/java/hudson/plugins/gearman/ExecutorWorkerThread.java b/src/main/java/hudson/plugins/gearman/ExecutorWorkerThread.java index 7409b00..18bf100 100644 --- a/src/main/java/hudson/plugins/gearman/ExecutorWorkerThread.java +++ b/src/main/java/hudson/plugins/gearman/ExecutorWorkerThread.java @@ -117,7 +117,7 @@ public class ExecutorWorkerThread extends AbstractWorkerThread{ if (!computer.isOffline()) { Node node = computer.getNode(); - List allProjects = Jenkins.getInstance().getAllItems(AbstractProject.class); + List allProjects = Jenkins.getActiveInstance().getAllItems(AbstractProject.class); for (AbstractProject project : allProjects) { if (project.isDisabled()) { // ignore all disabled projects diff --git a/src/main/java/hudson/plugins/gearman/GearmanPluginUtil.java b/src/main/java/hudson/plugins/gearman/GearmanPluginUtil.java index 4f35d84..e16e315 100644 --- a/src/main/java/hudson/plugins/gearman/GearmanPluginUtil.java +++ b/src/main/java/hudson/plugins/gearman/GearmanPluginUtil.java @@ -51,7 +51,7 @@ public class GearmanPluginUtil { */ public static String getRealName(Computer computer) { - if (Computer.currentComputer() == computer) { + if (Jenkins.getActiveInstance().getComputer("") == computer) { return "master"; } else { return computer.getName(); @@ -70,7 +70,7 @@ public class GearmanPluginUtil { */ public static Run findBuild(String jobName, int buildNumber) { - AbstractProject project = Jenkins.getInstance().getItemByFullName(jobName, AbstractProject.class); + AbstractProject project = Jenkins.getActiveInstance().getItemByFullName(jobName, AbstractProject.class); if (project != null){ Run run = project.getBuildByNumber(buildNumber); if (run != null) { diff --git a/src/main/java/hudson/plugins/gearman/GearmanProxy.java b/src/main/java/hudson/plugins/gearman/GearmanProxy.java index 38f3360..26935e8 100644 --- a/src/main/java/hudson/plugins/gearman/GearmanProxy.java +++ b/src/main/java/hudson/plugins/gearman/GearmanProxy.java @@ -70,7 +70,7 @@ public class GearmanProxy { String hostname = Constants.GEARMAN_DEFAULT_EXECUTOR_NAME; // query Jenkins for master's name try { - master = Jenkins.getInstance().getComputer(""); + master = Jenkins.getActiveInstance().getComputer(""); hostname = master.getHostName(); } catch (Exception e) { logger.warn("Exception while getting hostname", e); @@ -119,7 +119,7 @@ public class GearmanProxy { // first make sure master is enabled (or has executors) Node masterNode = null; try { - masterNode = Computer.currentComputer().getNode(); + masterNode = Jenkins.getActiveInstance().getComputer("").getNode(); } catch (NullPointerException npe) { logger.info("---- Master is offline"); } catch (Exception e) { @@ -136,7 +136,7 @@ public class GearmanProxy { /* * Spawn executors for the jenkins slaves */ - List nodes = Jenkins.getInstance().getNodes(); + List nodes = Jenkins.getActiveInstance().getNodes(); if (!nodes.isEmpty()) { for (Node node : nodes) { Computer computer = node.toComputer(); diff --git a/src/main/java/hudson/plugins/gearman/NodeAvailabilityMonitor.java b/src/main/java/hudson/plugins/gearman/NodeAvailabilityMonitor.java index 24f3f66..96b6aae 100644 --- a/src/main/java/hudson/plugins/gearman/NodeAvailabilityMonitor.java +++ b/src/main/java/hudson/plugins/gearman/NodeAvailabilityMonitor.java @@ -38,7 +38,7 @@ public class NodeAvailabilityMonitor implements AvailabilityMonitor { { this.computer = computer; queue = Queue.getInstance(); - jenkins = Jenkins.getInstance(); + jenkins = Jenkins.getActiveInstance(); } public Computer getComputer() { diff --git a/src/test/java/hudson/plugins/gearman/GearmanPluginConfigTest.java b/src/test/java/hudson/plugins/gearman/GearmanPluginConfigTest.java index 7382b10..f403387 100644 --- a/src/test/java/hudson/plugins/gearman/GearmanPluginConfigTest.java +++ b/src/test/java/hudson/plugins/gearman/GearmanPluginConfigTest.java @@ -48,7 +48,7 @@ public class GearmanPluginConfigTest { public void setUp() { Jenkins jenkins = mock(Jenkins.class); PowerMockito.mockStatic(Jenkins.class); - when(Jenkins.getInstance()).thenReturn(jenkins); + when(Jenkins.getActiveInstance()).thenReturn(jenkins); gpc = new GearmanPluginConfig(); } diff --git a/src/test/java/hudson/plugins/gearman/GearmanPluginUtilTest.java b/src/test/java/hudson/plugins/gearman/GearmanPluginUtilTest.java index 0f5a269..486efd6 100644 --- a/src/test/java/hudson/plugins/gearman/GearmanPluginUtilTest.java +++ b/src/test/java/hudson/plugins/gearman/GearmanPluginUtilTest.java @@ -20,6 +20,7 @@ package hudson.plugins.gearman; import hudson.model.Computer; import hudson.slaves.DumbSlave; +import jenkins.model.Jenkins; import org.junit.Test; import org.jvnet.hudson.test.HudsonTestCase; @@ -45,7 +46,7 @@ public class GearmanPluginUtilTest extends HudsonTestCase { @Test public void testGetRealNameMaster() throws Exception { - assertEquals("master", GearmanPluginUtil.getRealName(Computer.currentComputer())); + assertEquals("master", GearmanPluginUtil.getRealName(Jenkins.getActiveInstance().getComputer(""))); } }