From 0cf5513aa7d70b76ebee8cbaafc56ebcdb113ad3 Mon Sep 17 00:00:00 2001
From: burnout87 <gabriele.barni@gmail.com>
Date: Thu, 16 May 2024 16:58:46 +0200
Subject: [PATCH 1/4] more details in the sentry and raise InternalError

---
 cdci_data_analysis/flask_app/dispatcher_query.py | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/cdci_data_analysis/flask_app/dispatcher_query.py b/cdci_data_analysis/flask_app/dispatcher_query.py
index 89c079a4..839a5950 100644
--- a/cdci_data_analysis/flask_app/dispatcher_query.py
+++ b/cdci_data_analysis/flask_app/dispatcher_query.py
@@ -1610,9 +1610,13 @@ def get_existing_job_ID_path(self, wd):
                 alias_dir = None
 
         elif len(dir_list) > 1:
-            sentry.capture_message('Found two non aliased identical job_id')
-            print(f'Found two non aliased identical job_id, dir_list: {dir_list}')
-            raise RuntimeError('Found two non aliased identical job_id')
+            sentry.capture_message(f'Found two non aliased identical job_id, dir_list: {dir_list}')
+            self.logger.warning(f'Found two non aliased identical job_id, dir_list: {dir_list}')
+
+            raise InternalError("We have encountered an internal error! "
+                                "Our team is notified and is working on it. We are sorry! "
+                                "When we find a solution we will try to reach you",
+                                status_code=500)
 
         else:
             alias_dir = None

From 8a3b0c5ea0bd256834c4c2406defcc81eae2a1a8 Mon Sep 17 00:00:00 2001
From: burnout87 <gabriele.barni@gmail.com>
Date: Thu, 16 May 2024 16:58:52 +0200
Subject: [PATCH 2/4] dedicated test

---
 tests/test_server_basic.py | 40 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/tests/test_server_basic.py b/tests/test_server_basic.py
index 2f7805b6..8245171c 100644
--- a/tests/test_server_basic.py
+++ b/tests/test_server_basic.py
@@ -233,6 +233,46 @@ def test_matrix_options_mode_empty_request(dispatcher_live_fixture_with_matrix_o
     logger.info(jdata['config'])
 
 
+@pytest.mark.not_safe_parallel
+@pytest.mark.fast
+def test_error_two_scratch_dir_same_job_id(dispatcher_live_fixture):
+    DispatcherJobState.remove_scratch_folders()
+    server = dispatcher_live_fixture
+    logger.info("constructed server: %s", server)
+
+    encoded_token = jwt.encode(default_token_payload, secret_key, algorithm='HS256')
+    # issuing a request each, with the same set of parameters
+    params = dict(
+        query_status="new",
+        query_type="Real",
+        instrument="empty-async",
+        product_type="dummy",
+        token=encoded_token
+    )
+    DataServerQuery.set_status('submitted')
+    # let's generate a fake scratch dir
+    jdata = ask(server,
+              params,
+              expected_query_status=["submitted"],
+              max_time_s=50,
+              )
+
+    job_id = jdata['job_monitor']['job_id']
+    session_id = jdata['session_id']
+    os.makedirs(f'scratch_sid_01234567890_jid_{job_id}')
+
+    params['job_id'] = job_id
+    params['session_id'] = session_id
+
+    jdata = ask(server,
+                params,
+                expected_status_code=500,
+                expected_query_status=None,
+                )
+    assert jdata['error'] == 'InternalError():we have encountered an internal error! Our team is notified and is working on it. We are sorry! When we find a solution we will try to reach you'
+    assert jdata['error_message'] == 'we have encountered an internal error! Our team is notified and is working on it. We are sorry! When we find a solution we will try to reach you'
+
+
 @pytest.mark.fast
 def test_same_request_different_users(dispatcher_live_fixture):
     server = dispatcher_live_fixture

From f421b9838a1f74ced3a5f79f95201cdd22d283cd Mon Sep 17 00:00:00 2001
From: burnout87 <gabriele.barni@gmail.com>
Date: Thu, 16 May 2024 17:11:03 +0200
Subject: [PATCH 3/4] error message in test

---
 tests/test_server_basic.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tests/test_server_basic.py b/tests/test_server_basic.py
index 8245171c..1891b276 100644
--- a/tests/test_server_basic.py
+++ b/tests/test_server_basic.py
@@ -269,8 +269,8 @@ def test_error_two_scratch_dir_same_job_id(dispatcher_live_fixture):
                 expected_status_code=500,
                 expected_query_status=None,
                 )
-    assert jdata['error'] == 'InternalError():we have encountered an internal error! Our team is notified and is working on it. We are sorry! When we find a solution we will try to reach you'
-    assert jdata['error_message'] == 'we have encountered an internal error! Our team is notified and is working on it. We are sorry! When we find a solution we will try to reach you'
+    assert jdata['error'] == 'InternalError():We have encountered an internal error! Our team is notified and is working on it. We are sorry! When we find a solution we will try to reach you'
+    assert jdata['error_message'] == 'We have encountered an internal error! Our team is notified and is working on it. We are sorry! When we find a solution we will try to reach you'
 
 
 @pytest.mark.fast

From 2e37c1ebb750ccbce5e108153569695773ef66d5 Mon Sep 17 00:00:00 2001
From: burnout87 <gabriele.barni@gmail.com>
Date: Thu, 16 May 2024 17:49:52 +0200
Subject: [PATCH 4/4] removed fake scratch dir at the end of the test

---
 tests/test_server_basic.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tests/test_server_basic.py b/tests/test_server_basic.py
index 1891b276..9e65ff7b 100644
--- a/tests/test_server_basic.py
+++ b/tests/test_server_basic.py
@@ -259,7 +259,8 @@ def test_error_two_scratch_dir_same_job_id(dispatcher_live_fixture):
 
     job_id = jdata['job_monitor']['job_id']
     session_id = jdata['session_id']
-    os.makedirs(f'scratch_sid_01234567890_jid_{job_id}')
+    fake_scratch_dir = f'scratch_sid_01234567890_jid_{job_id}'
+    os.makedirs(fake_scratch_dir)
 
     params['job_id'] = job_id
     params['session_id'] = session_id
@@ -271,6 +272,7 @@ def test_error_two_scratch_dir_same_job_id(dispatcher_live_fixture):
                 )
     assert jdata['error'] == 'InternalError():We have encountered an internal error! Our team is notified and is working on it. We are sorry! When we find a solution we will try to reach you'
     assert jdata['error_message'] == 'We have encountered an internal error! Our team is notified and is working on it. We are sorry! When we find a solution we will try to reach you'
+    os.rmdir(fake_scratch_dir)
 
 
 @pytest.mark.fast