Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1293 from olivergondza/master-listener
[FIXED JENKINS-23481][FIXED JENKINS-23551] Send onOffline notification to master computer prior Jenkins restart
  • Loading branch information
olivergondza committed Jun 26, 2014
2 parents 4de0769 + 3879981 commit 8274c98
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
15 changes: 15 additions & 0 deletions core/src/main/java/hudson/slaves/ComputerListener.java
Expand Up @@ -36,6 +36,9 @@

import java.io.IOException;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
* Receives notifications about status changes of {@link Computer}s.
*
Expand Down Expand Up @@ -161,9 +164,21 @@ public void onOnline(Computer c, TaskListener listener) throws IOException, Inte

/**
* Called right after a {@link Computer} went offline.
*
* @deprecated since TODO. Use {@link #onOffline(Computer, OfflineCause)} instead.
*/
@Deprecated
public void onOffline(Computer c) {}

/**
* Called right after a {@link Computer} went offline.
*
* @since TODO
*/
public void onOffline(@Nonnull Computer c, @CheckForNull OfflineCause cause) {
onOffline(c);
}

/**
* Indicates that the computer was marked as temporarily online by the administrator.
* This is the reverse operation of {@link #onTemporarilyOffline(Computer, OfflineCause)}
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/java/hudson/slaves/OfflineCause.java
Expand Up @@ -55,7 +55,10 @@ public abstract class OfflineCause {
public static class SimpleOfflineCause extends OfflineCause {
public final Localizable description;

private SimpleOfflineCause(Localizable description) {
/**
* @since TODO
*/
protected SimpleOfflineCause(Localizable description) {
this.description = description;
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/slaves/SlaveComputer.java
Expand Up @@ -625,7 +625,7 @@ private void closeChannel() {
logger.log(Level.SEVERE, "Failed to terminate channel to " + getDisplayName(), e);
}
for (ComputerListener cl : ComputerListener.all())
cl.onOffline(this);
cl.onOffline(this, offlineCause);
}
}

Expand Down
25 changes: 25 additions & 0 deletions core/src/main/java/jenkins/model/Jenkins.java
Expand Up @@ -3329,6 +3329,31 @@ public void run() {
}.start();
}

@Extension @Restricted(NoExternalUse.class)
public static class MasterRestartNotifyier extends RestartListener {

@Override
public void onRestart() {
Computer computer = Jenkins.getInstance().toComputer();
if (computer == null) return;
RestartCause cause = new RestartCause();
for (ComputerListener listener: ComputerListener.all()) {
listener.onOffline(computer, cause);
}
}

@Override
public boolean isReadyToRestart() throws IOException, InterruptedException {
return true;
}

private static class RestartCause extends OfflineCause.SimpleOfflineCause {
protected RestartCause() {
super(Messages._Jenkins_IsRestarting());
}
}
}

/**
* Shutdown the system.
* @since 1.161
Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/hudson/model/Messages.properties
Expand Up @@ -359,3 +359,4 @@ Jenkins.CheckDisplayName.NameNotUniqueWarning=The display name, "{0}", is used a
Jenkins.CheckDisplayName.DisplayNameNotUniqueWarning=The display name, "{0}", is already in use by another job and could cause confusion and delay.

Jenkins.NotAllowedName="{0}" is not allowed name
Jenkins.IsRestarting=Jenkins is restarting
20 changes: 20 additions & 0 deletions test/src/test/java/jenkins/model/JenkinsTest.java
Expand Up @@ -28,9 +28,11 @@
import com.gargoylesoftware.htmlunit.WebRequestSettings;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import hudson.maven.MavenModuleSet;
import hudson.maven.MavenModuleSetBuild;
import hudson.model.Failure;
import hudson.model.RestartListener;
import hudson.model.RootAction;
import hudson.model.UnprotectedRootAction;
import hudson.model.User;
Expand All @@ -41,6 +43,8 @@
import hudson.security.GlobalMatrixAuthorizationStrategy;
import hudson.security.LegacySecurityRealm;
import hudson.security.Permission;
import hudson.slaves.ComputerListener;
import hudson.slaves.OfflineCause;
import hudson.util.FormValidation;

import org.junit.Assert;
Expand All @@ -51,6 +55,9 @@
import org.jvnet.hudson.test.JenkinsRule.DummySecurityRealm;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.HttpResponse;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import java.net.HttpURLConnection;
import java.net.URL;

Expand Down Expand Up @@ -384,4 +391,17 @@ public HttpResponse doReportError() {
return new Failure("My car is black");
}

@Bug(23551)
public void testComputerListenerNotifiedOnRestart() {
// Simulate restart calling listeners
for (RestartListener listener : RestartListener.all())
listener.onRestart();

ArgumentCaptor<OfflineCause> captor = ArgumentCaptor.forClass(OfflineCause.class);
Mockito.verify(listenerMock).onOffline(Mockito.eq(jenkins.toComputer()), captor.capture());
assertTrue(captor.getValue().toString().contains("restart"));
}

@TestExtension(value = "testComputerListenerNotifiedOnRestart")
public static final ComputerListener listenerMock = Mockito.mock(ComputerListener.class);
}

0 comments on commit 8274c98

Please sign in to comment.