From dd467df5fc1daa19094150e02143826415cf43b2 Mon Sep 17 00:00:00 2001 From: pacnpal <183241239+pacnpal@users.noreply.github.com> Date: Sat, 16 Nov 2024 18:12:35 +0000 Subject: [PATCH] Created a new constants.py file to store the REACTIONS dictionary Updated all files to import REACTIONS from constants.py instead of reactions.py: queue_processor.py message_handler.py reactions.py (now only contains reaction handling functions) events.py (now imports REACTIONS from constants.py and handle_archived_reaction from reactions.py) core.py queue_handler.py processor/init.py --- videoarchiver/core/events.py | 3 ++- videoarchiver/processor/__init__.py | 17 +++++++++++------ videoarchiver/processor/constants.py | 13 +++++++++++++ videoarchiver/processor/core.py | 4 ++-- videoarchiver/processor/message_handler.py | 2 +- videoarchiver/processor/queue_handler.py | 13 ++++++++++++- videoarchiver/processor/queue_processor.py | 2 +- videoarchiver/processor/reactions.py | 16 +++------------- 8 files changed, 45 insertions(+), 25 deletions(-) create mode 100644 videoarchiver/processor/constants.py diff --git a/videoarchiver/core/events.py b/videoarchiver/core/events.py index cbb1420..2228b79 100644 --- a/videoarchiver/core/events.py +++ b/videoarchiver/core/events.py @@ -7,7 +7,8 @@ import traceback from typing import TYPE_CHECKING, Dict, Any, Optional from datetime import datetime -from ..processor.reactions import REACTIONS, handle_archived_reaction +from ..processor.constants import REACTIONS +from ..processor.reactions import handle_archived_reaction from .guild import initialize_guild_components, cleanup_guild_components from .error_handler import error_manager from .response_handler import response_manager diff --git a/videoarchiver/processor/__init__.py b/videoarchiver/processor/__init__.py index 60a2ca7..bdca8e4 100644 --- a/videoarchiver/processor/__init__.py +++ b/videoarchiver/processor/__init__.py @@ -1,40 +1,45 @@ """Video processing module for VideoArchiver""" from .core import VideoProcessor -from .reactions import REACTIONS +from .constants import REACTIONS from .progress_tracker import ProgressTracker from .message_handler import MessageHandler from .queue_handler import QueueHandler # Export public classes and constants __all__ = [ - 'VideoProcessor', - 'REACTIONS', - 'ProgressTracker', - 'MessageHandler', - 'QueueHandler' + "VideoProcessor", + "REACTIONS", + "ProgressTracker", + "MessageHandler", + "QueueHandler", ] # Create a shared progress tracker instance for module-level access progress_tracker = ProgressTracker() + # Export progress tracking functions that wrap the instance methods def update_download_progress(url, progress_data): """Update download progress for a specific URL""" progress_tracker.update_download_progress(url, progress_data) + def complete_download(url): """Mark a download as complete""" progress_tracker.complete_download(url) + def increment_download_retries(url): """Increment retry count for a download""" progress_tracker.increment_download_retries(url) + def get_download_progress(url=None): """Get download progress for a specific URL or all downloads""" return progress_tracker.get_download_progress(url) + def get_active_operations(): """Get all active operations""" return progress_tracker.get_active_operations() diff --git a/videoarchiver/processor/constants.py b/videoarchiver/processor/constants.py new file mode 100644 index 0000000..71b988b --- /dev/null +++ b/videoarchiver/processor/constants.py @@ -0,0 +1,13 @@ +"""Constants for VideoProcessor""" + +# Reaction emojis +REACTIONS = { + 'queued': '📹', + 'processing': '⚙️', + 'success': '✅', + 'error': '❌', + 'archived': '🔄', # New reaction for already archived videos + 'numbers': ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣'], + 'progress': ['⬛', '🟨', '🟩'], + 'download': ['0️⃣', '2️⃣', '4️⃣', '6️⃣', '8️⃣', '🔟'] +} diff --git a/videoarchiver/processor/core.py b/videoarchiver/processor/core.py index 9d76680..cc29a59 100644 --- a/videoarchiver/processor/core.py +++ b/videoarchiver/processor/core.py @@ -3,7 +3,7 @@ import logging import asyncio from enum import Enum -from typing import Optional, Tuple, Dict, Any +from typing import Optional, Tuple, Dict, Any, List from datetime import datetime import discord from discord.ext import commands @@ -13,7 +13,7 @@ from .queue_handler import QueueHandler from .progress_tracker import ProgressTracker from .status_display import StatusDisplay from .cleanup_manager import CleanupManager -from .reactions import REACTIONS +from .constants import REACTIONS logger = logging.getLogger("VideoArchiver") diff --git a/videoarchiver/processor/message_handler.py b/videoarchiver/processor/message_handler.py index 1d5111f..fb1c542 100644 --- a/videoarchiver/processor/message_handler.py +++ b/videoarchiver/processor/message_handler.py @@ -10,7 +10,7 @@ import discord from .url_extractor import URLExtractor from .message_validator import MessageValidator from .queue_processor import QueueProcessor -from .reactions import REACTIONS +from .constants import REACTIONS logger = logging.getLogger("VideoArchiver") diff --git a/videoarchiver/processor/queue_handler.py b/videoarchiver/processor/queue_handler.py index 7c9a445..b58db80 100644 --- a/videoarchiver/processor/queue_handler.py +++ b/videoarchiver/processor/queue_handler.py @@ -7,7 +7,7 @@ import discord from typing import Dict, Optional, Tuple, Any from datetime import datetime -from .reactions import REACTIONS +from .constants import REACTIONS from .progress_tracker import ProgressTracker logger = logging.getLogger("VideoArchiver") @@ -305,3 +305,14 @@ class QueueHandler: except Exception as e: logger.error(f"Failed to update download progress reaction: {e}") + + def is_healthy(self) -> bool: + """Check if handler is healthy""" + # Check if any downloads are stuck + current_time = datetime.utcnow() + for url, task in self._active_downloads.items(): + if not task.done() and task.get_coro().cr_frame.f_locals.get('start_time'): + start_time = task.get_coro().cr_frame.f_locals['start_time'] + if (current_time - start_time).total_seconds() > 3600: # 1 hour timeout + return False + return True diff --git a/videoarchiver/processor/queue_processor.py b/videoarchiver/processor/queue_processor.py index 1565441..4731be4 100644 --- a/videoarchiver/processor/queue_processor.py +++ b/videoarchiver/processor/queue_processor.py @@ -8,7 +8,7 @@ from datetime import datetime import discord from ..queue.models import QueueItem -from .reactions import REACTIONS +from .constants import REACTIONS logger = logging.getLogger("VideoArchiver") diff --git a/videoarchiver/processor/reactions.py b/videoarchiver/processor/reactions.py index 2c27688..60c8cd9 100644 --- a/videoarchiver/processor/reactions.py +++ b/videoarchiver/processor/reactions.py @@ -1,22 +1,12 @@ -"""Reaction emojis and reaction management for VideoProcessor""" +"""Reaction handling for VideoProcessor""" import logging import asyncio import discord -logger = logging.getLogger("VideoArchiver") +from .constants import REACTIONS -# Reaction emojis -REACTIONS = { - 'queued': '📹', - 'processing': '⚙️', - 'success': '✅', - 'error': '❌', - 'archived': '🔄', # New reaction for already archived videos - 'numbers': ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣'], - 'progress': ['⬛', '🟨', '🟩'], - 'download': ['0️⃣', '2️⃣', '4️⃣', '6️⃣', '8️⃣', '🔟'] -} +logger = logging.getLogger("VideoArchiver") async def handle_archived_reaction(message: discord.Message, user: discord.User, db) -> None: """Handle reaction to archived video message"""