This commit is contained in:
pacnpal
2024-11-15 02:48:05 +00:00
parent 6f0f31944f
commit 8fcdec7550

View File

@@ -37,7 +37,7 @@ class VideoProcessor:
max_queue_size=1000, max_queue_size=1000,
cleanup_interval=1800, # 30 minutes (reduced from 1 hour for more frequent cleanup) cleanup_interval=1800, # 30 minutes (reduced from 1 hour for more frequent cleanup)
max_history_age=86400, # 24 hours max_history_age=86400, # 24 hours
persistence_path=str(queue_path), persistence_path=str(queue_path)
) )
# Track failed downloads for cleanup # Track failed downloads for cleanup
@@ -45,73 +45,39 @@ class VideoProcessor:
self._failed_downloads_lock = asyncio.Lock() self._failed_downloads_lock = asyncio.Lock()
# Start queue processing # Start queue processing
self._queue_task = asyncio.create_task(self._process_queue()) self._queue_task = asyncio.create_task(self.queue_manager.process_queue(self._process_video))
async def _process_queue(self):
"""Process the queue continuously"""
try:
await self.queue_manager.process_queue(self._process_video)
except Exception as e:
logger.error(f"Queue processing error: {traceback.format_exc()}")
# Restart queue processing
self._queue_task = asyncio.create_task(self._process_queue())
async def _process_video(self, item: Any) -> Tuple[bool, Optional[str]]: async def _process_video(self, item: Any) -> Tuple[bool, Optional[str]]:
"""Process a video from the queue""" """Process a video from the queue"""
try: try:
# Get the callback from the item # Get the message
callback = getattr(item, "callback", None) channel = self.bot.get_channel(item.channel_id)
if callback: if not channel:
success = await callback(item.url, True, "") return False, "Channel not found"
return success, None if success else "Callback failed"
return False, "No callback found" try:
except Exception as e: message = await channel.fetch_message(item.message_id)
logger.error(f"Error processing video: {traceback.format_exc()}") if not message:
return False, str(e) return False, "Message not found"
except discord.NotFound:
return False, "Message not found"
except discord.Forbidden:
return False, "Bot lacks permissions to fetch message"
except Exception as e:
return False, f"Error fetching message: {str(e)}"
async def process_video_url(
self, url: str, message: discord.Message, priority: int = 0
) -> bool:
"""Process a video URL: download, reupload, and cleanup"""
guild_id = message.guild.id guild_id = message.guild.id
file_path = None
start_time = datetime.utcnow() start_time = datetime.utcnow()
try: try:
# Add initial reactions
await message.add_reaction("📹")
await message.add_reaction("")
await self._log_message(message.guild, f"Processing video URL: {url}")
settings = await self.config.get_guild_settings(guild_id) settings = await self.config.get_guild_settings(guild_id)
# Check user roles with detailed error message
if not await self.config.check_user_roles(message.author):
await message.remove_reaction("", self.bot.user)
await message.add_reaction("🚫")
await self._log_message(
message.guild,
f"User {message.author} does not have required roles for video archiving",
"warning",
)
return False
# Create callback for queue processing with enhanced error handling
async def process_callback(url: str, success: bool, error: str) -> bool:
file_path = None
try:
if not success:
await message.remove_reaction("", self.bot.user)
await message.add_reaction("")
await self._log_message(
message.guild, f"Failed to process video: {error}", "error"
)
return False
# Download video with enhanced error handling # Download video with enhanced error handling
try: try:
success, file_path, error = await self.components[guild_id][ success, file_path, error = await self.components[guild_id][
"downloader" "downloader"
].download_video(url) ].download_video(item.url)
except Exception as e: except Exception as e:
logger.error(f"Download error: {traceback.format_exc()}") logger.error(f"Download error: {traceback.format_exc()}")
success, file_path, error = False, None, str(e) success, file_path, error = False, None, str(e)
@@ -126,7 +92,7 @@ class VideoProcessor:
if file_path: if file_path:
async with self._failed_downloads_lock: async with self._failed_downloads_lock:
self._failed_downloads.add(file_path) self._failed_downloads.add(file_path)
return False return False, error
# Get channels with enhanced error handling # Get channels with enhanced error handling
try: try:
@@ -147,7 +113,7 @@ class VideoProcessor:
f"Channel configuration error: {str(e)}", f"Channel configuration error: {str(e)}",
"error", "error",
) )
return False return False, str(e)
try: try:
# Upload to archive channel with original message link # Upload to archive channel with original message link
@@ -198,7 +164,7 @@ class VideoProcessor:
f"Successfully archived video from {message.author} (took {processing_time:.1f}s)", f"Successfully archived video from {message.author} (took {processing_time:.1f}s)",
) )
return True return True, None
except discord.HTTPException as e: except discord.HTTPException as e:
await self._log_message( await self._log_message(
@@ -206,7 +172,7 @@ class VideoProcessor:
) )
await message.remove_reaction("", self.bot.user) await message.remove_reaction("", self.bot.user)
await message.add_reaction("") await message.add_reaction("")
return False return False, str(e)
finally: finally:
# Always attempt to delete the file if configured # Always attempt to delete the file if configured
@@ -238,9 +204,37 @@ class VideoProcessor:
self._failed_downloads.add(file_path) self._failed_downloads.add(file_path)
except Exception as e: except Exception as e:
logger.error(f"Process callback error: {traceback.format_exc()}") logger.error(f"Process error: {traceback.format_exc()}")
await self._log_message( await self._log_message(
message.guild, f"Error in process callback: {str(e)}", "error" message.guild, f"Error in process: {str(e)}", "error"
)
return False, str(e)
except Exception as e:
logger.error(f"Error processing video: {traceback.format_exc()}")
return False, str(e)
async def process_video_url(self, url: str, message: discord.Message, priority: int = 0) -> bool:
"""Process a video URL: download, reupload, and cleanup"""
guild_id = message.guild.id
start_time = datetime.utcnow()
try:
# Add initial reactions
await message.add_reaction("📹")
await message.add_reaction("")
await self._log_message(message.guild, f"Processing video URL: {url}")
settings = await self.config.get_guild_settings(guild_id)
# Check user roles with detailed error message
if not await self.config.check_user_roles(message.author):
await message.remove_reaction("", self.bot.user)
await message.add_reaction("🚫")
await self._log_message(
message.guild,
f"User {message.author} does not have required roles for video archiving",
"warning",
) )
return False return False
@@ -252,7 +246,7 @@ class VideoProcessor:
channel_id=message.channel.id, channel_id=message.channel.id,
guild_id=guild_id, guild_id=guild_id,
author_id=message.author.id, author_id=message.author.id,
callback=process_callback, callback=None, # No callback needed since _process_video handles everything
priority=priority, priority=priority,
) )
except Exception as e: except Exception as e: