Class Queue
- java.lang.Object
-
- hudson.model.ResourceController
-
- hudson.model.Queue
-
- All Implemented Interfaces:
Saveable
@ExportedBean public class Queue extends ResourceController implements Saveable
Build queue.This class implements the core scheduling logic.
Queue.Task
represents the executable task that are placed in the queue. While in the queue, it's wrapped intoQueue.Item
so that we can keep track of additional data used for deciding what to execute when.Items in queue goes through several stages, as depicted below:
(enter) --> waitingList --+--> blockedProjects | ^ | | | v +--> buildables ---> pending ---> left ^ | | | +---(rarely)---+
Note: In the normal case of events pending items only move to left. However they can move back if the node they are assigned to execute on disappears before their
Executor
thread starts, where the node is removed before theQueue.Executable
has been instantiated it is safe to move the pending item back to buildable. Once theQueue.Executable
has been instantiated the only option is to let theQueue.Executable
bomb out as soon as it starts to try an execute on the node that no longer exists.In addition, at any stage, an item can be removed from the queue (for example, when the user cancels a job in the queue.) See the corresponding field for their exact meanings.
- Author:
- Kohsuke Kawaguchi
- See Also:
QueueListener
,QueueTaskDispatcher
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
Queue.BlockedItem
Queue.Item
in theblockedProjects
stage.static class
Queue.BuildableItem
Queue.Item
in thebuildables
stage.static interface
Queue.Executable
Represents the real meat of the computation run byExecutor
.static interface
Queue.FlyweightTask
MarksQueue.Task
s that do not consumeExecutor
.static class
Queue.Item
Item in a queue.static class
Queue.JobOffer
Data structure created for each idleExecutor
.static class
Queue.LeftItem
Queue.Item
in theleftItems
stage.static interface
Queue.NonBlockingTask
MarksQueue.Task
s that are not affected by the Jenkins.isQuietingDown() quieting down}, because these tasks keep other tasks executing.static class
Queue.NotWaitingItem
Common part betweenQueue.BlockedItem
andQueue.BuildableItem
.static interface
Queue.QueueAction
An optional interface for actions on Queue.Item.static class
Queue.QueueDecisionHandler
Extension point for deciding if particular job should be scheduled or not.static class
Queue.Saver
Schedulesave()
call for near future once items change.static class
Queue.StubItem
A Stub class forQueue.Item
which exposes only the name of the Task to be displayed when the user has DISCOVERY permissions only.static class
Queue.StubTask
A Stub class forQueue.Task
which exposes only the name of the Task to be displayed when the user has DISCOVERY permissions only.static interface
Queue.Task
Task whose execution is controlled by the queue.static interface
Queue.TransientTask
MarksQueue.Task
s that are not persisted.static class
Queue.WaitingItem
Queue.Item
in thewaitingList
stage.
-
Field Summary
Fields Modifier and Type Field Description static com.thoughtworks.xstream.XStream
XSTREAM
ThisXStream
instance is used to persistQueue.Task
s.
-
Constructor Summary
Constructors Constructor Description Queue(LoadBalancer loadBalancer)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
_await()
protected void
_signalAll()
protected boolean
_tryWithLock(Runnable runnable)
protected <V,T extends Throwable>
V_withLock(hudson.remoting.Callable<V,T> callable)
Some operations require to be performed with theQueue
lock held.protected void
_withLock(Runnable runnable)
Some operations require to be performed with theQueue
lock held.protected <V> V
_withLock(Callable<V> callable)
Some operations require to be performed with theQueue
lock held.boolean
add(AbstractProject p)
Deprecated.as of 1.311 Useschedule(AbstractProject)
boolean
add(AbstractProject p, int quietPeriod)
Deprecated.as of 1.311 Useschedule(Task, int)
boolean
add(Queue.Task p, int quietPeriod)
Deprecated.as of 1.311 Useschedule(Task, int)
boolean
add(Queue.Task p, int quietPeriod, Action... actions)
Deprecated.as of 1.311 Useschedule(Task, int, Action...)
boolean
cancel(Queue.Item item)
boolean
cancel(Queue.Task p)
Cancels the item in the queue.void
clear()
Wipes out all the items currently in the queue, as if all of them are cancelled at once.void
clearLeftItems()
Immediately clear thegetLeftItems()
cache.boolean
contains(Queue.Task t)
Returns true if this queue contains the said project.int
countBuildableItems()
Counts all theQueue.BuildableItem
s currently in the queue.int
countBuildableItemsFor(Label l)
How manyQueue.BuildableItem
s are assigned for the given label?org.kohsuke.stapler.HttpResponse
doCancelItem(long id)
Called fromqueue.jelly
andqueue-items.jelly
.Api
getApi()
List<Queue.Item>
getApproximateItemsQuickly()
Deprecated.UsegetItems()
directly.protected List<Queue.BlockedItem>
getBlockedItems()
Gets the snapshot of allQueue.BlockedItem
s.List<Queue.BuildableItem>
getBuildableItems()
Gets the snapshot of allQueue.BuildableItem
s.List<Queue.BuildableItem>
getBuildableItems(Computer c)
Gets all theQueue.BuildableItem
s that are waiting for an executor in the givenComputer
.Queue.StubItem[]
getDiscoverableItems()
Returns an array of Item for which it is only visible the name of the task.static Queue
getInstance()
Queue.Item
getItem(long id)
Queue.Item
getItem(Queue.Task t)
Gets the information about the queue item for the given project.Queue.Item[]
getItems()
Gets a snapshot of items in the queue.List<Queue.Item>
getItems(Queue.Task t)
Gets the information about the queue item for the given project.Collection<Queue.LeftItem>
getLeftItems()
Returns the snapshot of allQueue.LeftItem
s.LoadBalancer
getLoadBalancer()
List<Queue.BuildableItem>
getPendingItems()
Gets the snapshot of allQueue.BuildableItem
s.QueueSorter
getSorter()
List<Queue.Item>
getUnblockedItems()
Gets all items that are in the queue but not blockedSet<Queue.Task>
getUnblockedTasks()
Works just likegetUnblockedItems()
but return tasks.static boolean
ifBlockedByHudsonShutdown(Queue.Task task)
Deprecated.UseisBlockedByShutdown(hudson.model.Queue.Task)
instead.static void
init(Jenkins h)
Restores the queue content during the start up.static boolean
isBlockedByShutdown(Queue.Task task)
Checks whether a task should not be scheduled becauseJenkins.isQuietingDown()
.boolean
isEmpty()
boolean
isPending(Queue.Task t)
Is the given task currently pending execution?void
load()
Loads the queue contents that wassaved
.void
maintain()
Queue maintenance.void
save()
Persists the queue contents to the disk.Queue.WaitingItem
schedule(AbstractProject p)
Schedule a new build for this project.Queue.WaitingItem
schedule(Queue.Task p, int quietPeriod)
Queue.WaitingItem
schedule(Queue.Task p, int quietPeriod, Action... actions)
Convenience wrapper method aroundschedule(Task, int, List)
Queue.WaitingItem
schedule(Queue.Task p, int quietPeriod, List<Action> actions)
Deprecated.as of 1.521 Useschedule2(Task, int, List)
ScheduleResult
schedule2(Queue.Task p, int quietPeriod, Action... actions)
Convenience wrapper method aroundschedule2(Task, int, List)
ScheduleResult
schedule2(Queue.Task p, int quietPeriod, List<Action> actions)
Schedules an execution of a task.Future<?>
scheduleMaintenance()
Checks the queue and runs anything that can be run.void
setLoadBalancer(LoadBalancer loadBalancer)
void
setSorter(QueueSorter sorter)
int
strictCountBuildableItemsFor(Label l)
How manyQueue.BuildableItem
s are assigned for the given label?static boolean
tryWithLock(Runnable runnable)
static <V,T extends Throwable>
VwithLock(hudson.remoting.Callable<V,T> callable)
Some operations require theQueue
lock held.static void
withLock(Runnable runnable)
Some operations require to be performed with theQueue
lock held.static <V> V
withLock(Callable<V> callable)
Some operations require to be performed with theQueue
lock held.static <V,T extends Throwable>
hudson.remoting.Callable<V,T>wrapWithLock(hudson.remoting.Callable<V,T> callable)
Wraps aCallable
with theQueue
lock held.static Runnable
wrapWithLock(Runnable runnable)
static <V> Callable<V>
wrapWithLock(Callable<V> callable)
-
Methods inherited from class hudson.model.ResourceController
canRun, execute, getBlockingActivity, getMissingResource
-
-
-
-
Field Detail
-
XSTREAM
public static final com.thoughtworks.xstream.XStream XSTREAM
ThisXStream
instance is used to persistQueue.Task
s.
-
-
Constructor Detail
-
Queue
public Queue(@NonNull LoadBalancer loadBalancer)
-
-
Method Detail
-
getLoadBalancer
public LoadBalancer getLoadBalancer()
-
setLoadBalancer
public void setLoadBalancer(@NonNull LoadBalancer loadBalancer)
-
getSorter
public QueueSorter getSorter()
-
setSorter
public void setSorter(QueueSorter sorter)
-
load
public void load()
Loads the queue contents that wassaved
.
-
save
public void save()
Persists the queue contents to the disk.
-
clear
public void clear()
Wipes out all the items currently in the queue, as if all of them are cancelled at once.
-
add
@Deprecated public boolean add(AbstractProject p)
Deprecated.as of 1.311 Useschedule(AbstractProject)
-
schedule
@CheckForNull public Queue.WaitingItem schedule(AbstractProject p)
Schedule a new build for this project.- See Also:
schedule(Task, int)
-
add
@Deprecated public boolean add(AbstractProject p, int quietPeriod)
Deprecated.as of 1.311 Useschedule(Task, int)
Schedules a new build with a custom quiet period.Left for backward compatibility with <1.114.
- Since:
- 1.105
-
schedule
@Deprecated public Queue.WaitingItem schedule(Queue.Task p, int quietPeriod, List<Action> actions)
Deprecated.as of 1.521 Useschedule2(Task, int, List)
-
schedule2
@NonNull public ScheduleResult schedule2(Queue.Task p, int quietPeriod, List<Action> actions)
Schedules an execution of a task.- Parameters:
actions
- These actions can be used for associating information scoped to a particular build, to the task being queued. Upon the start of the build, theseAction
s will be automatically added to theRun
object, and hence available to everyone. For the convenience of the caller, this list can contain null, and those will be silently ignored.- Returns:
ScheduleResult.Refused
if Jenkins refused to add this task into the queue (for example because the system is about to shutdown.) Otherwise the task is either merged into existing items in the queue (in which case you getScheduleResult.Existing
instance back), or a new item gets created in the queue (in which case you getScheduleResult.Created
. Note the nature of the queue is that suchQueue.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 atQueue.Item.future
,Queue.Item.getId()
, etc.- Since:
- 1.311
-
add
@Deprecated public boolean add(Queue.Task p, int quietPeriod)
Deprecated.as of 1.311 Useschedule(Task, int)
-
schedule
@CheckForNull public Queue.WaitingItem schedule(Queue.Task p, int quietPeriod)
-
add
@Deprecated public boolean add(Queue.Task p, int quietPeriod, Action... actions)
Deprecated.as of 1.311 Useschedule(Task, int, Action...)
-
schedule
@CheckForNull public Queue.WaitingItem schedule(Queue.Task p, int quietPeriod, Action... actions)
Convenience wrapper method aroundschedule(Task, int, List)
-
schedule2
@NonNull public ScheduleResult schedule2(Queue.Task p, int quietPeriod, Action... actions)
Convenience wrapper method aroundschedule2(Task, int, List)
-
cancel
public boolean cancel(Queue.Task p)
Cancels the item in the queue. If the item is scheduled more than once, cancels the first occurrence.- Returns:
- true if the project was indeed in the queue and was removed. false if this was no-op.
-
cancel
public boolean cancel(Queue.Item item)
-
doCancelItem
public org.kohsuke.stapler.HttpResponse doCancelItem(@QueryParameter long id) throws IOException, javax.servlet.ServletException
Called fromqueue.jelly
andqueue-items.jelly
.- Throws:
IOException
javax.servlet.ServletException
-
isEmpty
public boolean isEmpty()
-
getItems
@Exported(inline=true) public Queue.Item[] getItems()
Gets a snapshot of items in the queue. Generally speaking the array is sorted such that the items that are most likely built sooner are at the end.
-
getDiscoverableItems
@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) @Exported(inline=true) public Queue.StubItem[] getDiscoverableItems()
Returns an array of Item for which it is only visible the name of the task. Generally speaking the array is sorted such that the items that are most likely built sooner are at the end.
-
getApproximateItemsQuickly
@Deprecated public List<Queue.Item> getApproximateItemsQuickly()
Deprecated.UsegetItems()
directly. As of 1.607 the approximation is no longer needed.LikegetItems()
, but returns an approximation that might not be completely up-to-date.At the expense of accuracy, this method does not usually lock
Queue
and therefore is faster in a highly concurrent situation.The list obtained is an accurate snapshot of the queue at some point in the past. The snapshot is updated and normally no more than one second old, but this is a soft commitment that might get violated when the lock on
Queue
is highly contended.This method is primarily added to make UI threads run faster.
- Since:
- 1.483
-
getItem
public Queue.Item getItem(long id)
-
getBuildableItems
public List<Queue.BuildableItem> getBuildableItems(Computer c)
Gets all theQueue.BuildableItem
s that are waiting for an executor in the givenComputer
.
-
getBuildableItems
public List<Queue.BuildableItem> getBuildableItems()
Gets the snapshot of allQueue.BuildableItem
s.
-
getPendingItems
public List<Queue.BuildableItem> getPendingItems()
Gets the snapshot of allQueue.BuildableItem
s.
-
getBlockedItems
protected List<Queue.BlockedItem> getBlockedItems()
Gets the snapshot of allQueue.BlockedItem
s.
-
getLeftItems
public Collection<Queue.LeftItem> getLeftItems()
Returns the snapshot of allQueue.LeftItem
s.- Since:
- 1.519
-
clearLeftItems
public void clearLeftItems()
Immediately clear thegetLeftItems()
cache. Useful for tests which need to verify that no links to a build remain.- Since:
- 1.519
-
getUnblockedItems
public List<Queue.Item> getUnblockedItems()
Gets all items that are in the queue but not blocked- Since:
- 1.402
-
getUnblockedTasks
public Set<Queue.Task> getUnblockedTasks()
Works just likegetUnblockedItems()
but return tasks.- Since:
- 1.402
-
isPending
public boolean isPending(Queue.Task t)
Is the given task currently pending execution?
-
countBuildableItemsFor
public int countBuildableItemsFor(@CheckForNull Label l)
How manyQueue.BuildableItem
s are assigned for the given label?- Parameters:
l
- Label to be checked. If null, any label will be accepted. If you want to countQueue.BuildableItem
s without assigned labels, usestrictCountBuildableItemsFor(hudson.model.Label)
.- Returns:
- Number of
Queue.BuildableItem
s for the specified label.
-
strictCountBuildableItemsFor
public int strictCountBuildableItemsFor(@CheckForNull Label l)
How manyQueue.BuildableItem
s are assigned for the given label?The implementation is quite similar to
countBuildableItemsFor(hudson.model.Label)
, but it has another behavior for null parameters.- Parameters:
l
- Label to be checked. If null, only jobs without assigned labels will be taken into the account.- Returns:
- Number of
Queue.BuildableItem
s for the specified label. - Since:
- 1.615
-
countBuildableItems
public int countBuildableItems()
Counts all theQueue.BuildableItem
s currently in the queue.
-
getItem
public Queue.Item getItem(Queue.Task t)
Gets the information about the queue item for the given project.- Returns:
- null if the project is not in the queue.
-
getItems
public List<Queue.Item> getItems(Queue.Task t)
Gets the information about the queue item for the given project.- Returns:
- empty if the project is not in the queue.
-
contains
public boolean contains(Queue.Task t)
Returns true if this queue contains the said project.
-
scheduleMaintenance
@WithBridgeMethods(void.class) public Future<?> scheduleMaintenance()
Checks the queue and runs anything that can be run.When conditions are changed, this method should be invoked.
This wakes up one
Executor
so that it will maintain a queue.
-
withLock
public static void withLock(Runnable runnable)
Some operations require to be performed with theQueue
lock held. Use one of these methods rather than locking directly on Queue in order to allow for future refactoring.- Parameters:
runnable
- the operation to perform.- Since:
- 1.592
-
withLock
public static <V,T extends Throwable> V withLock(hudson.remoting.Callable<V,T> callable) throws T extends Throwable
Some operations require theQueue
lock held. Use one of these methods rather than locking directly on Queue in order to allow for future refactoring.- Type Parameters:
V
- the type of return valueT
- the type of exception.- Parameters:
callable
- the operation to perform.- Returns:
- the result of the callable.
- Throws:
T
- the exception of the callableT extends Throwable
- Since:
- 1.592
-
withLock
public static <V> V withLock(Callable<V> callable) throws Exception
Some operations require to be performed with theQueue
lock held. Use one of these methods rather than locking directly on Queue in order to allow for future refactoring.- Type Parameters:
V
- the type of return value- Parameters:
callable
- the operation to perform.- Returns:
- the result of the callable.
- Throws:
Exception
- if the callable throws an exception.- Since:
- 1.592
-
tryWithLock
public static boolean tryWithLock(Runnable runnable)
- Parameters:
runnable
- the operation to perform.- Returns:
true
if the lock was available and the operation was performed.- Since:
- 1.618
-
wrapWithLock
public static Runnable wrapWithLock(Runnable runnable)
- Parameters:
runnable
- the operation to wrap.- Since:
- 1.618
-
wrapWithLock
public static <V,T extends Throwable> hudson.remoting.Callable<V,T> wrapWithLock(hudson.remoting.Callable<V,T> callable)
Wraps aCallable
with theQueue
lock held.- Parameters:
callable
- the operation to wrap.- Since:
- 1.618
-
wrapWithLock
public static <V> Callable<V> wrapWithLock(Callable<V> callable)
- Parameters:
callable
- the operation to wrap.- Since:
- 1.618
-
_await
protected void _await() throws InterruptedException
- Overrides:
_await
in classResourceController
- Throws:
InterruptedException
-
_signalAll
protected void _signalAll()
- Overrides:
_signalAll
in classResourceController
-
_withLock
protected void _withLock(Runnable runnable)
Some operations require to be performed with theQueue
lock held. Use one of these methods rather than locking directly on Queue in order to allow for future refactoring.- Overrides:
_withLock
in classResourceController
- Parameters:
runnable
- the operation to perform.- Since:
- 1.592
-
_tryWithLock
protected boolean _tryWithLock(Runnable runnable)
- Parameters:
runnable
- the operation to perform.- Returns:
true
if the lock was available and the operation was performed.- Since:
- 1.618
-
_withLock
protected <V,T extends Throwable> V _withLock(hudson.remoting.Callable<V,T> callable) throws T extends Throwable
Some operations require to be performed with theQueue
lock held. Use one of these methods rather than locking directly on Queue in order to allow for future refactoring.- Overrides:
_withLock
in classResourceController
- Type Parameters:
V
- the type of return valueT
- the type of exception.- Parameters:
callable
- the operation to perform.- Returns:
- the result of the callable.
- Throws:
T
- the exception of the callableT extends Throwable
- Since:
- 1.592
-
_withLock
protected <V> V _withLock(Callable<V> callable) throws Exception
Some operations require to be performed with theQueue
lock held. Use one of these methods rather than locking directly on Queue in order to allow for future refactoring.- Overrides:
_withLock
in classResourceController
- Type Parameters:
V
- the type of return value- Parameters:
callable
- the operation to perform.- Returns:
- the result of the callable.
- Throws:
Exception
- if the callable throws an exception.- Since:
- 1.592
-
maintain
public void maintain()
Queue maintenance.Move projects between
waitingList
,blockedProjects
,buildables
, andpendings
appropriately.Jenkins internally invokes this method by itself whenever there's a change that can affect the scheduling (such as new node becoming online, # of executors change, a task completes execution, etc.), and it also gets invoked periodically (see
Queue.MaintainTask
.)
-
ifBlockedByHudsonShutdown
@Deprecated public static boolean ifBlockedByHudsonShutdown(Queue.Task task)
Deprecated.UseisBlockedByShutdown(hudson.model.Queue.Task)
instead.
-
isBlockedByShutdown
public static boolean isBlockedByShutdown(Queue.Task task)
Checks whether a task should not be scheduled becauseJenkins.isQuietingDown()
.- Parameters:
task
- some queue task- Returns:
- true if
Jenkins.isQuietingDown()
unless this is aQueue.NonBlockingTask
- Since:
- 1.598
-
getApi
public Api getApi()
-
getInstance
@CLIResolver public static Queue getInstance()
-
init
@Initializer(after=JOB_CONFIG_ADAPTED) public static void init(Jenkins h)
Restores the queue content during the start up.
-
-