Commits

Jan Lahoda committed 63f9365

Trying to make downloading of indexer more explicit.

Comments (0)

Files changed (2)

remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/PluginImpl.java

  */
 package org.netbeans.modules.jackpot30.hudson;
 
+import hudson.Extension;
 import hudson.Plugin;
+import hudson.model.listeners.ItemListener;
+import java.io.IOException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**https://issues.jenkins-ci.org/browse/JENKINS-5427
  *
 public class PluginImpl extends Plugin {
 
     @Override
-    public void postInitialize() throws Exception {
-        super.postInitialize();
-        InstallIndexingTool.prepareUpdates();
-        WebFrontEnd.ensureStarted();
-    }
-
-    @Override
     public void stop() throws Exception {
         super.stop();
         WebFrontEnd.stop();
     }
 
+    @Extension
+    public static class OnLoadListener extends ItemListener {
+        @Override
+        public void onLoaded() {
+            try {
+                super.onLoaded();
+                InstallIndexingTool.prepareUpdates();
+                WebFrontEnd.ensureStarted();
+            } catch (IOException ex) {
+                Logger.getLogger(PluginImpl.class.getName()).log(Level.SEVERE, null, ex);
+            } catch (InterruptedException ex) {
+                Logger.getLogger(PluginImpl.class.getName()).log(Level.SEVERE, null, ex);
+            }
+        }
+    }
+
 }

remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/WebFrontEnd.java

 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2011 Sun Microsystems, Inc. All rights reserved.
+ * Copyright 2011-2012 Sun Microsystems, Inc. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common
  *
  * Contributor(s):
  *
- * Portions Copyrighted 2011 Sun Microsystems, Inc.
+ * Portions Copyrighted 2011-2012 Sun Microsystems, Inc.
  */
 package org.netbeans.modules.jackpot30.hudson;
 
 import hudson.Extension;
 import hudson.Launcher;
+import hudson.Launcher.ProcStarter;
 import hudson.Proc;
 import hudson.cli.CLICommand;
 import hudson.model.Hudson;
+import hudson.model.Label;
+import hudson.model.Node;
+import hudson.model.Queue;
+import hudson.model.Queue.Executable;
+import hudson.model.Queue.FlyweightTask;
+import hudson.model.Queue.NonBlockingTask;
+import hudson.model.Queue.Task;
+import hudson.model.Queue.TransientTask;
+import hudson.model.ResourceList;
+import hudson.model.queue.CauseOfBlockage;
+import hudson.model.queue.SubTask;
 import hudson.util.ArgumentListBuilder;
 import hudson.util.LogTaskListener;
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
     public static boolean disable = false;
 
     private static Proc frontend;
+    private static long requestId;
 
     public static synchronized void ensureStarted() {
         if (disable) return ;
                 frontend = null;
             }
 
