Esempio n. 1
0
    @Test
    public void testGetQueueItems() throws IOException, Exception{
        ListView view1 = listView("view1");
        view1.filterQueue = true;
        ListView view2 = listView("view2");
        view2.filterQueue = true;

        FreeStyleProject inView1 = j.createFreeStyleProject("in-view1");
        inView1.setAssignedLabel(j.jenkins.getLabelAtom("without-any-slave"));
        view1.add(inView1);

        MatrixProject inView2 = j.createMatrixProject("in-view2");
        inView2.setAssignedLabel(j.jenkins.getLabelAtom("without-any-slave"));
        view2.add(inView2);

        FreeStyleProject notInView = j.createFreeStyleProject("not-in-view");
        notInView.setAssignedLabel(j.jenkins.getLabelAtom("without-any-slave"));

        FreeStyleProject inBothViews = j.createFreeStyleProject("in-both-views");
        inBothViews.setAssignedLabel(j.jenkins.getLabelAtom("without-any-slave"));
        view1.add(inBothViews);
        view2.add(inBothViews);

        Queue.getInstance().schedule(notInView, 0);
        Queue.getInstance().schedule(inView1, 0);
        Queue.getInstance().schedule(inView2, 0);
        Queue.getInstance().schedule(inBothViews, 0);

        Thread.sleep(1000);

        assertContainsItems(view1, inView1, inBothViews);
        assertNotContainsItems(view1, notInView, inView2);
        assertContainsItems(view2, inView2, inBothViews);
        assertNotContainsItems(view2, notInView, inView1);
    }
Esempio n. 2
0
 @Override
 public boolean cancel(boolean mayInterruptIfRunning) {
   Queue q = Hudson.getInstance().getQueue();
   synchronized (q) {
     if (executor != null) {
       if (mayInterruptIfRunning) executor.interrupt();
       return mayInterruptIfRunning;
     }
     return q.cancel(task);
   }
 }
Esempio n. 3
0
 @Override
 public boolean cancel(boolean mayInterruptIfRunning) {
   Queue q = Jenkins.getInstance().getQueue();
   synchronized (q) {
     synchronized (this) {
       if (!executors.isEmpty()) {
         if (mayInterruptIfRunning) for (Executor e : executors) e.interrupt();
         return mayInterruptIfRunning;
       }
       return q.cancel(task);
     }
   }
 }
Esempio n. 4
0
 private void assertNotContainsItems(View view, Task... items) {
     for (Task job: items) {
         assertFalse(
                 "Queued items for " + view.getDisplayName() + " should not contain " + job.getDisplayName(),
                 view.getQueueItems().contains(Queue.getInstance().getItem(job))
         );
     }
 }
Esempio n. 5
0
 /**
  * Called when a new {@link Computer} object is introduced (such as when Hudson started, or when a
  * new slave is added.)
  *
  * <p>The default implementation of this method delegates to {@link #check(Computer)}, but this
  * allows {@link RetentionStrategy} to distinguish the first time invocation from the rest.
  *
  * @since 1.275
  */
 public void start(final T c) {
   Queue.withLock(
       new Runnable() {
         @Override
         public void run() {
           check(c);
         }
       });
 }
Esempio n. 6
0
 /**
  * Updates an existing node on disk. If the node instance is not in the list of nodes, then this
  * will be a no-op, even if there is another instance with the same {@link Node#getNodeName()}.
  *
  * @param node the node to be updated.
  * @return {@code true}, if the node was updated. {@code false}, if the node was not in the list
  *     of nodes.
  * @throws IOException if the node could not be persisted.
  */
 public boolean updateNode(final @Nonnull Node node) throws IOException {
   if (node == nodes.get(node.getNodeName())) {
     Queue.withLock(
         new Runnable() {
           @Override
           public void run() {
             jenkins.trimLabels();
           }
         });
     persistNode(node);
     return true;
   }
   return false;
 }
