|
import logging |
|
import asyncio |
|
import base64 |
|
from pyrogram import idle |
|
from .multi_start import start_user |
|
from . import assistant |
|
from database import db |
|
|
|
class DetectionManager: |
|
def __init__(self): |
|
self.loop = asyncio.get_event_loop() |
|
|
|
self._setup_logging() |
|
|
|
def _setup_logging(self): |
|
logging.basicConfig( |
|
level=logging.INFO, |
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
|
handlers=[ |
|
logging.FileHandler("detection.log", encoding='utf-8'), |
|
logging.StreamHandler() |
|
] |
|
) |
|
logging.getLogger("pyrogram").setLevel(logging.WARNING) |
|
|
|
async def _start_services(self): |
|
logging.info("π‘ Starting Detection Manager...") |
|
await assistant.start() |
|
|
|
|
|
|
|
logging.info(f"π’ Assistant {assistant.me.mention} [ID: {assistant.me.id}] started") |
|
await db.connect() |
|
logging.info("π’ Database connection established") |
|
await start_user() |
|
logging.info("π’ All user sessions initialized") |
|
|
|
async def _shutdown(self): |
|
logging.info("π‘ Shutting down Detection Manager...") |
|
tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()] |
|
for task in tasks: |
|
task.cancel() |
|
await asyncio.gather(*tasks, return_exceptions=True) |
|
await assistant.stop() |
|
logging.info("π’ All services stopped gracefully") |
|
|
|
async def run(self): |
|
try: |
|
await self._start_services() |
|
await idle() |
|
except asyncio.CancelledError: |
|
logging.warning("π Received shutdown signal") |
|
except Exception as e: |
|
logging.critical(f"π΄ Fatal error: {e}", exc_info=True) |
|
finally: |
|
await self._shutdown() |