File size: 2,039 Bytes
a4b0b69
 
3179f2f
a4b0b69
 
 
 
 
 
 
 
12ea7b7
a4b0b69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12ea7b7
 
 
a4b0b69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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()