From 7c2e38d3fddb66ce6cc1971d24ccdca1f93e7b37 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Fri, 5 Feb 2021 09:37:02 +0100
Subject: [PATCH] TMSS-573: added method
 TMSSsession.check_connection_and_exit_on_error with a clear method name and
 intent. This also produces uch more user frienly log lines

---
 .../feedback_handling/lib/feedback_handling.py  |  2 +-
 .../scheduling/bin/tmss_scheduling_service      |  2 +-
 .../services/websocket/lib/websocket_service.py |  2 +-
 SAS/TMSS/client/lib/tmss_http_rest_client.py    | 17 +++++++++++++++--
 4 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/SAS/TMSS/backend/services/feedback_handling/lib/feedback_handling.py b/SAS/TMSS/backend/services/feedback_handling/lib/feedback_handling.py
index 5a8145bb7ec..0740de6b3a9 100644
--- a/SAS/TMSS/backend/services/feedback_handling/lib/feedback_handling.py
+++ b/SAS/TMSS/backend/services/feedback_handling/lib/feedback_handling.py
@@ -203,7 +203,7 @@ def main():
 
     (options, args) = parser.parse_args()
 
-    TMSSsession.check_connection(options.rest_credentials)
+    TMSSsession.check_connection_and_exit_on_error(options.rest_credentials)
 
     with create_service(exchange=options.exchange, broker=options.broker, rest_client_creds_id=options.rest_credentials, qpid_broker=options.qpid_broker, feedback_wait_timeout=options.timeout):
         waitForInterrupt()
diff --git a/SAS/TMSS/backend/services/scheduling/bin/tmss_scheduling_service b/SAS/TMSS/backend/services/scheduling/bin/tmss_scheduling_service
index 13210ce1609..fcbc1d107f5 100755
--- a/SAS/TMSS/backend/services/scheduling/bin/tmss_scheduling_service
+++ b/SAS/TMSS/backend/services/scheduling/bin/tmss_scheduling_service
@@ -48,7 +48,7 @@ def main():
     (options, args) = parser.parse_args()
 
     from lofar.sas.tmss.client.tmss_http_rest_client import TMSSsession
-    TMSSsession.check_connection(options.rest_credentials)
+    TMSSsession.check_connection_and_exit_on_error(options.rest_credentials)
 
     from lofar.sas.tmss.tmss import setup_and_check_tmss_django
     setup_and_check_tmss_django(options.dbcredentials)
diff --git a/SAS/TMSS/backend/services/websocket/lib/websocket_service.py b/SAS/TMSS/backend/services/websocket/lib/websocket_service.py
index 9f641d04aaf..e87029d4d68 100644
--- a/SAS/TMSS/backend/services/websocket/lib/websocket_service.py
+++ b/SAS/TMSS/backend/services/websocket/lib/websocket_service.py
@@ -184,7 +184,7 @@ def main():
 
     (options, args) = parser.parse_args()
 
-    TMSSsession.check_connection(options.rest_credentials)
+    TMSSsession.check_connection_and_exit_on_error(options.rest_credentials)
 
     with create_service(options.websocket_port, options.exchange, options.broker, rest_client_creds_id=options.rest_credentials):
         waitForInterrupt()
diff --git a/SAS/TMSS/client/lib/tmss_http_rest_client.py b/SAS/TMSS/client/lib/tmss_http_rest_client.py
index 760097c1623..ee8e5494123 100644
--- a/SAS/TMSS/client/lib/tmss_http_rest_client.py
+++ b/SAS/TMSS/client/lib/tmss_http_rest_client.py
@@ -64,8 +64,21 @@ class TMSSsession(object):
         '''Open a connection to TMSS using the credentials for the given dbcreds_name
         raises if no connection possible'''
         with TMSSsession.create_from_dbcreds_for_ldap(dbcreds_name) as client:
-            client.get_path_as_string("")
-            logger.info("Http REST login to TMSS working on %s for user '%s'", client.host_url, client.username)
+            try:
+                client.get_path_as_string("")
+                logger.info("Http REST login to TMSS working on %s for user '%s'", client.api_url, client.username)
+            except:
+                raise ConnectionError("Cannot connect to TMSS REST API %s for user '%s'" % (client.api_url, client.username))
+
+    @staticmethod
+    def check_connection_and_exit_on_error(dbcreds_name: str=None):
+        '''Open a connection to TMSS using the credentials for the given dbcreds_name
+        exit(1) upon ConnectionError'''
+        try:
+            TMSSsession.check_connection(dbcreds_name)
+        except Exception as e:
+            logger.error(e)
+            exit(1)
 
     def __enter__(self):
         self.open()
-- 
GitLab