randydev commited on
Commit
a4b0b69
Β·
verified Β·
1 Parent(s): 3e1bbe0
Files changed (1) hide show
  1. Detection/utils.py +51 -0
Detection/utils.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import asyncio
3
+ from pyrogram import idle
4
+ from .multi_start import start_user
5
+ from . import assistant
6
+ from database import db
7
+
8
+ class DetectionManager:
9
+ def __init__(self):
10
+ self.loop = asyncio.get_event_loop()
11
+ self._setup_logging()
12
+
13
+ def _setup_logging(self):
14
+ logging.basicConfig(
15
+ level=logging.INFO,
16
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
17
+ handlers=[
18
+ logging.FileHandler("detection.log", encoding='utf-8'),
19
+ logging.StreamHandler()
20
+ ]
21
+ )
22
+ logging.getLogger("pyrogram").setLevel(logging.WARNING)
23
+
24
+ async def _start_services(self):
25
+ logging.info("🟑 Starting Detection Manager...")
26
+ await assistant.start()
27
+ logging.info(f"🟒 Assistant {assistant.me.mention} [ID: {assistant.me.id}] started")
28
+ await db.connect()
29
+ logging.info("🟒 Database connection established")
30
+ await start_user()
31
+ logging.info("🟒 All user sessions initialized")
32
+
33
+ async def _shutdown(self):
34
+ logging.info("🟑 Shutting down Detection Manager...")
35
+ tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
36
+ for task in tasks:
37
+ task.cancel()
38
+ await asyncio.gather(*tasks, return_exceptions=True)
39
+ await assistant.stop()
40
+ logging.info("🟒 All services stopped gracefully")
41
+
42
+ async def run(self):
43
+ try:
44
+ await self._start_services()
45
+ await idle()
46
+ except asyncio.CancelledError:
47
+ logging.warning("🟠 Received shutdown signal")
48
+ except Exception as e:
49
+ logging.critical(f"πŸ”΄ Fatal error: {e}", exc_info=True)
50
+ finally:
51
+ await self._shutdown()