From 8e0feb4c981dea80d6f0fc0af28861fb1d5ad64e Mon Sep 17 00:00:00 2001 From: Daniel Maljovec Date: Thu, 22 Oct 2020 00:49:02 -0600 Subject: [PATCH 1/3] Add a multiprocessing lock to index syncing --- knowledge_repo/app/index.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/knowledge_repo/app/index.py b/knowledge_repo/app/index.py index 43da1afc4..0541c982e 100644 --- a/knowledge_repo/app/index.py +++ b/knowledge_repo/app/index.py @@ -15,6 +15,7 @@ LOCKED = CHECKED = '1' UNLOCKED = '0' +sync_lock = multiprocessing.Lock() def set_up_indexing_timers(app): if not app.config['INDEXING_ENABLED']: @@ -39,9 +40,19 @@ def index_watchdog(app): def index_sync_loop(app): current_app.db.engine.dispose() while True: - with app.app_context(): - update_index(check_timeouts=False) - time.sleep(app.config['INDEXING_INTERVAL']) + sync_lock.acquire() + try: + with app.app_context(): + print('*'*80) + print(f'Thread {os.getpid()} (locked) calling update_index') + print('*'*80) + + update_index(check_timeouts=False) + finally: + print('*'*80) + print(f'Process {os.getpid()} releasing lock') + print('*'*80) + sync_lock.release() app.index_watchdog = multiprocessing.Process(target=index_watchdog, args=(app,)) app.index_watchdog.start() From 1ea232148ab781fa17f1cfff05025e3928b7933d Mon Sep 17 00:00:00 2001 From: Dan Maljovec <51134596+dmaljovec@users.noreply.github.com> Date: Thu, 22 Oct 2020 07:57:47 -0600 Subject: [PATCH 2/3] Remove print statements --- knowledge_repo/app/index.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/knowledge_repo/app/index.py b/knowledge_repo/app/index.py index 0541c982e..6cd7705ec 100644 --- a/knowledge_repo/app/index.py +++ b/knowledge_repo/app/index.py @@ -43,15 +43,9 @@ def index_sync_loop(app): sync_lock.acquire() try: with app.app_context(): - print('*'*80) - print(f'Thread {os.getpid()} (locked) calling update_index') - print('*'*80) update_index(check_timeouts=False) finally: - print('*'*80) - print(f'Process {os.getpid()} releasing lock') - print('*'*80) sync_lock.release() app.index_watchdog = multiprocessing.Process(target=index_watchdog, args=(app,)) From 37a537d6d1d71746ce7be96af5be8de00e63853a Mon Sep 17 00:00:00 2001 From: Daniel Maljovec Date: Thu, 22 Oct 2020 11:05:14 -0600 Subject: [PATCH 3/3] lint code --- knowledge_repo/app/index.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/knowledge_repo/app/index.py b/knowledge_repo/app/index.py index 6cd7705ec..9dafed1d3 100644 --- a/knowledge_repo/app/index.py +++ b/knowledge_repo/app/index.py @@ -17,6 +17,7 @@ sync_lock = multiprocessing.Lock() + def set_up_indexing_timers(app): if not app.config['INDEXING_ENABLED']: return False @@ -43,10 +44,9 @@ def index_sync_loop(app): sync_lock.acquire() try: with app.app_context(): - update_index(check_timeouts=False) finally: - sync_lock.release() + sync_lock.release() app.index_watchdog = multiprocessing.Process(target=index_watchdog, args=(app,)) app.index_watchdog.start()