From 162c86365ab10bc6b5f5cb8c4f15aa10c1fe4ab0 Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Tue, 15 Jun 2010 11:17:24 +0100
Subject: [PATCH] Abstract loading job file from disk

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 | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/lib/jqueue.py b/lib/jqueue.py
index 2554c7cc4..725d93bbd 100644
--- a/lib/jqueue.py
+++ b/lib/jqueue.py
@@ -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):
-- 
GitLab