From 4136a2a4f2c0374489538a37b09dadcf57167dc0 Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 2 Dec 2021 19:11:45 +0700 Subject: [PATCH] add rescan lock --- tubearchivist/home/apps.py | 8 +++++++- tubearchivist/home/tasks.py | 33 ++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/tubearchivist/home/apps.py b/tubearchivist/home/apps.py index f17ff3a..878770a 100644 --- a/tubearchivist/home/apps.py +++ b/tubearchivist/home/apps.py @@ -38,7 +38,13 @@ def make_folders(): def release_lock(): """make sure there are no leftover locks set in redis on container start""" - all_locks = ["manual_import", "downloading", "dl_queue", "dl_queue_id"] + all_locks = [ + "manual_import", + "downloading", + "dl_queue", + "dl_queue_id", + "rescan", + ] for lock in all_locks: response = RedisArchivist().del_message(lock) if response: diff --git a/tubearchivist/home/tasks.py b/tubearchivist/home/tasks.py index ac78f0d..0b5a55e 100644 --- a/tubearchivist/home/tasks.py +++ b/tubearchivist/home/tasks.py @@ -45,17 +45,28 @@ def update_subscribed(): "title": "Start rescanning channels and playlists.", } RedisArchivist().set_message("progress:download", message) - channel_handler = ChannelSubscription() - missing_from_channels = channel_handler.find_missing() - playlist_handler = PlaylistSubscription() - missing_from_playlists = playlist_handler.find_missing() - missing_videos = missing_from_channels + missing_from_playlists - if missing_videos: - pending_handler = PendingList() - all_videos_added = pending_handler.add_to_pending(missing_videos) - ThumbManager().download_vid(all_videos_added) - # check if reindex is needed - check_reindex.delay() + + have_lock = False + my_lock = RedisArchivist().get_lock("rescan") + + try: + have_lock = my_lock.acquire(blocking=False) + if have_lock: + channel_handler = ChannelSubscription() + missing_from_channels = channel_handler.find_missing() + playlist_handler = PlaylistSubscription() + missing_from_playlists = playlist_handler.find_missing() + missing = missing_from_channels + missing_from_playlists + if missing: + pending_handler = PendingList() + all_videos_added = pending_handler.add_to_pending(missing) + ThumbManager().download_vid(all_videos_added) + else: + print("Did not acquire rescan lock.") + + finally: + if have_lock: + my_lock.release() @shared_task(name="download_pending")