From 911a495b6553598d29c7b23294651be3cb602ce5 Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Mon, 14 Jul 2008 10:08:52 +0000 Subject: [PATCH] Change JobStorage to work with ids not filenames Currently some of the functions in JobStorage work with filenames (which is an implementation detail and should only be used when dealing with the storage) and not with job IDs. We need to change this in order to implement a job cache. Reviewed-by: ultrotter --- lib/jqueue.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/jqueue.py b/lib/jqueue.py index 2e765af0d..01a5816fd 100644 --- a/lib/jqueue.py +++ b/lib/jqueue.py @@ -223,7 +223,7 @@ class _JobQueueWorkerPool(workerpool.WorkerPool): class JobStorage(object): - _RE_JOB_FILE = re.compile(r"^job-\d+$") + _RE_JOB_FILE = re.compile(r"^job-(\d+)$") def __init__(self): self._lock = threading.Lock() @@ -313,15 +313,27 @@ class JobStorage(object): def _GetJobPath(self, job_id): return os.path.join(constants.QUEUE_DIR, "job-%s" % job_id) + def _GetJobIDsUnlocked(self, archived=False): + """Return all known job IDs. + + If the parameter archived is True, archived jobs IDs will be + included. Currently this argument is unused. + + """ + jfiles = self._ListJobFiles() + return [m.group(1) for m in + [self._RE_JOB_FILE.match(name) for name in jfiles]] + def _ListJobFiles(self): assert self.lock_fd, "Queue should be open" return [name for name in utils.ListVisibleFiles(constants.QUEUE_DIR) if self._RE_JOB_FILE.match(name)] - def _LoadJobUnlocked(self, filepath): + def _LoadJobUnlocked(self, job_id): assert self.lock_fd, "Queue should be open" + filepath = self._GetJobPath(job_id) logging.debug("Loading job from %s", filepath) try: fd = open(filepath, "r") @@ -337,13 +349,10 @@ class JobStorage(object): return _QueuedJob.Restore(self, data) def _GetJobsUnlocked(self, job_ids): - if job_ids: - files = [self._GetJobPath(job_id) for job_id in job_ids] - else: - files = [os.path.join(constants.QUEUE_DIR, filename) - for filename in self._ListJobFiles()] + if not job_ids: + job_ids = self._GetJobIDsUnlocked() - return [self._LoadJobUnlocked(filepath) for filepath in files] + return [self._LoadJobUnlocked(job_id) for job_id in job_ids] @utils.LockedMethod def GetJobs(self, job_ids): -- GitLab