dtcn / Detection /utils.py
randydev's picture
Update Detection/utils.py
12ea7b7 verified
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.something_do = "RGV0ZWN0aW9uTGl0ZURldl9Cb3Q="
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()
#if assistant.me.username != base64.b64decode(self.something_do):
# logging.critical(f"πŸ”΄ Fatal error Invalid", exc_info=True)
# exit(1)
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()