From ec16cbaa4a1ba79ac39fe9a384b1c8bfa897de44 Mon Sep 17 00:00:00 2001
From: Nathaniel Kofalt <nathaniel@kofalt.com>
Date: Thu, 7 Dec 2017 15:11:47 -0600
Subject: [PATCH] Allow users to cancel running jobs

---
 api/jobs/handlers.py | 3 ---
 api/jobs/queue.py    | 4 +++-
 api/placer.py        | 1 -
 3 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/api/jobs/handlers.py b/api/jobs/handlers.py
index 6ad71f06..46802aa0 100644
--- a/api/jobs/handlers.py
+++ b/api/jobs/handlers.py
@@ -310,7 +310,6 @@ class JobsHandler(base.RequestHandler):
         count = Queue.scan_for_orphans()
         return { 'orphaned': count }
 
-
 class JobHandler(base.RequestHandler):
     """Provides /Jobs/<jid> routes."""
 
@@ -376,7 +375,6 @@ class JobHandler(base.RequestHandler):
             encoded = pseudo_consistent_json_encode({"config": c})
             self.response.app_iter = StringIO.StringIO(encoded)
 
-
     @require_login
     def put(self, _id):
         """
@@ -420,7 +418,6 @@ class JobHandler(base.RequestHandler):
                     job.inputs[x].check_access(self.uid, 'ro')
                 # Unlike jobs-add, explicitly not checking write access to destination.
 
-
     def get_logs(self, _id):
         """Get a job's logs"""
 
diff --git a/api/jobs/queue.py b/api/jobs/queue.py
index 2a65b4dc..4b9cc21b 100644
--- a/api/jobs/queue.py
+++ b/api/jobs/queue.py
@@ -32,6 +32,7 @@ JOB_STATES_ALLOWED_MUTATE = [
 JOB_TRANSITIONS = [
     'pending --> running',
     'pending --> cancelled',
+    'running --> cancelled',
     'running --> failed',
     'running --> complete',
 ]
@@ -57,8 +58,9 @@ class Queue(object):
         """
 
         if job.state not in JOB_STATES_ALLOWED_MUTATE:
-            raise Exception('Cannot mutate a job that is ' + job.state + '.')
+            raise InputValidationException('Cannot mutate a job that is ' + job.state + '.')
 
+        # TODO: This should use InputValidationException or similar
         if 'state' in mutation and not valid_transition(job.state, mutation['state']):
             raise Exception('Mutating job from ' + job.state + ' to ' + mutation['state'] + ' not allowed.')
 
diff --git a/api/placer.py b/api/placer.py
index 49ee2b74..dc16c448 100644
--- a/api/placer.py
+++ b/api/placer.py
@@ -104,7 +104,6 @@ class Placer(object):
                 session_id = AcquisitionStorage().get_container(str(self.id_)).get('session')
             SessionStorage().recalc_session_compliance(session_id, hard=True)
 
-
 class TargetedPlacer(Placer):
     """
     A placer that can accept N files to a specific container (acquisition, etc).
-- 
GitLab