Commit 78d12585 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

jqueue: When auto-archiving jobs, calculate job status only once

This is done by passing the job object to _ArchiveJobUnlocked instead
of only the job ID. Also return whether job was actually archived.

Reviewed-by: ultrotter
parent 58b22b6e
...@@ -1123,25 +1123,20 @@ class JobQueue(object): ...@@ -1123,25 +1123,20 @@ class JobQueue(object):
self.UpdateJobUnlocked(job) self.UpdateJobUnlocked(job)
@_RequireOpenQueue @_RequireOpenQueue
def _ArchiveJobUnlocked(self, job_id): def _ArchiveJobUnlocked(self, job):
"""Archives a job. """Archives a job.
@type job_id: string @type job: L{_QueuedJob}
@param job_id: the ID of job to be archived @param job: Job object
@rtype bool
@return Whether job was archived
""" """
logging.info("Archiving job %s", job_id)
job = self._LoadJobUnlocked(job_id)
if not job:
logging.debug("Job %s not found", job_id)
return
if job.CalcStatus() not in (constants.JOB_STATUS_CANCELED, if job.CalcStatus() not in (constants.JOB_STATUS_CANCELED,
constants.JOB_STATUS_SUCCESS, constants.JOB_STATUS_SUCCESS,
constants.JOB_STATUS_ERROR): constants.JOB_STATUS_ERROR):
logging.debug("Job %s is not yet done", job.id) logging.debug("Job %s is not yet done", job.id)
return return False
old = self._GetJobPath(job.id) old = self._GetJobPath(job.id)
new = self._GetArchivedJobPath(job.id) new = self._GetArchivedJobPath(job.id)
...@@ -1150,6 +1145,8 @@ class JobQueue(object): ...@@ -1150,6 +1145,8 @@ class JobQueue(object):
logging.debug("Successfully archived job %s", job.id) logging.debug("Successfully archived job %s", job.id)
return True
@utils.LockedMethod @utils.LockedMethod
@_RequireOpenQueue @_RequireOpenQueue
def ArchiveJob(self, job_id): def ArchiveJob(self, job_id):
...@@ -1159,9 +1156,18 @@ class JobQueue(object): ...@@ -1159,9 +1156,18 @@ class JobQueue(object):
@type job_id: string @type job_id: string
@param job_id: Job ID of job to be archived. @param job_id: Job ID of job to be archived.
@rtype: bool
@return: Whether job was archived
""" """
return self._ArchiveJobUnlocked(job_id) logging.info("Archiving job %s", job_id)
job = self._LoadJobUnlocked(job_id)
if not job:
logging.debug("Job %s not found", job_id)
return False
return self._ArchiveJobUnlocked(job)
@utils.LockedMethod @utils.LockedMethod
@_RequireOpenQueue @_RequireOpenQueue
...@@ -1180,12 +1186,12 @@ class JobQueue(object): ...@@ -1180,12 +1186,12 @@ class JobQueue(object):
logging.info("Archiving jobs with age more than %s seconds", age) logging.info("Archiving jobs with age more than %s seconds", age)
now = time.time() now = time.time()
for jid in self._GetJobIDsUnlocked(archived=False): for job_id in self._GetJobIDsUnlocked(archived=False):
job = self._LoadJobUnlocked(jid) # Returns None if the job failed to load
if job.CalcStatus() not in (constants.OP_STATUS_SUCCESS, job = self._LoadJobUnlocked(job_id)
constants.OP_STATUS_ERROR, if not job:
constants.OP_STATUS_CANCELED):
continue continue
if job.end_timestamp is None: if job.end_timestamp is None:
if job.start_timestamp is None: if job.start_timestamp is None:
job_age = job.received_timestamp job_age = job.received_timestamp
...@@ -1195,7 +1201,7 @@ class JobQueue(object): ...@@ -1195,7 +1201,7 @@ class JobQueue(object):
job_age = job.end_timestamp job_age = job.end_timestamp
if age == -1 or now - job_age[0] > age: if age == -1 or now - job_age[0] > age:
self._ArchiveJobUnlocked(jid) self._ArchiveJobUnlocked(job)
def _GetJobInfoUnlocked(self, job, fields): def _GetJobInfoUnlocked(self, job, fields):
"""Returns information about a job. """Returns information about a job.
......
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