Esempio n. 7
0
 /**
  * Adds a node. If a node of the same name already exists then that node will be replaced.
  *
  * @param node the new node.
  * @throws IOException if the list of nodes could not be persisted.
  */
 public void addNode(final @Nonnull Node node) throws IOException {
   if (node != nodes.get(node.getNodeName())) {
     // TODO we should not need to lock the queue for adding nodes but until we have a way to
     // update the
     // computer list for just the new node
     Queue.withLock(
         new Runnable() {
           @Override
           public void run() {
             nodes.put(node.getNodeName(), node);
             jenkins.updateComputerList();
             jenkins.trimLabels();
           }
         });
     persistNode(node);
   }
 }
Esempio n. 8
0
 /**
  * Sets the list of nodes.
  *
  * @param nodes the new list of nodes.
  * @throws IOException if the new list of nodes could not be persisted.
  */
 public void setNodes(final @Nonnull Collection<? extends Node> nodes) throws IOException {
   Queue.withLock(
       new Runnable() {
         @Override
         public void run() {
           Set<String> toRemove = new HashSet<String>(Nodes.this.nodes.keySet());
           for (Node n : nodes) {
             final String name = n.getNodeName();
             toRemove.remove(name);
             Nodes.this.nodes.put(name, n);
           }
           Nodes.this
               .nodes
               .keySet()
               .removeAll(toRemove); // directory clean up will be handled by save
           jenkins.updateComputerList();
           jenkins.trimLabels();
         }
       });
   save();
 }
Esempio n. 9
0
 /**
  * Removes a node. If the node instance is not in the list of nodes, then this will be a no-op,
  * even if there is another instance with the same {@link Node#getNodeName()}.
  *
  * @param node the node instance to remove.
  * @throws IOException if the list of nodes could not be persisted.
  */
 public void removeNode(final @Nonnull Node node) throws IOException {
   if (node == nodes.get(node.getNodeName())) {
     Queue.withLock(
         new Runnable() {
           @Override
           public void run() {
             Computer c = node.toComputer();
             if (c != null) {
               c.recordTermination();
               c.disconnect(OfflineCause.create(hudson.model.Messages._Hudson_NodeBeingRemoved()));
             }
             if (node == nodes.remove(node.getNodeName())) {
               jenkins.updateComputerList();
               jenkins.trimLabels();
             }
           }
         });
     // no need for a full save() so we just do the minimum
     Util.deleteRecursive(new File(getNodesDir(), node.getNodeName()));
   }
 }
Esempio n. 10
0
  /** Checks the persistence of queue. */
  public void testPersistence() throws Exception {
    Queue q = hudson.getQueue();

    // prevent execution to push stuff into the queue
    hudson.setNumExecutors(0);
    hudson.setNodes(hudson.getNodes());

    FreeStyleProject testProject = createFreeStyleProject("test");
    testProject.scheduleBuild(new UserCause());
    q.save();

    System.out.println(FileUtils.readFileToString(new File(hudson.getRootDir(), "queue.xml")));

    assertEquals(1, q.getItems().length);
    q.clear();
    assertEquals(0, q.getItems().length);

    // load the contents back
    q.load();
    assertEquals(1, q.getItems().length);

    // did it bind back to the same object?
    assertSame(q.getItems()[0].task, testProject);
  }
Esempio n. 11
0
 /**
  * Loads the nodes from disk.
  *
  * @throws IOException if the nodes could not be deserialized.
  */
 public void load() throws IOException {
   final File nodesDir = getNodesDir();
   final File[] subdirs =
       nodesDir.listFiles(
           new FileFilter() {
             public boolean accept(File child) {
               return child.isDirectory();
             }
           });
   final Map<String, Node> newNodes = new TreeMap<String, Node>();
   if (subdirs != null) {
     for (File subdir : subdirs) {
       try {
         XmlFile xmlFile = new XmlFile(Jenkins.XSTREAM, new File(subdir, "config.xml"));
         if (xmlFile.exists()) {
           Node node = (Node) xmlFile.read();
           newNodes.put(node.getNodeName(), node);
         }
       } catch (IOException e) {
         Logger.getLogger(Nodes.class.getName()).log(Level.WARNING, "could not load " + subdir, e);
       }
     }
   }
   Queue.withLock(
       new Runnable() {
         @Override
         public void run() {
           for (Iterator<Map.Entry<String, Node>> i = nodes.entrySet().iterator(); i.hasNext(); ) {
             if (!(i.next().getValue() instanceof EphemeralNode)) {
               i.remove();
             }
           }
           nodes.putAll(newNodes);
           jenkins.updateComputerList();
           jenkins.trimLabels();
         }
       });
 }
