Python Jenkins API does not allow to queue several jobs at once. Is there a way to work around the restriction?
Question: There is a parametrized job in my Jenkins server. I would like to initiate
several builds of the job with different parameters using Python Jenkins API.
This is completely legitimate in Jenkins. If I call 'invoke' method of the
same job instance several times, the API starts only the first build and
rejects to queue another builds because it considers that the job is already
in the queue.

I am using Python Jenkins API version 0.2.14. This is the latest version of
the API on the current moment. I have taken a look into the Jenkins API source
code and it really restricts to invoke a new build until there is another job
in the queue (job.py: lines 104,105):

    
    
    if self.is_queued():
        raise WillNotBuild('%s is already queued' % repr(self))
    

Is there a way to get over the restriction? I have found some way (see the
sample below), but I am not sure it is the best way to do it. Maybe there are
different solutions. I would like to know about these solutions if there are
ones.

In my solution I have created a subclass `ParameterizedJob` of the original
`jenkinsapi.job.Job` class and I have overridden four methods: `__init__`,
`invoke`, `is_queued`, `is_queued_or_running`. I have instantiated a job
object from my class instead of calling get_job method from the Jenkins
instance. This approach works fine for me.

    
    
    import jenkinsapi
    from jenkinsapi.job import Job
    
    class ParametrizedJob(Job):
        def __init__(self, jobname, jenkins_instance):
            self._allowQueuingMultipleBuilds = False
            jobExists = False
            for info in jenkins_instance._data["jobs"]:
                if info["name"] == jobname:
                    Job.__init__(self, info["url"], info["name"], jenkins_obj=jenkins_instance)
                    jobExists = True
                    break
            if not jobExists:
                raise UnknownJob(jobname)
    
        def invoke(self, securitytoken=None, block=False, skip_if_running=False, invoke_pre_check_delay=3, invoke_block_delay=15, build_params=None, cause=None):
            self._allowQueuingMultipleBuilds = True
            try:
                Job.invoke(self, securitytoken, block, skip_if_running, invoke_pre_check_delay, invoke_block_delay, build_params, cause)
            except:
                raise
            finally:
                self._allowQueuingMultipleBuilds = False
    
        def is_queued_or_running(self):
            if not self._allowQueuingMultipleBuilds:
                return Job.is_queued_or_running(self)
            else:
                return False
    
        def is_queued(self):
            if not self._allowQueuingMultipleBuilds:
                return Job.is_queued(self)
            else:
                return False
    
    def startJenkinsJob(jenkinsInstance, jobName, parameters):
        #targetJob = jenkinsInstance.get_job(jobName)
        targetJob = ParametrizedJob(jobName, jenkinsInstance)
        targetJob.invoke(securitytoken='TOKEN', build_params=parameters, block=True)
        return
    


Answer: You can not schedule the same job several times through the UI either.
Exception is, if you have a parametrized job, and both starts have different
parameters.

