Merge lp:~hashar/python-jenkins/fast-job-existence-check into lp:~python-jenkins-developers/python-jenkins/trunk

Proposed by Antoine "hashar" Musso
Status: Merged
Merged at revision: 19
Proposed branch: lp:~hashar/python-jenkins/fast-job-existence-check
Merge into: lp:~python-jenkins-developers/python-jenkins/trunk
Diff against target: 50 lines (+21/-4)
1 file modified
jenkins/__init__.py (+21/-4)
To merge this branch: bzr merge lp:~hashar/python-jenkins/fast-job-existence-check
Reviewer Review Type Date Requested Status
Python Jenkins Developers Pending
Review via email: mp+171303@code.launchpad.net

Description of the change

Branch contains a single patch that aims to speed up job existence test by lowering the amount of work that need to be done on the server side.

Previously job_exists() was calling get_job_info() which does an API call to 'job/%(name)s/api/json?depth=0'. That forces the Jenkins server to load all the build history which is potentially a long operation with a cold cache and definitely unneeded when we just want to verify whether a job exist.

Another step would be to update the other methods to rely on job_exists() instead of get_job_info(). Maybe with a new method like requireExistingJob() that will throw an exception whenever job_exists() returns None.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'jenkins/__init__.py'
--- jenkins/__init__.py 2013-04-13 21:59:59 +0000
+++ jenkins/__init__.py 2013-06-25 13:45:38 +0000
@@ -59,6 +59,7 @@
5959
60INFO = 'api/json'60INFO = 'api/json'
61JOB_INFO = 'job/%(name)s/api/json?depth=0'61JOB_INFO = 'job/%(name)s/api/json?depth=0'
62JOB_NAME = 'job/%(name)s/api/json?tree=name'
62Q_INFO = 'queue/api/json?depth=0'63Q_INFO = 'queue/api/json?depth=0'
63CANCEL_QUEUE = 'queue/item/%(number)s/cancelQueue'64CANCEL_QUEUE = 'queue/item/%(number)s/cancelQueue'
64CREATE_JOB = 'createItem?name=%(name)s' # also post config.xml65CREATE_JOB = 'createItem?name=%(name)s' # also post config.xml
@@ -172,6 +173,25 @@
172 raise JenkinsException(173 raise JenkinsException(
173 "Could not parse JSON info for job[%s]" % name)174 "Could not parse JSON info for job[%s]" % name)
174175
176 def get_job_name(self, name):
177 '''
178 Return the name of a job using the API. That is roughly an identity
179 method which can be used to quickly verify a job exist or is accessible
180 without causing too much stress on the server side.
181
182 :param name: Job name, ``str``
183 :returns: Name of job or None
184 '''
185 response = self.jenkins_open(
186 urllib2.Request(self.server + JOB_NAME % locals()))
187 if response:
188 if json.loads(response)['name'] != name:
189 raise JenkinsException(
190 'Jenkins returned an unexpected job name')
191 return json.loads(response)['name']
192 else:
193 return None
194
175 def debug_job_info(self, job_name):195 def debug_job_info(self, job_name):
176 '''196 '''
177 Print out job info in more readable format197 Print out job info in more readable format
@@ -345,11 +365,8 @@
345 :param name: Name of Jenkins job, ``str``365 :param name: Name of Jenkins job, ``str``
346 :returns: ``True`` if Jenkins job exists366 :returns: ``True`` if Jenkins job exists
347 '''367 '''
348 try:368 if self.get_job_name(name) == name:
349 self.get_job_info(name)
350 return True369 return True
351 except JenkinsException:
352 return False
353370
354 def create_job(self, name, config_xml):371 def create_job(self, name, config_xml):
355 '''372 '''

Subscribers

People subscribed via source and target branches