From b4a763c8efa269a9fbb424e1b9d94a7aa4530b14 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rn=20K=C3=BCnsem=C3=B6ller?=
 <jkuensem@physik.uni-bielefeld.de>
Date: Thu, 4 Nov 2021 14:54:40 +0100
Subject: [PATCH] TMSS-763: allow setting an error reason through REST client

---
 SAS/TMSS/client/lib/mains.py                 | 3 ++-
 SAS/TMSS/client/lib/tmss_http_rest_client.py | 5 ++++-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/SAS/TMSS/client/lib/mains.py b/SAS/TMSS/client/lib/mains.py
index f817a3fc21c..4b6ea7e3816 100644
--- a/SAS/TMSS/client/lib/mains.py
+++ b/SAS/TMSS/client/lib/mains.py
@@ -99,12 +99,13 @@ def main_set_subtask_state():
     parser = argparse.ArgumentParser()
     parser.add_argument("subtask_id", type=int, help="The ID of the TMSS subtask to set the status on")
     parser.add_argument("state", help="The state to set")
+    parser.add_argument('-e', '--error_reason', default=None, help="Optional error message string when setting a subtask to error.")
     parser.add_argument('-R', '--rest_api_credentials', type=str, default='TMSSClient', help='TMSS django REST API credentials name, default: TMSSClient')
     args = parser.parse_args()
 
     try:
         with TMSSsession.create_from_dbcreds_for_ldap(dbcreds_name=args.rest_api_credentials) as session:
-            changed_subtask = session.set_subtask_status(args.subtask_id, args.state)
+            changed_subtask = session.set_subtask_status(args.subtask_id, args.state, args.error_reason)
             print("%s now has state %s, see: %s" % (changed_subtask['id'], changed_subtask['state_value'], changed_subtask['url']))
     except Exception as e:
         print(e)
diff --git a/SAS/TMSS/client/lib/tmss_http_rest_client.py b/SAS/TMSS/client/lib/tmss_http_rest_client.py
index 6e6fb09ba50..460154cb0ea 100644
--- a/SAS/TMSS/client/lib/tmss_http_rest_client.py
+++ b/SAS/TMSS/client/lib/tmss_http_rest_client.py
@@ -151,11 +151,14 @@ class TMSSsession(object):
         except:
             pass
 
-    def set_subtask_status(self, subtask_id: int, status: str) -> {}:
+    def set_subtask_status(self, subtask_id: int, status: str, error_reason=None) -> {}:
         '''set the status for the given subtask, and return the subtask with its new state, or raise on error'''
         json_doc = {'state': "%s/subtask_state/%s/" % (self.api_url, status)}
         if status == 'finishing' or status == 'cancelling':
             json_doc['scheduled_on_sky_stop_time'] = datetime.utcnow().isoformat()
+        if status == 'error':
+            hostname = socket.gethostname()
+            json_doc['error_reason'] = error_reason if error_reason is not None else f"set to error via REST client on host '{hostname}'"
         logger.info("updating subtask id=%s status to '%s'", subtask_id, status)
         response = self.session.patch(url='%s/subtask/%s/' % (self.api_url, subtask_id),
                                       json=json_doc)
-- 
GitLab