Add/remove from, and change order of queue from build trigger

I would like to be able to make adjustments to the build queue, which can include adding to it, removing from it, and changing the order of things that are in it, from within a build trigger.

The specific order of operations in the build trigger might be something like:

1) Add new tasks to the queue (BuildPromotion.addToQueue)
2) Get all queued tasks (BuildQueue.getItems)
3) Re-order within queue (BuildQueue.applyOrdering)
4) Remove existing tasks from the queue (BuildQueue.removeItems)

Basically, what I am trying to attempt is to have a trigger actually replace tasks in the queue with tasks that are slightly different, (and have these newly created tasks queued in the same place as the old tasks).

Things appear to be working, but I have a couple of questions about doing this:

1) Is it actually safe to directly change the queue from within a build trigger?
2) Each individual operation seems to be atomic, but I have a hunch that the set of operations is not atomic.  i.e. Within the context of a build trigger, is it possible (if improbable) for tasks to be added or removed from the queue in between performing calls to BuildQueue, BuildPromotion.addToQueue, or SQueuedBuild.getOrderNumber?  If so, is there a way to do something like this atomically (like locking the queue temporarily)?

This post:

seemed relevant, though not the exact same situation as what I am attempting to do.


1 comment
Comment actions Permalink

Sorry for delay. Answering to your questions if they are still relevant:

1) Build queue is thread safe and can be modified from a build trigger too; except it's unusual when trigger performs some replacements there, usually trigger only adds builds to the queue and if some optimization needs to be done to builds, this is performed by some background thread. 

2) Individual operations are atomic. But there are no operations for batch changes, like add / remove and apply order at the same time. So, yes, new builds can appear in the build queue while trigger works, it is not possible to take an exclusinve lock for the queue, and we'd not recommend doing so anyway. What you can do is to require that builds are processed by some thread before they can start. You can use StartBuildPrecondition to prevent builds from starting untill they are processed.


Please sign in to leave a comment.