Miroslav
Miroslav

Reputation: 21

How to assign "Multi-Project Throttle Category" in a script

I am using Jenkins with Throttle Concurrent Builds plugin to ensure exclusive access to USB device in test jobs. I use parametrized jobs, with a parameter named MODE. For some MODE values the test uses USB device and for the other MODE values the test does not use the USB device. I am writing a Groovy script for running the tests. Is it possible to assign "Multi-Project Throttle Category" in a script, so that I could assign it based on the value of my MODE parameter? Thanks

Upvotes: 2

Views: 1373

Answers (2)

Christian
Christian

Reputation: 14111

Modifying the categories in-place did not work for me. Instead I had to create a new ThrottleJobProperty and add it:

ThrottleJobProperty jobProperty = item.getProperty(ThrottleJobProperty)

println("ThrottleJobProperty of " + item.name + " has categories: " + jobProperty?.categories)
String category = "long-running"
if (!jobProperty?.categories?.contains(category)) {
    if (jobProperty != null) item.removeProperty(jobProperty)

    List<String> categories = jobProperty != null ?
            new ArrayList<String>(jobProperty.categories) :
            new ArrayList<String>()
    categories.add(category)
    jobProperty = new ThrottleJobProperty(
            /*maxConcurrentPerNode:*/ 0,
            /*maxConcurrentTotal:*/ 0,
            /*categories:*/ categories,
            /*throttleEnabled:*/ true,
            /*throttleOption:*/ 'category',
            /*limitOneJobWithMatchingParams:*/ false,
            /*paramsToUseForLimit:*/ '',
            /*matrixOptions:*/ null
    )
    item.addProperty(jobProperty)
    println("Assigning ThrottleJobProperty.categories for " + item.name + ": " + jobProperty?.categories)
    item.save()
}

Upvotes: 1

Miroslav
Miroslav

Reputation: 21

I found this working

tjp = myjob.getProperty(hudson.plugins.throttleconcurrents.ThrottleJobProperty)

// see what we got
if(tjp != null) {
    println("--- Throttle concurrents for " + myjob.name + " ---")

    try {
        println "Got this: " + tjp.categories + " items " + tjp.categories.size
    } catch(Exception e) {
        println(tjp.categories)
    }
}

// change the first one
tjp.categories[0] = "myCategory"

// update job properties
myjob.addProperty(tjp)

Upvotes: 0

Related Questions