[workflow] creating multiple threads programatically hangs

36 views
Skip to first unread message

euphxenos

unread,
Oct 19, 2015, 8:05:17 PM10/19/15
to Jenkins Users
I'm new to Groovy and the Workflow plugin, so perhaps this is something obvious.  I'm trying to create a set of threads to run in parallel, each one calling a parameterized job.  If I do this, it works:

def paramnodes = [:]
paramnodes['alpha'] = {
    node() {
        build job: 'parameterized-build-job'
    }
}
paramnodes['beta'] = {
    node() {
        build job: 'parameterized-build-job'
    }
}
parallel paramnodes


But if I do something like this, it hangs:

def nodes = [:]
nodes['alpha'] = [
    'OPT': 'alpha',
]
nodes['beta'] = [
    'OPT': 'beta',
]
@NonCPS
def paramNodes(nodemap) {
    def ret = [:]
    for (key in nodemap.keySet()) {
        ret[key] = {
            node() {
                build job: 'parameterized-build-job'
            }
        }
    }
    return ret
}
def paramnodes = paramNodes(nodes)
parallel paramnodes


The loop has been buried inside a function annotated with @NonCPS to avoid the problem with for loops on maps not being serializable.

What am I doing wrong?  Is there another (supported) way to loop across a Groovy map to accomplish the same thing?


thanks,
--Andrew

euphxenos

unread,
Oct 21, 2015, 4:44:17 PM10/21/15
to Jenkins Users
To answer my own question, this works for iterating across a map in a groovy workflow:


def nodes = [:]
nodes['alpha'] = [
    'OPT': 'alpha',
]
nodes['beta'] = [
    'OPT': 'beta',
]
def nodelist = nodes.keySet().toList()
def paramnodes = [:]
def nodename = null
for (int i = 0; i < nodelist.size(); i++) {
    nodename = nodelist[i]
    paramnodes[nodename] = {

        node() {
            build job: 'parameterized-build-job'
        }
    }
}
parallel paramnodes

Where I'm going with this is that I'm trying to get it to invoke a single parameterized job multiple times, with different parameters (I've left that part of the workflow out, here -- at the moment it just tells me I called that job multiple times and runs it with one of those sets of parameters, so it's not particularly useful).  If you're doing something else and need to iterate across a map in a workflow, perhaps this part of it will be useful to you.


--Andrew

Chris Hillery

unread,
Nov 2, 2015, 5:35:34 AM11/2/15
to Jenkins Users
I'm dealing with similar issues to you, and don't yet have a good solution. However, a quick note: this -

On Wednesday, October 21, 2015 at 1:44:17 PM UTC-7, euphxenos wrote:
        node() {
            build job: 'parameterized-build-job'
        }

is unnecessary. You don't need to allocate a node() to call build in your Workflow, and it won't accomplish anything if you do so. In particular it does not mean "run this job on this node". The job you call will still pick a new executor on a new node just as if you'd clicked "Build" in the web interface.

Ceej
aka Chris Hillery
Reply all
Reply to author
Forward
0 new messages