From 7435f941a6f25faf65dc8120a0fbdd86d3513e8f Mon Sep 17 00:00:00 2001 From: Lisa-Ann B Date: Fri, 28 Jun 2024 08:36:03 -0400 Subject: [PATCH 1/2] Check if job already running - https://github.com/hubmapconsortium/search-api/issues/141 --- src/app.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/app.py b/src/app.py index c2fa5b0..35662a2 100644 --- a/src/app.py +++ b/src/app.py @@ -705,9 +705,14 @@ def reindex_all(self): try: translator = self.init_translator(token) - threading.Thread(target=translator.translate_all, args=[]).start() + job_name = 'reindex_all' + if self._is_job_thread_running(job_name) is False: + threading.Thread(target=translator.translate_all, args=[], name=job_name).start() + logger.info('Started live reindex all') + else: + logger.info('Could not start live reindex all. Thread already running.') + return 'Request of live reindex all documents not accepted', 409 - logger.info('Started live reindex all') except Exception as e: logger.exception(e) @@ -715,6 +720,15 @@ def reindex_all(self): return 'Request of live reindex all documents accepted', 202 + def _is_job_thread_running(self, job_name): + is_running = False + for th in threading.enumerate(): + if th.name == job_name: + is_running = True + break + + return is_running + def _get_index_mappings(self, composite_index): # get URL for the composite index specified. configured_base_url = self.INDICES['indices'][composite_index]['elasticsearch']['url'].strip('/') From 077593cc327bc3a77b50f2b0b27a4e5d1914b2a4 Mon Sep 17 00:00:00 2001 From: Lisa-Ann B Date: Fri, 28 Jun 2024 08:52:42 -0400 Subject: [PATCH 2/2] Update response message - https://github.com/hubmapconsortium/search-api/issues/141 --- src/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app.py b/src/app.py index 35662a2..0273ff2 100644 --- a/src/app.py +++ b/src/app.py @@ -711,7 +711,7 @@ def reindex_all(self): logger.info('Started live reindex all') else: logger.info('Could not start live reindex all. Thread already running.') - return 'Request of live reindex all documents not accepted', 409 + return 'Request of live reindex all documents not accepted. Reindex all process already running', 409 except Exception as e: logger.exception(e)