Browse Source

Get master hostname from master computer object.

Jenkins.getDisplayName() is useless and returns "Jenkins". Get the name
of the master via the Jenkins master computer object. Fall back on
doing a DNS name lookup otherwise.

Change-Id: Icee4e22e9ca989fd1faa6d27f4dad3be76da6385
tags/0.0.3^0
Clark Boylan 5 years ago
parent
commit
df9ee54284

+ 6
- 4
src/main/java/org/jenkinsci/plugins/ZMQEventPublisher/Phase.java View File

@@ -38,7 +38,7 @@ public enum Phase {
38 38
     STARTED, COMPLETED, FINISHED;
39 39
 
40 40
     @SuppressWarnings({ "unchecked", "rawtypes" })
41
-    public String handlePhase(Run run, String status, TaskListener listener) {
41
+    public String handlePhase(Run run, String status, String masterName, TaskListener listener) {
42 42
         Jenkins jenkins = Jenkins.getInstance();
43 43
         HudsonNotificationProperty property = (HudsonNotificationProperty)
44 44
             run.getParent().getProperty(HudsonNotificationProperty.class);
@@ -47,7 +47,7 @@ public enum Phase {
47 47
                 jenkins.getDescriptor(HudsonNotificationProperty.class);
48 48
         if ((property != null && property.isEnabled()) ||
49 49
                 (globalProperty != null && globalProperty.isGloballyEnabled())) {
50
-            return buildMessage(run.getParent(), run, status);
50
+            return buildMessage(run.getParent(), run, status, masterName);
51 51
         }
52 52
         return null;
53 53
     }
@@ -55,7 +55,7 @@ public enum Phase {
55 55
     private Gson gson = new GsonBuilder().setFieldNamingPolicy(
56 56
         FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
57 57
 
58
-    private String buildMessage(Job job, Run run, String status) {
58
+    private String buildMessage(Job job, Run run, String status, String masterName) {
59 59
         JobState jobState = new JobState();
60 60
         jobState.setName(job.getName());
61 61
         jobState.setUrl(job.getUrl());
@@ -70,13 +70,15 @@ public enum Phase {
70 70
         if (rootUrl != null) {
71 71
             buildState.setFullUrl(rootUrl + run.getUrl());
72 72
         }
73
+        if (masterName != null) {
74
+            buildState.setHostName(masterName);
75
+        }
73 76
 
74 77
         Executor executor = run.getExecutor();
75 78
         if (executor != null) {
76 79
             Computer computer = executor.getOwner();
77 80
             if (computer != null) {
78 81
                 buildState.setNodeName(computer.getName());
79
-                buildState.setHostName(jenkins.getDisplayName());
80 82
             }
81 83
         }
82 84
 

+ 24
- 3
src/main/java/org/jenkinsci/plugins/ZMQEventPublisher/RunListenerImpl.java View File

@@ -18,12 +18,16 @@
18 18
 package org.jenkinsci.plugins.ZMQEventPublisher;
19 19
 
20 20
 import hudson.Extension;
21
+import hudson.model.Computer;
21 22
 import hudson.model.Result;
22 23
 import hudson.model.Run;
23 24
 import hudson.model.TaskListener;
24 25
 import hudson.model.listeners.RunListener;
25 26
 import hudson.util.DaemonThreadFactory;
26 27
 
28
+import jenkins.model.Jenkins;
29
+
30
+import java.net.InetAddress;
27 31
 import java.util.concurrent.LinkedBlockingQueue;
28 32
 import java.util.logging.Level;
29 33
 import java.util.logging.Logger;
@@ -43,18 +47,35 @@ public class RunListenerImpl extends RunListener<Run> {
43 47
         new DaemonThreadFactory();
44 48
     private ZMQRunnable ZMQRunner;
45 49
     private Thread thread;
50
+    private String masterName;
46 51
 
47 52
     public RunListenerImpl() {
48 53
         super(Run.class);
49 54
         ZMQRunner = new ZMQRunnable(queue);
50 55
         thread = threadFactory.newThread(ZMQRunner);
51 56
         thread.start();
57
+
58
+        Computer master = null;
59
+        String hostname = null;
60
+        // query Jenkins for master's name
61
+        try {
62
+            master = Jenkins.getInstance().getComputer("");
63
+            if (master != null) {
64
+                hostname = master.getHostName();
65
+            }
66
+            else {
67
+                // master node may not be enabled so get masterName from system
68
+                hostname = java.net.InetAddress.getLocalHost().getHostName();
69
+            }
70
+        } catch (Exception e) {
71
+        }
72
+        masterName = hostname;
52 73
     }
53 74
 
54 75
     @Override
55 76
     public void onCompleted(Run build, TaskListener listener) {
56 77
         String event = "onCompleted";
57
-        String json = Phase.COMPLETED.handlePhase(build, getStatus(build), listener);
78
+        String json = Phase.COMPLETED.handlePhase(build, getStatus(build), masterName, listener);
58 79
         sendEvent(event, json);
59 80
     }
60 81
 
@@ -70,14 +91,14 @@ public class RunListenerImpl extends RunListener<Run> {
70 91
     @Override
71 92
     public void onFinalized(Run build) {
72 93
         String event = "onFinalized";
73
-        String json = Phase.FINISHED.handlePhase(build, getStatus(build), TaskListener.NULL);
94
+        String json = Phase.FINISHED.handlePhase(build, getStatus(build), masterName, TaskListener.NULL);
74 95
         sendEvent(event, json);
75 96
     }
76 97
 
77 98
     @Override
78 99
     public void onStarted(Run build, TaskListener listener) {
79 100
         String event = "onStarted";
80
-        String json = Phase.STARTED.handlePhase(build, getStatus(build), listener);
101
+        String json = Phase.STARTED.handlePhase(build, getStatus(build), masterName, listener);
81 102
         sendEvent(event, json);
82 103
     }
83 104
 

Loading…
Cancel
Save