WorkerApi - Limit workers programmatically

I am using the worker api. The problem is that when the workers us up all my cpu’s my development box goes very slow and it pretty much unusable. The parameter --max-works works great but I wanted to be able to set this programmatically.

Questions:

  • Can I set the max workers programmatically inside my task?
  • Is there a way to figure out when a worker becomes free?

I was thinking about putting in logic to control how many times I call workerExecutor.submit but I was not sure how to tell if there are any workers free. This also seemed messy.

Snippet
@TaskAction
void runMyCommand() {

    workerExecutor.submit(MyRunnable.class) { WorkerConfiguration config ->
        // Use the minimum level of isolation
        config.isolationMode = IsolationMode.NONE

        config.params myParam
        
    }
}

}