From 099b2870b6a2da3215d001d5aa77e703b173a096 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Wed, 15 Sep 2010 19:31:03 +0200 Subject: [PATCH] jqueue: Move CancelJob logic to separate function Moving the internals of this function will allow it to be used from unittests in the future. Splitting this into a pure, side-effect free function and an impure one makes the pure function easily testable. Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/jqueue.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/jqueue.py b/lib/jqueue.py index d7cf580e6..2927b2a37 100644 --- a/lib/jqueue.py +++ b/lib/jqueue.py @@ -410,6 +410,26 @@ class _QueuedJob(object): op.result = result not_marked = False + def Cancel(self): + status = self.CalcStatus() + + if status not in (constants.JOB_STATUS_QUEUED, + constants.JOB_STATUS_WAITLOCK): + logging.debug("Job %s is no longer waiting in the queue", self.id) + return (False, "Job %s is no longer waiting in the queue" % self.id) + + if status == constants.JOB_STATUS_QUEUED: + self.MarkUnfinishedOps(constants.OP_STATUS_CANCELED, + "Job canceled by request") + msg = "Job %s canceled" % self.id + + elif status == constants.JOB_STATUS_WAITLOCK: + # The worker will notice the new status and cancel the job + self.MarkUnfinishedOps(constants.OP_STATUS_CANCELING, None) + msg = "Job %s will be canceled" % self.id + + return (True, msg) + class _OpExecCallbacks(mcpu.OpExecCbBase): def __init__(self, queue, job, op): @@ -1502,26 +1522,12 @@ class JobQueue(object): logging.debug("Job %s not found", job_id) return (False, "Job %s not found" % job_id) - job_status = job.CalcStatus() + (success, msg) = job.Cancel() - if job_status not in (constants.JOB_STATUS_QUEUED, - constants.JOB_STATUS_WAITLOCK): - logging.debug("Job %s is no longer waiting in the queue", job.id) - return (False, "Job %s is no longer waiting in the queue" % job.id) + if success: + self.UpdateJobUnlocked(job) - if job_status == constants.JOB_STATUS_QUEUED: - job.MarkUnfinishedOps(constants.OP_STATUS_CANCELED, - "Job canceled by request") - msg = "Job %s canceled" % job.id - - elif job_status == constants.JOB_STATUS_WAITLOCK: - # The worker will notice the new status and cancel the job - job.MarkUnfinishedOps(constants.OP_STATUS_CANCELING, None) - msg = "Job %s will be canceled" % job.id - - self.UpdateJobUnlocked(job) - - return (True, msg) + return (success, msg) @_RequireOpenQueue def _ArchiveJobsUnlocked(self, jobs): -- GitLab