Stop the index frontend when the hudson is going down, adding a CLI command to restart the frontend.
authorJan Lahoda <jlahoda@netbeans.org>
Wed, 06 Jul 2011 11:39:05 +0200
changeset 625f7e813947ce3
parent 624 627d12658781
child 626 a318387d41a4
Stop the index frontend when the hudson is going down, adding a CLI command to restart the frontend.
remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/PluginImpl.java
remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/StartWebFrontEnd.java
remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/WebFrontEnd.java
remoting/server/hudson/src/test/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilderTest.java
     1.1 --- a/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/PluginImpl.java	Wed Jul 06 11:38:07 2011 +0200
     1.2 +++ b/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/PluginImpl.java	Wed Jul 06 11:39:05 2011 +0200
     1.3 @@ -50,7 +50,13 @@
     1.4      public void postInitialize() throws Exception {
     1.5          super.postInitialize();
     1.6          InstallIndexingTool.prepareUpdates();
     1.7 -        StartWebFrontEnd.ensureStarted();
     1.8 +        WebFrontEnd.ensureStarted();
     1.9 +    }
    1.10 +
    1.11 +    @Override
    1.12 +    public void stop() throws Exception {
    1.13 +        super.stop();
    1.14 +        WebFrontEnd.stop();
    1.15      }
    1.16  
    1.17  }
     2.1 --- a/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/StartWebFrontEnd.java	Wed Jul 06 11:38:07 2011 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,95 +0,0 @@
     2.4 -/*
     2.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 - *
     2.7 - * Copyright 2011 Sun Microsystems, Inc. All rights reserved.
     2.8 - *
     2.9 - * The contents of this file are subject to the terms of either the GNU
    2.10 - * General Public License Version 2 only ("GPL") or the Common
    2.11 - * Development and Distribution License("CDDL") (collectively, the
    2.12 - * "License"). You may not use this file except in compliance with the
    2.13 - * License. You can obtain a copy of the License at
    2.14 - * http://www.netbeans.org/cddl-gplv2.html
    2.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.16 - * specific language governing permissions and limitations under the
    2.17 - * License.  When distributing the software, include this License Header
    2.18 - * Notice in each file and include the License file at
    2.19 - * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    2.20 - * particular file as subject to the "Classpath" exception as provided
    2.21 - * by Sun in the GPL Version 2 section of the License file that
    2.22 - * accompanied this code. If applicable, add the following below the
    2.23 - * License Header, with the fields enclosed by brackets [] replaced by
    2.24 - * your own identifying information:
    2.25 - * "Portions Copyrighted [year] [name of copyright owner]"
    2.26 - *
    2.27 - * If you wish your version of this file to be governed by only the CDDL
    2.28 - * or only the GPL Version 2, indicate your decision by adding
    2.29 - * "[Contributor] elects to include this software in this distribution
    2.30 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.31 - * single choice of license, a recipient has the option to distribute
    2.32 - * your version of this file under either the CDDL, the GPL Version 2 or
    2.33 - * to extend the choice of license to its licensees as provided above.
    2.34 - * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.35 - * Version 2 license, then the option applies only if the new code is
    2.36 - * made subject to such option by the copyright holder.
    2.37 - *
    2.38 - * Contributor(s):
    2.39 - *
    2.40 - * Portions Copyrighted 2011 Sun Microsystems, Inc.
    2.41 - */
    2.42 -package org.netbeans.modules.jackpot30.hudson;
    2.43 -
    2.44 -import hudson.Launcher;
    2.45 -import hudson.Proc;
    2.46 -import hudson.model.Hudson;
    2.47 -import hudson.util.ArgumentListBuilder;
    2.48 -import hudson.util.LogTaskListener;
    2.49 -import java.io.File;
    2.50 -import java.io.FileOutputStream;
    2.51 -import java.io.IOException;
    2.52 -import java.util.logging.Level;
    2.53 -import java.util.logging.Logger;
    2.54 -
    2.55 -/**
    2.56 - *
    2.57 - * @author lahvac
    2.58 - */
    2.59 -public class StartWebFrontEnd {
    2.60 -
    2.61 -    public static boolean disable = false;
    2.62 -
    2.63 -    private static Proc frontend;
    2.64 -
    2.65 -    public static void ensureStarted() {
    2.66 -        if (disable) return ;
    2.67 -        
    2.68 -        try {
    2.69 -            if (frontend != null && frontend.isAlive()) return;
    2.70 -
    2.71 -            IndexingTool[] tools = Hudson.getInstance().getDescriptorByType(IndexingTool.DescriptorImpl.class).getInstallations();
    2.72 -
    2.73 -            if (tools.length == 0) return;
    2.74 -
    2.75 -            File cacheDir = Hudson.getInstance().getDescriptorByType(IndexingBuilder.DescriptorImpl.class).getCacheDir();
    2.76 -
    2.77 -            if (!cacheDir.exists()) {
    2.78 -                cacheDir.mkdirs();
    2.79 -            }
    2.80 -            
    2.81 -            LogTaskListener listener = new LogTaskListener(Logger.global, Level.INFO);
    2.82 -            IndexingTool tool = tools[0].forNode(Hudson.getInstance(), listener);
    2.83 -
    2.84 -            ArgumentListBuilder args = new ArgumentListBuilder();
    2.85 -            Launcher launcher = new Launcher.LocalLauncher(listener);
    2.86 -            args.add(new File(tool.getHome(), "web.sh")); //XXX
    2.87 -            args.add(cacheDir);
    2.88 -
    2.89 -            frontend = launcher.launch().cmds(args)
    2.90 -                                        .stdout(listener)
    2.91 -                                        .start();
    2.92 -        } catch (IOException ex) {
    2.93 -            Logger.getLogger(StartWebFrontEnd.class.getName()).log(Level.SEVERE, null, ex);
    2.94 -        } catch (InterruptedException ex) {
    2.95 -            Logger.getLogger(StartWebFrontEnd.class.getName()).log(Level.SEVERE, null, ex);
    2.96 -        }
    2.97 -    }
    2.98 -}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/WebFrontEnd.java	Wed Jul 06 11:39:05 2011 +0200
     3.3 @@ -0,0 +1,131 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2011 Sun Microsystems, Inc. All rights reserved.
     3.8 + *
     3.9 + * The contents of this file are subject to the terms of either the GNU
    3.10 + * General Public License Version 2 only ("GPL") or the Common
    3.11 + * Development and Distribution License("CDDL") (collectively, the
    3.12 + * "License"). You may not use this file except in compliance with the
    3.13 + * License. You can obtain a copy of the License at
    3.14 + * http://www.netbeans.org/cddl-gplv2.html
    3.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.16 + * specific language governing permissions and limitations under the
    3.17 + * License.  When distributing the software, include this License Header
    3.18 + * Notice in each file and include the License file at
    3.19 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    3.20 + * particular file as subject to the "Classpath" exception as provided
    3.21 + * by Sun in the GPL Version 2 section of the License file that
    3.22 + * accompanied this code. If applicable, add the following below the
    3.23 + * License Header, with the fields enclosed by brackets [] replaced by
    3.24 + * your own identifying information:
    3.25 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.26 + *
    3.27 + * If you wish your version of this file to be governed by only the CDDL
    3.28 + * or only the GPL Version 2, indicate your decision by adding
    3.29 + * "[Contributor] elects to include this software in this distribution
    3.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    3.31 + * single choice of license, a recipient has the option to distribute
    3.32 + * your version of this file under either the CDDL, the GPL Version 2 or
    3.33 + * to extend the choice of license to its licensees as provided above.
    3.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    3.35 + * Version 2 license, then the option applies only if the new code is
    3.36 + * made subject to such option by the copyright holder.
    3.37 + *
    3.38 + * Contributor(s):
    3.39 + *
    3.40 + * Portions Copyrighted 2011 Sun Microsystems, Inc.
    3.41 + */
    3.42 +package org.netbeans.modules.jackpot30.hudson;
    3.43 +
    3.44 +import hudson.Extension;
    3.45 +import hudson.Launcher;
    3.46 +import hudson.Proc;
    3.47 +import hudson.cli.CLICommand;
    3.48 +import hudson.model.Hudson;
    3.49 +import hudson.util.ArgumentListBuilder;
    3.50 +import hudson.util.LogTaskListener;
    3.51 +import java.io.File;
    3.52 +import java.io.FileOutputStream;
    3.53 +import java.io.IOException;
    3.54 +import java.util.logging.Level;
    3.55 +import java.util.logging.Logger;
    3.56 +
    3.57 +/**
    3.58 + *
    3.59 + * @author lahvac
    3.60 + */
    3.61 +public class WebFrontEnd {
    3.62 +
    3.63 +    public static boolean disable = false;
    3.64 +
    3.65 +    private static Proc frontend;
    3.66 +
    3.67 +    public static synchronized void ensureStarted() {
    3.68 +        if (disable) return ;
    3.69 +        
    3.70 +        try {
    3.71 +            if (frontend != null && frontend.isAlive()) {
    3.72 +                frontend = null;
    3.73 +            }
    3.74 +
    3.75 +            IndexingTool[] tools = Hudson.getInstance().getDescriptorByType(IndexingTool.DescriptorImpl.class).getInstallations();
    3.76 +
    3.77 +            if (tools.length == 0) return;
    3.78 +
    3.79 +            File cacheDir = Hudson.getInstance().getDescriptorByType(IndexingBuilder.DescriptorImpl.class).getCacheDir();
    3.80 +
    3.81 +            if (!cacheDir.exists()) {
    3.82 +                cacheDir.mkdirs();
    3.83 +            }
    3.84 +            
    3.85 +            LogTaskListener listener = new LogTaskListener(Logger.global, Level.INFO);
    3.86 +            IndexingTool tool = tools[0].forNode(Hudson.getInstance(), listener);
    3.87 +
    3.88 +            ArgumentListBuilder args = new ArgumentListBuilder();
    3.89 +            Launcher launcher = new Launcher.LocalLauncher(listener);
    3.90 +            args.add(new File(tool.getHome(), "web.sh")); //XXX
    3.91 +            args.add(cacheDir);
    3.92 +
    3.93 +            frontend = launcher.launch().cmds(args)
    3.94 +                                        .stdout(listener)
    3.95 +                                        .start();
    3.96 +        } catch (IOException ex) {
    3.97 +            Logger.getLogger(WebFrontEnd.class.getName()).log(Level.SEVERE, null, ex);
    3.98 +        } catch (InterruptedException ex) {
    3.99 +            Logger.getLogger(WebFrontEnd.class.getName()).log(Level.SEVERE, null, ex);
   3.100 +        }
   3.101 +    }
   3.102 +
   3.103 +    public static synchronized void stop() {
   3.104 +        try {
   3.105 +            if (frontend == null || !frontend.isAlive()) {
   3.106 +                frontend = null;
   3.107 +                return;
   3.108 +            }
   3.109 +
   3.110 +            frontend.kill();
   3.111 +        } catch (IOException ex) {
   3.112 +            Logger.getLogger(WebFrontEnd.class.getName()).log(Level.SEVERE, null, ex);
   3.113 +        } catch (InterruptedException ex) {
   3.114 +            Logger.getLogger(WebFrontEnd.class.getName()).log(Level.SEVERE, null, ex);
   3.115 +        }
   3.116 +    }
   3.117 +
   3.118 +    @Extension
   3.119 +    public static final class RestartIndexingFrontend extends CLICommand {
   3.120 +
   3.121 +        @Override
   3.122 +        public String getShortDescription() {
   3.123 +            return "Restart indexing frontend";
   3.124 +        }
   3.125 +
   3.126 +        @Override
   3.127 +        protected int run() throws Exception {
   3.128 +            stop();
   3.129 +            ensureStarted();
   3.130 +            return 0;
   3.131 +        }
   3.132 +
   3.133 +    }
   3.134 +}
     4.1 --- a/remoting/server/hudson/src/test/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilderTest.java	Wed Jul 06 11:38:07 2011 +0200
     4.2 +++ b/remoting/server/hudson/src/test/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilderTest.java	Wed Jul 06 11:39:05 2011 +0200
     4.3 @@ -70,7 +70,7 @@
     4.4  
     4.5      @Override
     4.6      protected void setUp() throws Exception {
     4.7 -        StartWebFrontEnd.disable = true;
     4.8 +        WebFrontEnd.disable = true;
     4.9          
    4.10          super.setUp();
    4.11