This repository was archived by the owner on Feb 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
216 lines (174 loc) · 6.47 KB
/
Copy pathmain.py
File metadata and controls
216 lines (174 loc) · 6.47 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# main.py
# !/usr/bin/env python3
import asyncio
import signal
from pathlib import Path
from typing import Dict, Optional
import uvloop
from client_manager import ClientManager
from config.settings import Config, ClientConfig
from utils.logger import get_logger
class PyrogramMultiClient:
def __init__(self, config_path: str = "config.yaml"):
"""
Initialize the multi-client manager.
Args:
config_path: Path to the configuration file.
"""
self.config = Config(config_path)
self.managers: Dict[str, ClientManager] = {}
self.main_logger = get_logger("PyrogramMultiClient")
self.shutdown_event = asyncio.Event()
# Set uvloop for improved performance
uvloop.install()
# Create necessary directories
for directory in ["sessions", "logs"]:
Path(directory).mkdir(exist_ok=True)
# Setup signal handlers
self._setup_signal_handlers()
def _setup_signal_handlers(self):
"""Setup signal handlers for graceful shutdown."""
for sig in (signal.SIGTERM, signal.SIGINT):
asyncio.get_event_loop().add_signal_handler(
sig,
lambda s=sig: asyncio.create_task(self._shutdown(s))
)
async def _shutdown(self, sig: Optional[signal.Signals] = None):
"""
Gracefully shutdown all clients.
Args:
sig: Signal that triggered the shutdown.
"""
if sig:
self.main_logger.info(f"Received exit signal {sig.name}...")
await self.stop_all()
self.shutdown_event.set()
async def _start_manager(self, client_config: ClientConfig) -> bool:
"""
Start an individual client manager.
Args:
client_config: Client configuration.
Returns:
bool: True if successful, False otherwise.
"""
try:
# Create the manager
manager = ClientManager(client_config)
success = await manager.start()
if success:
# Save the manager and its formatter
self.managers[client_config.session_name] = manager
formatter = manager.get_formatter()
if formatter:
# Use formatter for system message
start_message = formatter.format_system_message(
f"Successfully started {client_config.type} client"
)
self.main_logger.info(start_message)
else:
self.main_logger.info(
f"Successfully started {client_config.type} client: {client_config.session_name}"
)
else:
self.main_logger.error(
f"Failed to start {client_config.type} client: {client_config.session_name}"
)
return success
except Exception as e:
self.main_logger.error(
f"Error starting {client_config.type} client {client_config.session_name}: {e}"
)
return False
async def start_all(self):
"""Start all clients in parallel."""
self.main_logger.info("Starting all clients...")
# Group clients by type for logging
clients_by_type = {
"user": [],
"bot": []
}
for client_config in self.config.clients:
clients_by_type[client_config.type].append(client_config)
# Start clients in parallel
tasks = []
for client_type, configs in clients_by_type.items():
self.main_logger.info(f"Starting {len(configs)} {client_type} client(s)...")
for config in configs:
task = asyncio.create_task(
self._start_manager(config),
name=f"start_{config.session_name}"
)
tasks.append((config, task))
# Await all tasks
failed = 0
for config, task in tasks:
try:
success = await task
if not success:
failed += 1
except Exception as e:
self.main_logger.error(
f"Unexpected error starting {config.session_name}: {e}"
)
failed += 1
total = len(tasks)
success = total - failed
self.main_logger.info(
f"Client startup completed. Success: {success}/{total}"
)
if failed:
self.main_logger.warning(
f"{failed} client(s) failed to start"
)
async def stop_all(self):
"""Gracefully shutdown all clients."""
if not self.managers:
return
self.main_logger.info("Shutting down all clients...")
stop_tasks = []
for name, manager in self.managers.items():
task = asyncio.create_task(
manager.stop(),
name=f"stop_{name}"
)
stop_tasks.append((name, task))
for name, task in stop_tasks:
try:
await task
self.main_logger.info(f"Client {name} stopped successfully")
except Exception as e:
self.main_logger.error(f"Error stopping {name}: {e}")
self.managers.clear()
self.main_logger.info("All clients stopped")
async def run(self):
"""Main method to start all clients."""
try:
# Start all clients
await self.start_all()
# Wait for shutdown signal
await self.shutdown_event.wait()
except Exception as e:
self.main_logger.error(f"Critical error: {e}")
finally:
# Attempt to gracefully stop all clients
await self.stop_all()
def get_manager(self, session_name: str) -> Optional[ClientManager]:
"""
Get the client manager by session name.
Args:
session_name: Session name.
Returns:
Optional[ClientManager]: Client manager or None.
"""
return self.managers.get(session_name)
async def main():
"""Entry point of the application."""
manager = PyrogramMultiClient("config.yaml")
try:
await manager.run()
except KeyboardInterrupt:
manager.main_logger.info("Received keyboard interrupt")
except Exception as e:
manager.main_logger.error(f"Critical error in main: {e}")
if __name__ == "__main__":
asyncio.run(main())