From a48fcd667bb1157765966b4786ec4140872d82a1 Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Mon, 3 Feb 2025 04:18:49 +0300 Subject: [PATCH] Improve media file download logging and handling in background task --- api_server.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/api_server.py b/api_server.py index 2d42622..44dd3fb 100644 --- a/api_server.py +++ b/api_server.py @@ -178,6 +178,11 @@ async def download_new_files(media_files: list, cache_dir: str): """ Download files that are not in cache yet """ + if not media_files: + logger.info("No media files found for download") + return + + files_to_download = 0 for file_data in media_files: try: file_id = file_data['file_id'] @@ -189,6 +194,7 @@ async def download_new_files(media_files: list, cache_dir: str): cache_path = os.path.join(cache_dir, f"{channel}-{post_id}-{file_unique_id}") if not os.path.exists(cache_path): + files_to_download += 1 logger.debug(f"Background download started for {file_id}") await download_media_file(channel, post_id, file_unique_id) await asyncio.sleep(1) # Delay between downloads @@ -197,6 +203,9 @@ async def download_new_files(media_files: list, cache_dir: str): logger.error(f"Background download failed for {file_id}: {str(e)}") continue + if files_to_download == 0: + logger.info("All media files are already in cache") + async def cache_media_files(): """Background task for cache management: removes old files and downloads new ones"""