-            IndexingTool[] tools = Hudson.getInstance().getDescriptorByType(IndexingTool.DescriptorImpl.class).getInstallations();
-
-            if (tools.length == 0) return;
-
-            File cacheDir = Hudson.getInstance().getDescriptorByType(IndexingBuilder.DescriptorImpl.class).getCacheDir();
-
-            if (!cacheDir.exists()) {
-                cacheDir.mkdirs();
-            }
-            
-            LogTaskListener listener = new LogTaskListener(Logger.global, Level.INFO);
-            IndexingTool tool = tools[0].forNode(Hudson.getInstance(), listener);
-
-            ArgumentListBuilder args = new ArgumentListBuilder();
-            Launcher launcher = new Launcher.LocalLauncher(listener);
-            args.add(new File(tool.getHome(), "web.sh")); //XXX
-            args.add(cacheDir);
-
-            frontend = launcher.launch().cmds(args)
-                                        .stdout(listener)
-                                        .start();
+            Queue.getInstance().schedule(new StartIndexingFrontEnd(++requestId), 0);
         } catch (IOException ex) {
             Logger.getLogger(WebFrontEnd.class.getName()).log(Level.SEVERE, null, ex);
         } catch (InterruptedException ex) {
 
     public static synchronized void stop() {
         try {
+            requestId++;
+
             if (frontend == null || !frontend.isAlive()) {
                 frontend = null;
                 return;
         }
     }
 
+    private static synchronized void doStart(ProcStarter proc, long originalRequestId) throws IOException {
+        if (requestId == originalRequestId) {
+            frontend = proc.start();
+        }
+    }
+
     @Extension
     public static final class RestartIndexingFrontend extends CLICommand {
 
         }
 
     }
+
+    private static class StartIndexingFrontEnd implements Task, TransientTask, FlyweightTask, NonBlockingTask {
+
+        private final long requestId;
+
+        public StartIndexingFrontEnd(long requestId) {
+            this.requestId = requestId;
+        }
+
+        public boolean isBuildBlocked() {
+            return false;
+        }
+
+        public String getWhyBlocked() {
+            return null;
+        }
+
+        public CauseOfBlockage getCauseOfBlockage() {
+            return null;
+        }
+
+        public String getName() {
+            return getDisplayName();
+        }
+
+        public String getFullDisplayName() {
+            return getDisplayName();
+        }
+
+        public void checkAbortPermission() {
+            Hudson.getInstance().getACL().checkPermission(Hudson.ADMINISTER);
+        }
+
+        public boolean hasAbortPermission() {
+            return Hudson.getInstance().getACL().hasPermission(Hudson.ADMINISTER);
+        }
+
+        public String getUrl() {
+            return "index";
+        }
+
+        public boolean isConcurrentBuild() {
+            return false;
+        }
+
+        public Collection<? extends SubTask> getSubTasks() {
+            return Collections.singletonList(this);
+        }
+
+        public String getDisplayName() {
+            return "Start Indexing Web Frontend";
+        }
+
+        public Label getAssignedLabel() {
+            return Hudson.getInstance().getSelfLabel();
+        }
+
+        public Node getLastBuiltOn() {
+            return null;
+        }
+
+        public long getEstimatedDuration() {
+            return -1;
+        }
+
+        public Executable createExecutable() throws IOException {
+            return new Executable() {
+                public SubTask getParent() {
+                    return StartIndexingFrontEnd.this;
+                }
+
+                public void run() {
+        try {
+            IndexingTool[] tools = Hudson.getInstance().getDescriptorByType(IndexingTool.DescriptorImpl.class).getInstallations();
+
+            if (tools.length == 0) return;
+
+            File cacheDir = Hudson.getInstance().getDescriptorByType(IndexingBuilder.DescriptorImpl.class).getCacheDir();
+
+            if (!cacheDir.exists()) {
+                cacheDir.mkdirs();
+            }
+
+            LogTaskListener listener = new LogTaskListener(Logger.global, Level.INFO);
+            IndexingTool tool = tools[0].forNode(Hudson.getInstance(), listener);
+
+            ArgumentListBuilder args = new ArgumentListBuilder();
+            Launcher launcher = new Launcher.LocalLauncher(listener);
+            args.add(new File(tool.getHome(), "web.sh")); //XXX
+            args.add(cacheDir);
+
+            doStart(launcher.launch().cmds(args)
+                                     .stdout(listener),
+                    requestId);
+        } catch (IOException ex) {
+            Logger.getLogger(WebFrontEnd.class.getName()).log(Level.SEVERE, null, ex);
+        } catch (InterruptedException ex) {
+            Logger.getLogger(WebFrontEnd.class.getName()).log(Level.SEVERE, null, ex);
+        }
+                }
+
+                public long getEstimatedDuration() {
+                    return -1;
+                }
+
+                @Override
+                public String toString() {
+                    return getDisplayName();
+                }
+
+            };
+        }
+
+        public Task getOwnerTask() {
+            return this;
+        }
+
+        public Object getSameNodeConstraint() {
+            return null;
+        }
+
+        public ResourceList getResourceList() {
+            return ResourceList.EMPTY;
+        }
+
+    }
 }