Class Queue
- All Implemented Interfaces:
- Saveable
 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 into Queue.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 the Queue.Executable has been instantiated it
 is safe to move the pending item back to buildable. Once the Queue.Executable has been
 instantiated the only option is to let the Queue.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:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionfinal classQueue.Itemin theblockedProjectsstage.static final classQueue.Itemin thebuildablesstage.static interfaceRepresents the real meat of the computation run byExecutor.static interfaceMarksQueue.Tasks that do not consumeExecutor.static classItem in a queue.static classData structure created for each idleExecutor.static final classQueue.Itemin theleftItemsstage.static interfaceMarksQueue.Tasks that are not affected by the Jenkins.isQuietingDown() quieting down}, because these tasks keep other tasks executing.static classCommon part betweenQueue.BlockedItemandQueue.BuildableItem.static interfaceAn optional interface for actions on Queue.Item.static classExtension point for deciding if particular job should be scheduled or not.static final classSchedulesave()call for near future once items change.static final classSimple queue state persistence object.static classA Stub class forQueue.Itemwhich exposes only the name of the Task to be displayed when the user has DISCOVERY permissions only.static classA Stub class forQueue.Taskwhich exposes only the name of the Task to be displayed when the user has DISCOVERY permissions only.static interfaceTask whose execution is controlled by the queue.static interfaceMarksQueue.Tasks that are not persisted.static final classQueue.Itemin thewaitingListstage.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final com.thoughtworks.xstream.XStreamThisXStreaminstance is used to persistQueue.Tasks.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprotected void_await()protected <V,T extends Throwable> 
 V_callWithLock(ThrowingCallable<V, T> callable) Some operations require to be performed with theQueuelock held.protected <T extends Throwable>
 void_runWithLock(ThrowingRunnable<T> runnable) Some operations require to be performed with theQueuelock held.protected voidprotected boolean_tryWithLock(Runnable runnable) protected boolean_tryWithLock(Runnable runnable, Duration timeout) protected <V,T extends Throwable> 
 V_withLock(hudson.remoting.Callable<V, T> callable) Some operations require to be performed with theQueuelock held.protected voidSome operations require to be performed with theQueuelock held.protected <V> VSome operations require to be performed with theQueuelock held.booleanDeprecated.booleanadd(AbstractProject p, int quietPeriod) Deprecated.as of 1.311 Useschedule(Task, int)booleanadd(Queue.Task p, int quietPeriod) Deprecated.as of 1.311 Useschedule(Task, int)booleanadd(Queue.Task p, int quietPeriod, Action... actions) Deprecated.as of 1.311 Useschedule(Task, int, Action...)static <V,T extends Throwable> 
 VcallWithLock(ThrowingCallable<V, T> callable) Some operations require theQueuelock held.booleancancel(Queue.Item item) booleancancel(Queue.Task p) Cancels the item in the queue.voidclear()Wipes out all the items currently in the queue, as if all of them are cancelled at once.voidImmediately clear thegetLeftItems()cache.booleanReturns true if this queue contains the said project.intCounts all theQueue.BuildableItems currently in the queue.intHow manyQueue.BuildableItems are assigned for the given label?org.kohsuke.stapler.HttpResponsedoCancelItem(long id) Called fromqueue.jellyandqueue-items.jelly.getApi()Deprecated.UsegetItems()directly.protected List<Queue.BlockedItem> Gets the snapshot of allQueue.BlockedItems.Gets the snapshot of allQueue.BuildableItems.Gets all theQueue.BuildableItems that are waiting for an executor in the givenComputer.Returns an array of Item for which it is only visible the name of the task.static QueuegetItem(long id) Gets the information about the queue item for the given project.getItems()Gets a snapshot of items in the queue.Gets the information about the queue item for the given project.Returns the snapshot of allQueue.LeftItems.Gets the snapshot of allQueue.BuildableItems.Gets all items that are in the queue but not blockedWorks just likegetUnblockedItems()but return tasks.static booleanDeprecated.UseisBlockedByShutdown(hudson.model.Queue.Task)instead.static voidRestores the queue content during the start up.static booleanChecks whether a task should not be scheduled becauseJenkins.isQuietingDown().booleanisEmpty()booleanIs the given task currently pending execution?voidload()Loads the queue contents that wassaved.voidmaintain()Queue maintenance.static <T extends Throwable>
 voidrunWithLock(ThrowingRunnable<T> runnable) Some operations require to be performed with theQueuelock held.voidsave()Persists the queue contents to the disk.Schedule a new build for this project.schedule(Queue.Task p, int quietPeriod) schedule(Queue.Task p, int quietPeriod, Action... actions) Convenience wrapper method aroundschedule(Task, int, List)schedule(Queue.Task p, int quietPeriod, List<Action> actions) Deprecated.as of 1.521 Useschedule2(Task, int, List)schedule2(Queue.Task p, int quietPeriod, Action... actions) Convenience wrapper method aroundschedule2(Task, int, List)schedule2(Queue.Task p, int quietPeriod, List<Action> actions) Schedules an execution of a task.Future<?> Checks the queue and runs anything that can be run.voidsetLoadBalancer(LoadBalancer loadBalancer) voidsetSorter(QueueSorter sorter) intHow manyQueue.BuildableItems are assigned for the given label?static booleantryWithLock(Runnable runnable) static booleantryWithLock(Runnable runnable, Duration timeout) static <V,T extends Throwable> 
 VwithLock(hudson.remoting.Callable<V, T> callable) static voidstatic <V> Vstatic <V,T extends Throwable> 
 hudson.remoting.Callable<V, T> wrapWithLock(hudson.remoting.Callable<V, T> callable) Wraps aCallablewith theQueuelock held.static RunnablewrapWithLock(Runnable runnable) static <V> Callable<V> wrapWithLock(Callable<V> callable) Methods inherited from class hudson.model.ResourceControllercanRun, execute, getBlockingActivity, getMissingResource
