Commit 85a1c57d authored by Guido Trotter's avatar Guido Trotter
Browse files

Optimize _GetJobIDsUnlocked

Currently we sort the list of job queue files twice (once in
utils.ListVisibleFiles with sort and then later with NiceSort). We apply
the _RE_JOB_FILE regular expression twice (once in _ListJobFiles and
once in _ExtractJobID). This simplifies the code a little, and a couple
of functions performing basically the same job are collapsed.
Signed-off-by: default avatarGuido Trotter <>
Reviewed-by: default avatarMichael Hanselmann <>
parent 69b03fd7
......@@ -901,49 +901,28 @@ class JobQueue(object):
return utils.PathJoin(constants.JOB_QUEUE_ARCHIVE_DIR,
cls._GetArchiveDirectory(job_id), "job-%s" % job_id)
def _ExtractJobID(cls, name):
"""Extract the job id from a filename.
@type name: str
@param name: the job filename
@rtype: job id or None
@return: the job id corresponding to the given filename,
or None if the filename does not represent a valid
job file
m = cls._RE_JOB_FILE.match(name)
if m:
return None
def _GetJobIDsUnlocked(self):
def _GetJobIDsUnlocked(self, sort=True):
"""Return all known job IDs.
The method only looks at disk because it's a requirement that all
jobs are present on disk (so in the _memcache we don't have any
extra IDs).
@type sort: boolean
@param sort: perform sorting on the returned job ids
@rtype: list
@return: the list of job IDs
jlist = [self._ExtractJobID(name) for name in self._ListJobFiles()]
jlist = utils.NiceSort(jlist)
jlist = []
for filename in utils.ListVisibleFiles(constants.QUEUE_DIR, sort=False):
m = self._RE_JOB_FILE.match(filename)
if m:
if sort:
jlist = utils.NiceSort(jlist)
return jlist
def _ListJobFiles(self):
"""Returns the list of current job files.
@rtype: list
@return: the list of job file names
return [name for name in utils.ListVisibleFiles(constants.QUEUE_DIR)
if self._RE_JOB_FILE.match(name)]
def _LoadJobUnlocked(self, job_id):
"""Loads a job from the disk or memory.
......@@ -1051,7 +1030,7 @@ class JobQueue(object):
raise errors.JobQueueDrainError("Job queue is drained, refusing job")
# Check job queue size
size = len(self._ListJobFiles())
size = len(self._GetJobIDsUnlocked(sort=False))
if size >= constants.JOB_QUEUE_SIZE_SOFT_LIMIT:
# TODO: Autoarchive jobs. Make sure it's not done on every job
# submission, though.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment