6

I'm trying to set the number of executors in Jenkins using Groovy. I've found a method hudson.model.Hudson.instance.setNumExecutors(int) but it doesn't seem to really work. The problem is that the modified value appears in the configuration panel after I run the Groovy script, but I have to click 'Save' in that panel to really have it changed.

Here's the code (executed as a build step with Jenkins Groovy plugin):

import hudson.model.*

// Initial number of executors is 1, let's increase the number of executors to 2
Hudson hudson = Hudson.getInstance()
hudson.setNumExecutors(2)
hudson.save()

def job = hudson.getJob("some_other_job")
def future = job.scheduleBuild2(0, new Cause.UpstreamCause(build))
subBuild = future.get()

// Set the number of executors back to 1
hudson.setNumExecutors(1)
hudson.save()

2 Answers 2

9

The solution is to call hudson.setNodes(hudson.getNodes()) after calling setNumExecutors().

0

If everything is working as you expect except you have to press the save button manually to make the changes stick then it sounds like you didn't call save on your Jenkins instance after you made your changes.

4
  • That's what I thought initially, but I've tried calling save() and it doesn't change anything- I still see the same behavior.
    – s4nk
    Feb 25, 2014 at 9:32
  • Added the code. As you can see initially there is 1 executor, then I try to add a new executor to schedule a job on it. Since the new executor is not being added, the job scheduled in the script cannot start, so everything is blocked.
    – s4nk
    Feb 25, 2014 at 13:54
  • Calling setNumExecutors like that seems too easy. I suspect something else needs to happen, like creating a new executor on a specific computer. Also, this is a long shot, but you might try using jenkins.model.Jenkins.getInstance() instead of Hudson. Feb 25, 2014 at 17:31
  • I've tried using jenkins.model.Jenkins.getInstance() already- no effect unfortunately. I've checked setNumExecutors() implementation and it actually calls save() internally after setting the new number of executors. Another possibility is that it's a bug...
    – s4nk
    Feb 25, 2014 at 17:59

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.