- 
Field Details- 
XSTREAMpublic static final com.thoughtworks.xstream.XStream XSTREAMThisXStreaminstance is used to persistQueue.Tasks.
 
- 
- 
Constructor Details- 
Queue
 
- 
- 
Method Details- 
getLoadBalancer
- 
setLoadBalancer
- 
getSorter
- 
setSorter
- 
loadpublic void load()Loads the queue contents that wassaved.
- 
savepublic void save()Persists the queue contents to the disk.
- 
clearpublic void clear()Wipes out all the items currently in the queue, as if all of them are cancelled at once.
- 
addDeprecated.as of 1.311 Useschedule(AbstractProject)
- 
scheduleSchedule a new build for this project.- See Also:
 
- 
addDeprecated.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
 
- 
scheduleDeprecated.as of 1.521 Useschedule2(Task, int, List)
- 
schedule2Schedules 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, these- Actions will be automatically added to the- Runobject, and hence available to everyone. For the convenience of the caller, this list can contain null, and those will be silently ignored.
- Returns:
- ScheduleResult.Refusedif 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 get- ScheduleResult.Existinginstance back), or a new item gets created in the queue (in which case you get- ScheduleResult.Created. Note the nature of the queue is that such- Queue.Itemonly 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- Queue.Item.future,- Queue.Item.getId(), etc.
- Since:
- 1.311
 
- 
addDeprecated.as of 1.311 Useschedule(Task, int)
- 
schedule
- 
addDeprecated.as of 1.311 Useschedule(Task, int, Action...)
- 
scheduleConvenience wrapper method aroundschedule(Task, int, List)
- 
schedule2Convenience wrapper method aroundschedule2(Task, int, List)
- 
cancelCancels 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
- 
doCancelItempublic org.kohsuke.stapler.HttpResponse doCancelItem(@QueryParameter long id) throws IOException, jakarta.servlet.ServletException Called fromqueue.jellyandqueue-items.jelly.- Throws:
- IOException
- jakarta.servlet.ServletException
 
- 
isEmptypublic boolean isEmpty()
- 
getItemsGets 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.
- 
getApproximateItemsQuicklyDeprecated.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 Queueand 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 Queueis highly contended.This method is primarily added to make UI threads run faster. - Since:
- 1.483
 
- 
getItem
- 
getBuildableItemsGets all theQueue.BuildableItems that are waiting for an executor in the givenComputer.
- 
getBuildableItemsGets the snapshot of allQueue.BuildableItems.
- 
getPendingItemsGets the snapshot of allQueue.BuildableItems.
- 
getBlockedItemsGets the snapshot of allQueue.BlockedItems.
- 
getLeftItemsReturns the snapshot of allQueue.LeftItems.- Since:
- 1.519
 
- 
clearLeftItemspublic void clearLeftItems()Immediately clear thegetLeftItems()cache. Useful for tests which need to verify that no links to a build remain.- Since:
- 1.519
 
- 
getUnblockedItemsGets all items that are in the queue but not blocked- Since:
- 1.402
 
- 
getUnblockedTasksWorks just likegetUnblockedItems()but return tasks.- Since:
- 1.402
 
- 
isPendingIs the given task currently pending execution?
- 
countBuildableItemsForHow manyQueue.BuildableItems are assigned for the given label?- Parameters:
- l- Label to be checked. If null, any label will be accepted. If you want to count- Queue.BuildableItems without assigned labels, use- strictCountBuildableItemsFor(hudson.model.Label).
- Returns:
- Number of Queue.BuildableItems for the specified label.
 
- 
strictCountBuildableItemsForHow manyQueue.BuildableItems 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.BuildableItems for the specified label.
- Since:
- 1.615
 
- 
countBuildableItemspublic int countBuildableItems()Counts all theQueue.BuildableItems currently in the queue.
- 
getItemGets the information about the queue item for the given project.- Returns:
- null if the project is not in the queue.
 
- 
getItemsGets the information about the queue item for the given project.- Returns:
- empty if the project is not in the queue.
 
- 
containsReturns true if this queue contains the said project.
- 
scheduleMaintenanceChecks the queue and runs anything that can be run.When conditions are changed, this method should be invoked. This wakes up one Executorso that it will maintain a queue.
- 
runWithLockSome operations require to be performed with theQueuelock 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.
- Throws:
- T- if the runnable throws an exception.
- Since:
- TODO
 
- 
withLock- Since:
- 1.592
 
- 
callWithLockSome operations require theQueuelock 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
- T- the type of exception.
- Parameters:
- callable- the operation to perform.
- Returns:
- the result of the callable.
- Throws:
- T- the exception of the callable
- Since:
- TODO
 
- 
withLock- Throws:
- T
- Since:
- 1.592
 
- 
withLock- Throws:
- Exception
- Since:
- 1.592
 
- 
tryWithLock- Parameters:
- runnable- the operation to perform.
- Returns:
- trueif the lock was available and the operation was performed.
- Since:
- 1.618
 
- 
tryWithLock- Parameters:
- runnable- the operation to perform.
- Returns:
- trueif the lock was acquired within the timeout and the operation was performed.
- Throws:
- InterruptedException
- Since:
- 2.529
 
- 
wrapWithLock- Parameters:
- runnable- the operation to wrap.
- Since:
- 1.618
 
- 
wrapWithLockpublic static <V,T extends Throwable> hudson.remoting.Callable<V,T> wrapWithLock(hudson.remoting.Callable<V, T> callable) Wraps aCallablewith theQueuelock held.- Parameters:
- callable- the operation to wrap.
- Since:
- 1.618
 
- 
wrapWithLock- Parameters:
- callable- the operation to wrap.
- Since:
- 1.618
 
- 
_await- Overrides:
- _awaitin class- ResourceController
- Throws:
- InterruptedException
 
- 
_signalAllprotected void _signalAll()- Overrides:
- _signalAllin class- ResourceController
 
- 
_runWithLockSome operations require to be performed with theQueuelock held. Use one of these methods rather than locking directly on Queue in order to allow for future refactoring.- Overrides:
- _runWithLockin class- ResourceController
- Type Parameters:
- T- the type of exception.
- Parameters:
- runnable- the operation to perform.
- Throws:
- T- the exception of the runnable
- Since:
- TODO
 
- 
_withLockSome operations require to be performed with theQueuelock held. Use one of these methods rather than locking directly on Queue in order to allow for future refactoring.- Overrides:
- _withLockin class- ResourceController
- Parameters:
- runnable- the operation to perform.
- Since:
- 1.592
 
- 
_tryWithLock- Parameters:
- runnable- the operation to perform.
- Returns:
- trueif the lock was available and the operation was performed.
- Since:
- 1.618
 
- 
_tryWithLock- Parameters:
- runnable- the operation to perform.
- Returns:
- trueif the lock was acquired within the timeout and the operation was performed.
- Throws:
- InterruptedException
- Since:
- 2.529
 
- 
_callWithLockSome operations require to be performed with theQueuelock held. Use one of these methods rather than locking directly on Queue in order to allow for future refactoring.- Overrides:
- _callWithLockin class- ResourceController
- Type Parameters:
- V- the type of return value
- T- the type of exception.
- Parameters:
- callable- the operation to perform.
- Returns:
- the result of the callable.
- Throws:
- T- the exception of the callable
- Since:
- TODO
 
- 
_withLockSome operations require to be performed with theQueuelock held. Use one of these methods rather than locking directly on Queue in order to allow for future refactoring.- Overrides:
- _withLockin class- ResourceController
- Type Parameters:
- V- the type of return value
- T- the type of exception.
- Parameters:
- callable- the operation to perform.
- Returns:
- the result of the callable.
- Throws:
- T- the exception of the callable
- Since:
- 1.592
 
- 
_withLockSome operations require to be performed with theQueuelock held. Use one of these methods rather than locking directly on Queue in order to allow for future refactoring.- Overrides:
- _withLockin class- ResourceController
- 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
 
- 
maintainpublic void maintain()Queue maintenance.Move projects between waitingList,blockedProjects,buildables, andpendingsappropriately.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.)
- 
ifBlockedByHudsonShutdownDeprecated.UseisBlockedByShutdown(hudson.model.Queue.Task)instead.
- 
isBlockedByShutdownChecks 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
- 
getInstance
- 
initRestores the queue content during the start up.
 
- 
schedule(AbstractProject)