From de68fa256887d9d5bdf2604f1d2defa31bce00cc Mon Sep 17 00:00:00 2001 From: kohsuke Date: Tue, 14 Jul 2009 17:38:17 +0000 Subject: [PATCH] Tentatively added additional extension points to control queue behaviors. (patch) git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@19695 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/model/Queue.java | 82 ++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/core/src/main/java/hudson/model/Queue.java b/core/src/main/java/hudson/model/Queue.java index bcf0f44e49..88aefa5589 100644 --- a/core/src/main/java/hudson/model/Queue.java +++ b/core/src/main/java/hudson/model/Queue.java @@ -24,6 +24,8 @@ package hudson.model; import hudson.BulkChange; +import hudson.ExtensionList; +import hudson.ExtensionPoint; import hudson.Util; import hudson.XmlFile; import hudson.remoting.AsyncFutureImpl; @@ -189,6 +191,8 @@ public class Queue extends ResourceController implements Saveable { private volatile transient LoadBalancer loadBalancer; + public volatile QueueSortingHandler sortingHandler = null; + public Queue(LoadBalancer loadBalancer) { this.loadBalancer = loadBalancer.sanitize(); // if all the executors are busy doing something, then the queue won't be maintained in @@ -370,6 +374,31 @@ public class Queue extends ResourceController implements Saveable { * That said, one can still look at {@link WaitingItem#future}, {@link WaitingItem#id}, etc. */ public synchronized WaitingItem schedule(Task p, int quietPeriod, List actions) { + boolean shouldSchedule = true; + for(QueueDecisionHandler h : QueueDecisionHandler.all()) { + shouldSchedule = shouldSchedule && h.shouldSchedule(p, actions); + } + + WaitingItem added = null; + if (shouldSchedule) { + added = scheduleInternal(p, quietPeriod, actions); + } + return added; + } + + /** + * Schedules an execution of a task. + * + * @since 1.311 + * @return + * null if this task is already in the queue and therefore the add operation was no-op. + * Otherwise indicates the {@link WaitingItem} object added, although the nature of the queue + * is that such {@link Item} only captures the state of the item at a particular moment, + * and by the time you inspect the object, some of its information can be already stale. + * + * That said, one can still look at {@link WaitingItem#future}, {@link WaitingItem#id}, etc. + */ + private synchronized WaitingItem scheduleInternal(Task p, int quietPeriod, List actions) { WaitingItem added=null; List items = getItems(p); Calendar due = new GregorianCalendar(); @@ -847,6 +876,9 @@ public class Queue extends ResourceController implements Saveable { blockedProjects.put(p,new BlockedItem(top)); } } + + if (sortingHandler != null) + sortingHandler.sortBuildableItems(buildables); } public Api getApi() { @@ -1116,6 +1148,56 @@ public class Queue extends ResourceController implements Saveable { public boolean shouldSchedule(List actions); } + /** + * Extension point for deciding if particular job should be scheduled or not + * + *

+ * This extension point is still a subject to change, as we are seeking more + * comprehensive Queue pluggability. See HUDSON-2072. + * + * @since 1.316 + */ + public static abstract class QueueDecisionHandler implements ExtensionPoint { + /** + * Returns whether the new item should be scheduled. + */ + public abstract boolean shouldSchedule(Task p, List actions); + + /** + * All registered {@link QueueDecisionHandler}s + * @return + */ + public static ExtensionList all() { + return Hudson.getInstance().getExtensionList(QueueDecisionHandler.class); + } + } + + /** + * Extension point for sorting buildable items + * + *

+ * This extension point is still a subject to change, as we are seeking more + * comprehensive Queue pluggability. See HUDSON-2072. + * + * @since 1.316 + */ + public static abstract class QueueSortingHandler implements ExtensionPoint { + /** + * Sorts the buildable items list. The items at the beginning will be executed + * before the items at the end of the list + * @param buildables + */ + public abstract void sortBuildableItems(ItemList buildables); + + /** + * All registered {@link QueueSortingHandler}s + * @return + */ + public static ExtensionList all() { + return Hudson.getInstance().getExtensionList(QueueSortingHandler.class); + } + } + /** * {@link Item} in the {@link Queue#waitingList} stage. */