media files import with lock to force single run

This commit is contained in:
simon 2021-09-14 18:25:28 +07:00
parent 297eb058cc
commit 7f6732422b
2 changed files with 23 additions and 3 deletions

View File

@ -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

View File

@ -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()