Esempio n. 12
0
  /** Can {@link Queue} successfully recover removal? */
  public void testPersistence2() throws Exception {
    Queue q = hudson.getQueue();

    // prevent execution to push stuff into the queue
    hudson.setNumExecutors(0);
    hudson.setNodes(hudson.getNodes());

    FreeStyleProject testProject = createFreeStyleProject("test");
    testProject.scheduleBuild(new UserCause());
    q.save();

    System.out.println(FileUtils.readFileToString(new File(hudson.getRootDir(), "queue.xml")));

    assertEquals(1, q.getItems().length);
    q.clear();
    assertEquals(0, q.getItems().length);

    // delete the project before loading the queue back
    testProject.delete();
    q.load();
    assertEquals(0, q.getItems().length);
  }
Esempio n. 13
0
    @Override
    @GuardedBy("hudson.model.Queue.lock")
    public long check(final SlaveComputer c) {
      if (c.isOffline() && c.isLaunchSupported()) {
        final HashMap<Computer, Integer> availableComputers = new HashMap<Computer, Integer>();
        for (Computer o : Jenkins.getInstance().getComputers()) {
          if ((o.isOnline() || o.isConnecting()) && o.isPartiallyIdle() && o.isAcceptingTasks()) {
            final int idleExecutors = o.countIdle();
            if (idleExecutors > 0) availableComputers.put(o, idleExecutors);
          }
        }

        boolean needComputer = false;
        long demandMilliseconds = 0;
        for (Queue.BuildableItem item : Queue.getInstance().getBuildableItems()) {
          // can any of the currently idle executors take this task?
          // assume the answer is no until we can find such an executor
          boolean needExecutor = true;
          for (Computer o : Collections.unmodifiableSet(availableComputers.keySet())) {
            Node otherNode = o.getNode();
            if (otherNode != null && otherNode.canTake(item) == null) {
              needExecutor = false;
              final int availableExecutors = availableComputers.remove(o);
              if (availableExecutors > 1) {
                availableComputers.put(o, availableExecutors - 1);
              } else {
                availableComputers.remove(o);
              }
              break;
            }
          }

          // this 'item' cannot be built by any of the existing idle nodes, but it can be built by
          // 'c'
          Node checkedNode = c.getNode();
          if (needExecutor && checkedNode != null && checkedNode.canTake(item) == null) {
            demandMilliseconds = System.currentTimeMillis() - item.buildableStartMilliseconds;
            needComputer = demandMilliseconds > inDemandDelay * 1000 * 60 /*MINS->MILLIS*/;
            break;
          }
        }

        if (needComputer) {
          // we've been in demand for long enough
          logger.log(
              Level.INFO,
              "Launching computer {0} as it has been in demand for {1}",
              new Object[] {c.getName(), Util.getTimeSpanString(demandMilliseconds)});
          c.connect(false);
        }
      } else if (c.isIdle()) {
        final long idleMilliseconds = System.currentTimeMillis() - c.getIdleStartMilliseconds();
        if (idleMilliseconds > idleDelay * 1000 * 60 /*MINS->MILLIS*/) {
          // we've been idle for long enough
          logger.log(
              Level.INFO,
              "Disconnecting computer {0} as it has been idle for {1}",
              new Object[] {c.getName(), Util.getTimeSpanString(idleMilliseconds)});
          c.disconnect(new OfflineCause.IdleOfflineCause());
        } else {
          // no point revisiting until we can be confident we will be idle
          return TimeUnit.MILLISECONDS.toMinutes(
              TimeUnit.MINUTES.toMillis(idleDelay) - idleMilliseconds);
        }
      }
      return 1;
    }
Esempio n. 14
0
 protected void doRun() {
   Queue q = queue.get();
   if (q != null) q.maintain();
   else cancel();
 }