← Back to team overview

python-jenkins-developers team mailing list archive

[Merge] lp:~hashar/python-jenkins/fast-job-existence-check into lp:python-jenkins

 

Antoine "hashar" Musso has proposed merging lp:~hashar/python-jenkins/fast-job-existence-check into lp:python-jenkins.

Requested reviews:
  Python Jenkins Developers (python-jenkins-developers)

For more details, see:
https://code.launchpad.net/~hashar/python-jenkins/fast-job-existence-check/+merge/171303

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.



-- 
https://code.launchpad.net/~hashar/python-jenkins/fast-job-existence-check/+merge/171303
Your team Python Jenkins Developers is requested to review the proposed merge of lp:~hashar/python-jenkins/fast-job-existence-check into lp:python-jenkins.
=== 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 @@
 
 INFO = 'api/json'
 JOB_INFO = 'job/%(name)s/api/json?depth=0'
+JOB_NAME = 'job/%(name)s/api/json?tree=name'
 Q_INFO = 'queue/api/json?depth=0'
 CANCEL_QUEUE = 'queue/item/%(number)s/cancelQueue'
 CREATE_JOB = 'createItem?name=%(name)s'  # also post config.xml
@@ -172,6 +173,25 @@
             raise JenkinsException(
                 "Could not parse JSON info for job[%s]" % name)
 
+    def get_job_name(self, name):
+        '''
+        Return the name of a job using the API. That is roughly an identity
+        method which can be used to quickly verify a job exist or is accessible
+        without causing too much stress on the server side.
+
+        :param name: Job name, ``str``
+        :returns: Name of job or None
+        '''
+        response = self.jenkins_open(
+            urllib2.Request(self.server + JOB_NAME % locals()))
+        if response:
+            if json.loads(response)['name'] != name:
+                raise JenkinsException(
+                    'Jenkins returned an unexpected job name')
+            return json.loads(response)['name']
+        else:
+            return None
+
     def debug_job_info(self, job_name):
         '''
         Print out job info in more readable format
@@ -345,11 +365,8 @@
         :param name: Name of Jenkins job, ``str``
         :returns: ``True`` if Jenkins job exists
         '''
-        try:
-            self.get_job_info(name)
+        if self.get_job_name(name) == name:
             return True
-        except JenkinsException:
-            return False
 
     def create_job(self, name, config_xml):
         '''


Follow ups