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
1=== modified file 'jenkins/__init__.py'
2--- jenkins/__init__.py 2013-04-13 21:59:59 +0000
3+++ jenkins/__init__.py 2013-06-25 13:45:38 +0000
4@@ -59,6 +59,7 @@
5
6 INFO = 'api/json'
7 JOB_INFO = 'job/%(name)s/api/json?depth=0'
8+JOB_NAME = 'job/%(name)s/api/json?tree=name'
9 Q_INFO = 'queue/api/json?depth=0'
10 CANCEL_QUEUE = 'queue/item/%(number)s/cancelQueue'
11 CREATE_JOB = 'createItem?name=%(name)s' # also post config.xml
12@@ -172,6 +173,25 @@
13 raise JenkinsException(
14 "Could not parse JSON info for job[%s]" % name)
15
16+ def get_job_name(self, name):
17+ '''
18+ Return the name of a job using the API. That is roughly an identity
19+ method which can be used to quickly verify a job exist or is accessible
20+ without causing too much stress on the server side.
21+
22+ :param name: Job name, ``str``
23+ :returns: Name of job or None
24+ '''
25+ response = self.jenkins_open(
26+ urllib2.Request(self.server + JOB_NAME % locals()))
27+ if response:
28+ if json.loads(response)['name'] != name:
29+ raise JenkinsException(
30+ 'Jenkins returned an unexpected job name')
31+ return json.loads(response)['name']
32+ else:
33+ return None
34+
35 def debug_job_info(self, job_name):
36 '''
37 Print out job info in more readable format
38@@ -345,11 +365,8 @@
39 :param name: Name of Jenkins job, ``str``
40 :returns: ``True`` if Jenkins job exists
41 '''
42- try:
43- self.get_job_info(name)
44+ if self.get_job_name(name) == name:
45 return True
46- except JenkinsException:
47- return False
48
49 def create_job(self, name, config_xml):
50 '''

Subscribers

People subscribed via source and target branches