From 9181558132fab0971d1e20301b894a9ed1e97b0f Mon Sep 17 00:00:00 2001 From: kohsuke Date: Fri, 31 Oct 2008 04:42:46 +0000 Subject: [PATCH] Adding auto-restart capability when Hudson runs as a Windows service git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@12887 71c3de6d-444a-0410-be80-ed276b4c234a --- core/pom.xml | 2 +- .../main/java/hudson/lifecycle/Lifecycle.java | 11 ++++++++++- .../lifecycle/WindowsServiceLifecycle.java | 19 +++++++++++++++++++ core/src/main/java/hudson/model/Hudson.java | 4 ++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index a940dd9b1a..9b5cd281be 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -488,7 +488,7 @@ com.sun.winsw winsw - 1.1 + 1.2 bin exe provided diff --git a/core/src/main/java/hudson/lifecycle/Lifecycle.java b/core/src/main/java/hudson/lifecycle/Lifecycle.java index a3a205f083..446b5a2332 100644 --- a/core/src/main/java/hudson/lifecycle/Lifecycle.java +++ b/core/src/main/java/hudson/lifecycle/Lifecycle.java @@ -4,6 +4,7 @@ import hudson.ExtensionPoint; import hudson.model.Hudson; import java.io.File; +import java.io.IOException; /** * Provides the capability for starting/stopping/restarting/uninstalling Hudson. @@ -74,8 +75,16 @@ public abstract class Lifecycle implements ExtensionPoint { * If this life cycle supports a restart of Hudson, do so. * Otherwise, throw {@link UnsupportedOperationException}, * which is what the default implementation does. + * + *

+ * The restart operation may happen synchronously (in which case + * this method will never return), or asynchronously (in which + * case this method will successfully return.) + * + *

+ * Throw an exception if the operation fails unexpectedly. */ - public void restart() { + public void restart() throws IOException, InterruptedException { throw new UnsupportedOperationException(); } } diff --git a/core/src/main/java/hudson/lifecycle/WindowsServiceLifecycle.java b/core/src/main/java/hudson/lifecycle/WindowsServiceLifecycle.java index a22b38d2e0..daa89e9206 100644 --- a/core/src/main/java/hudson/lifecycle/WindowsServiceLifecycle.java +++ b/core/src/main/java/hudson/lifecycle/WindowsServiceLifecycle.java @@ -1,9 +1,28 @@ package hudson.lifecycle; +import org.apache.commons.io.output.ByteArrayOutputStream; +import hudson.util.StreamTaskListener; +import hudson.Launcher.LocalLauncher; +import hudson.FilePath; + +import java.io.File; +import java.io.IOException; + /** * {@link Lifecycle} for Hudson installed as Windows service. * * @author Kohsuke Kawaguchi */ public class WindowsServiceLifecycle extends Lifecycle { + public void restart() throws IOException, InterruptedException { + File me = getHudsonWar(); + File home = me.getParentFile(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + StreamTaskListener task = new StreamTaskListener(baos); + task.getLogger().println("Restarting a service"); + int r = new LocalLauncher(task).launch(new String[]{new File(home, "hudson.exe").getPath(), "restart"}, new String[0], task.getLogger(), new FilePath(home)).join(); + if(r!=0) + throw new IOException(baos.toString()); + } } diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index 97c60c0d88..9e195d7630 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -2116,6 +2116,10 @@ public final class Hudson extends View implements ItemGroup, Node, rsp.sendRedirect2("."); } catch (UnsupportedOperationException e) { sendError("Restart is not supported in this running mode.",req,rsp); + } catch (IOException e) { + sendError(e,req,rsp); + } catch (InterruptedException e) { + sendError(e,req,rsp); } } -- GitLab