mirror of
https://github.com/pacnpal/Pac-cogs.git
synced 2025-12-20 02:41:06 -05:00
fix: Add robust error handling for cog loading - Add initialization timeout - Add proper cleanup on failure - Add teardown function - Add proper logging - Add proper cog removal order
This commit is contained in:
@@ -1,23 +1,62 @@
|
|||||||
"""VideoArchiver cog for Red-DiscordBot"""
|
"""VideoArchiver cog for Red-DiscordBot"""
|
||||||
from redbot.core.bot import Red
|
from redbot.core.bot import Red
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
from .video_archiver import VideoArchiver
|
from .video_archiver import VideoArchiver
|
||||||
|
from .exceptions import ProcessingError
|
||||||
|
|
||||||
|
logger = logging.getLogger("VideoArchiver")
|
||||||
|
|
||||||
async def setup(bot: Red) -> None:
|
async def setup(bot: Red) -> None:
|
||||||
"""Load VideoArchiver."""
|
"""Load VideoArchiver."""
|
||||||
# Load main cog first
|
try:
|
||||||
cog = VideoArchiver(bot)
|
# Load main cog first
|
||||||
await bot.add_cog(cog)
|
cog = VideoArchiver(bot)
|
||||||
|
await bot.add_cog(cog)
|
||||||
# Wait for initialization to complete
|
|
||||||
await cog.ready.wait()
|
# Wait for initialization to complete with timeout
|
||||||
|
try:
|
||||||
# Only load commands if main cog initialized successfully
|
await asyncio.wait_for(cog.ready.wait(), timeout=30)
|
||||||
if cog.ready.is_set():
|
except asyncio.TimeoutError:
|
||||||
from .commands import VideoArchiverCommands
|
logger.error("VideoArchiver initialization timed out")
|
||||||
commands_cog = VideoArchiverCommands(
|
await bot.remove_cog(cog.__class__.__name__)
|
||||||
bot,
|
raise ProcessingError("Initialization timed out")
|
||||||
cog.config_manager,
|
|
||||||
cog.update_checker,
|
# Only load commands if main cog initialized successfully
|
||||||
cog.processor
|
if cog.ready.is_set():
|
||||||
)
|
try:
|
||||||
await bot.add_cog(commands_cog)
|
from .commands import VideoArchiverCommands
|
||||||
|
commands_cog = VideoArchiverCommands(
|
||||||
|
bot,
|
||||||
|
cog.config_manager,
|
||||||
|
cog.update_checker,
|
||||||
|
cog.processor
|
||||||
|
)
|
||||||
|
await bot.add_cog(commands_cog)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to load commands cog: {str(e)}")
|
||||||
|
# Clean up main cog if commands fail to load
|
||||||
|
await bot.remove_cog(cog.__class__.__name__)
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
logger.error("VideoArchiver failed to initialize")
|
||||||
|
await bot.remove_cog(cog.__class__.__name__)
|
||||||
|
raise ProcessingError("Initialization failed")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to load VideoArchiver: {str(e)}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
async def teardown(bot: Red) -> None:
|
||||||
|
"""Clean up when unloading."""
|
||||||
|
try:
|
||||||
|
# Remove commands cog first
|
||||||
|
if "VideoArchiverCommands" in bot.cogs:
|
||||||
|
await bot.remove_cog("VideoArchiverCommands")
|
||||||
|
|
||||||
|
# Then remove main cog
|
||||||
|
if "VideoArchiver" in bot.cogs:
|
||||||
|
await bot.remove_cog("VideoArchiver")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error during teardown: {str(e)}")
|
||||||
|
raise
|
||||||
|
|||||||
Reference in New Issue
Block a user