Abstract loading job file from disk
authorGuido Trotter <ultrotter@google.com>
Tue, 15 Jun 2010 10:17:24 +0000 (11:17 +0100)
committerGuido Trotter <ultrotter@google.com>
Thu, 17 Jun 2010 09:53:38 +0000 (10:53 +0100)
Move the work from _LoadJobUnlocked to _LoadJobFileFromDisk, which can
then be used in other contexts as well. Also, if we fail to deserialize
the job, archive it as well (before we archived it only if we failed to
create the related object, but kept it there if deserialization failed.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>

lib/jqueue.py

index 2554c7c..725d93b 100644 (file)
@@ -938,18 +938,34 @@ class JobQueue(object):
       logging.debug("Found job %s in memcache", job_id)
       return job
 
+    job = self._LoadJobFromDisk(job_id)
+
+    self._memcache[job_id] = job
+    logging.debug("Added job %s to the cache", job_id)
+    return job
+
+  def _LoadJobFromDisk(self, job_id):
+    """Load the given job file from disk.
+
+    Given a job file, read, load and restore it in a _QueuedJob format.
+
+    @type job_id: string
+    @param job_id: job identifier
+    @rtype: L{_QueuedJob} or None
+    @return: either None or the job object
+
+    """
     filepath = self._GetJobPath(job_id)
     logging.debug("Loading job from %s", filepath)
     try:
       raw_data = utils.ReadFile(filepath)
-    except IOError, err:
+    except EnvironmentError, err:
       if err.errno in (errno.ENOENT, ):
         return None
       raise
 
-    data = serializer.LoadJson(raw_data)
-
     try:
+      data = serializer.LoadJson(raw_data)
       job = _QueuedJob.Restore(self, data)
     except Exception, err: # pylint: disable-msg=W0703
       new_path = self._GetArchivedJobPath(job_id)
@@ -962,8 +978,6 @@ class JobQueue(object):
         self._RenameFilesUnlocked([(filepath, new_path)])
       return None
 
-    self._memcache[job_id] = job
-    logging.debug("Added job %s to the cache", job_id)
     return job
 
   def _GetJobsUnlocked(self, job_ids):