From 7f6732422bc9ccc5b98489f088b567fa874d7387 Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 14 Sep 2021 18:25:28 +0700 Subject: [PATCH] media files import with lock to force single run --- tubearchivist/home/src/helper.py | 6 ++++++ tubearchivist/home/tasks.py | 20 +++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/tubearchivist/home/src/helper.py b/tubearchivist/home/src/helper.py index b4210dd..900722e 100644 --- a/tubearchivist/home/src/helper.py +++ b/tubearchivist/home/src/helper.py @@ -100,6 +100,12 @@ def get_dl_message(cache_dir): return json_str +def get_lock(lock_key): + """ handle lock for task management """ + redis_lock = redis.Redis(host=REDIS_HOST).lock(lock_key) + return redis_lock + + def monitor_cache_dir(cache_dir): """ look at download cache dir directly as alterative progress info diff --git a/tubearchivist/home/tasks.py b/tubearchivist/home/tasks.py index 8cb8ec1..b24751c 100644 --- a/tubearchivist/home/tasks.py +++ b/tubearchivist/home/tasks.py @@ -15,6 +15,7 @@ from home.src.download import ( ) from home.src.config import AppConfig from home.src.reindex import reindex_old_documents, ManualImport +from home.src.helper import get_lock CONFIG = AppConfig().config @@ -75,7 +76,20 @@ def check_reindex(): @shared_task def run_manual_import(): """ called from settings page, to go through import folder """ + print('starting media file import') - import_handler = ManualImport() - if import_handler.identified: - import_handler.process_import() + have_lock = False + my_lock = get_lock('manual_import') + + try: + have_lock = my_lock.acquire(blocking=False) + if have_lock: + import_handler = ManualImport() + if import_handler.identified: + import_handler.process_import() + else: + print("Did not acquire lock form import.") + + finally: + if have_lock: + my_lock.release()