forked from ShaerWare/AI_Secretary_System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_bot_manager.py
More file actions
341 lines (282 loc) · 11.7 KB
/
multi_bot_manager.py
File metadata and controls
341 lines (282 loc) · 11.7 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python3
"""
Multi-Bot Manager for AI Secretary System.
Manages multiple Telegram bot instances as separate subprocesses.
Each bot runs independently with its own configuration loaded from the database.
"""
import asyncio
import json
import logging
import os
import subprocess
import sys
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional
logger = logging.getLogger(__name__)
class BotProcess:
"""Represents a running bot process."""
def __init__(self, instance_id: str, process: subprocess.Popen, log_file: Path):
self.instance_id = instance_id
self.process = process
self.log_file = log_file
self.started_at = datetime.utcnow()
@property
def is_running(self) -> bool:
"""Check if process is still running."""
return self.process.poll() is None
@property
def pid(self) -> Optional[int]:
"""Get process ID."""
return self.process.pid if self.is_running else None
@property
def uptime_seconds(self) -> int:
"""Get uptime in seconds."""
return int((datetime.utcnow() - self.started_at).total_seconds())
def to_dict(self) -> dict:
"""Convert to dictionary for API response."""
return {
"instance_id": self.instance_id,
"pid": self.pid,
"is_running": self.is_running,
"log_file": str(self.log_file),
"started_at": self.started_at.isoformat(),
"uptime_seconds": self.uptime_seconds,
}
class MultiBotManager:
"""
Manages multiple Telegram bot instances.
Each bot is run as a separate subprocess with the BOT_INSTANCE_ID
environment variable set to load its specific configuration.
"""
def __init__(self):
self._processes: Dict[str, BotProcess] = {}
self._lock = asyncio.Lock()
self._logs_dir = Path(__file__).parent / "logs"
self._logs_dir.mkdir(exist_ok=True)
# New aiogram bot module (replaces old telegram_bot_service.py)
self._bot_module = "telegram_bot"
self._bot_module_path = Path(__file__).parent / "telegram_bot"
def _get_log_path(self, instance_id: str) -> Path:
"""Get log file path for bot instance."""
return self._logs_dir / f"telegram_bot_{instance_id}.log"
async def start_bot(self, instance_id: str) -> dict:
"""
Start a bot instance.
Args:
instance_id: The bot instance ID to start
Returns:
Status dict with pid and info
"""
async with self._lock:
# Check if already running
if instance_id in self._processes:
proc = self._processes[instance_id]
if proc.is_running:
return {
"status": "already_running",
"pid": proc.pid,
"instance_id": instance_id,
}
else:
# Clean up dead process
del self._processes[instance_id]
if not self._bot_module_path.exists():
return {
"status": "error",
"error": f"Bot module not found: {self._bot_module_path}",
"instance_id": instance_id,
}
# Set up environment
env = os.environ.copy()
env["BOT_INSTANCE_ID"] = instance_id
env["PYTHONUNBUFFERED"] = "1"
# Ensure subprocess can reach orchestrator (use 127.0.0.1 to avoid DNS issues)
if "ORCHESTRATOR_URL" not in env:
env["ORCHESTRATOR_URL"] = "http://127.0.0.1:8002"
# Generate internal JWT for subprocess to authenticate with orchestrator API
try:
from auth_manager import create_access_token
token, _, _ = create_access_token(
username="__internal_bot__", role="admin", user_id=0
)
env["BOT_INTERNAL_TOKEN"] = token
except Exception as e:
logger.warning(f"Could not generate internal token: {e}")
# Pre-fetch config and pass via temp file (avoids subprocess→orchestrator callback)
try:
from db.integration import async_bot_instance_manager
instance_data = await async_bot_instance_manager.get_instance_with_token(
instance_id
)
if instance_data:
config_path = Path(f"/tmp/bot_config_{instance_id}.json")
config_path.write_text(
json.dumps(instance_data, ensure_ascii=False, default=str)
)
env["BOT_CONFIG_FILE"] = str(config_path)
logger.info(f"Pre-fetched config for {instance_id} → {config_path}")
except Exception as e:
logger.warning(f"Could not pre-fetch config for {instance_id}: {e}")
# Set up log file
log_file = self._get_log_path(instance_id)
try:
# Start subprocess
with open(log_file, "a") as log_fd:
log_fd.write(f"\n{'=' * 60}\n")
log_fd.write(f"Starting bot instance: {instance_id}\n")
log_fd.write(f"Time: {datetime.utcnow().isoformat()}\n")
log_fd.write(f"{'=' * 60}\n\n")
# Run as module: python -m telegram_bot
process = subprocess.Popen(
[sys.executable, "-m", self._bot_module],
env=env,
stdout=log_fd,
stderr=subprocess.STDOUT,
start_new_session=True, # Don't kill on parent exit
cwd=str(Path(__file__).parent), # Run from project root
)
self._processes[instance_id] = BotProcess(
instance_id=instance_id,
process=process,
log_file=log_file,
)
logger.info(f"Started bot instance {instance_id} with PID {process.pid}")
return {
"status": "started",
"pid": process.pid,
"instance_id": instance_id,
"log_file": str(log_file),
}
except Exception as e:
logger.error(f"Failed to start bot {instance_id}: {e}")
return {
"status": "error",
"error": str(e),
"instance_id": instance_id,
}
async def stop_bot(self, instance_id: str, timeout: int = 5) -> dict:
"""
Stop a bot instance.
Args:
instance_id: The bot instance ID to stop
timeout: Seconds to wait for graceful shutdown
Returns:
Status dict
"""
async with self._lock:
if instance_id not in self._processes:
return {
"status": "not_running",
"instance_id": instance_id,
}
proc = self._processes[instance_id]
if not proc.is_running:
del self._processes[instance_id]
return {
"status": "already_stopped",
"instance_id": instance_id,
}
try:
# Try graceful shutdown first
proc.process.terminate()
try:
proc.process.wait(timeout=timeout)
except subprocess.TimeoutExpired:
# Force kill
proc.process.kill()
proc.process.wait(timeout=2)
del self._processes[instance_id]
logger.info(f"Stopped bot instance {instance_id}")
return {
"status": "stopped",
"instance_id": instance_id,
}
except Exception as e:
logger.error(f"Error stopping bot {instance_id}: {e}")
return {
"status": "error",
"error": str(e),
"instance_id": instance_id,
}
async def restart_bot(self, instance_id: str) -> dict:
"""Restart a bot instance."""
await self.stop_bot(instance_id)
await asyncio.sleep(0.5) # Brief pause between stop and start
return await self.start_bot(instance_id)
async def get_bot_status(self, instance_id: str) -> dict:
"""Get status of a specific bot instance."""
async with self._lock:
if instance_id not in self._processes:
return {
"status": "stopped",
"running": False,
"instance_id": instance_id,
}
proc = self._processes[instance_id]
if not proc.is_running:
# Clean up dead process
del self._processes[instance_id]
return {
"status": "stopped",
"running": False,
"instance_id": instance_id,
}
return {
"status": "running",
"running": True,
"instance_id": instance_id,
"pid": proc.pid,
"log_file": str(proc.log_file),
"started_at": proc.started_at.isoformat(),
"uptime_seconds": proc.uptime_seconds,
}
async def get_all_statuses(self) -> Dict[str, dict]:
"""Get status of all bot instances."""
async with self._lock:
statuses = {}
dead_instances = []
for instance_id, proc in self._processes.items():
if proc.is_running:
statuses[instance_id] = {
"running": True,
"pid": proc.pid,
"started_at": proc.started_at.isoformat(),
"uptime_seconds": proc.uptime_seconds,
}
else:
statuses[instance_id] = {"running": False}
dead_instances.append(instance_id)
# Clean up dead processes
for instance_id in dead_instances:
del self._processes[instance_id]
return statuses
async def get_running_instances(self) -> List[str]:
"""Get list of running instance IDs."""
statuses = await self.get_all_statuses()
return [k for k, v in statuses.items() if v.get("running")]
async def stop_all(self) -> dict:
"""Stop all running bot instances."""
results = {}
instance_ids = list(self._processes.keys())
for instance_id in instance_ids:
results[instance_id] = await self.stop_bot(instance_id)
return results
def get_log_path(self, instance_id: str) -> Path:
"""Get log file path for bot instance."""
return self._get_log_path(instance_id)
async def get_recent_logs(self, instance_id: str, lines: int = 100) -> str:
"""Get recent log lines for a bot instance."""
log_file = self._get_log_path(instance_id)
if not log_file.exists():
return ""
try:
# Read last N lines
with open(log_file) as f:
all_lines = f.readlines()
return "".join(all_lines[-lines:])
except Exception as e:
logger.error(f"Error reading logs for {instance_id}: {e}")
return f"Error reading logs: {e}"
# Global instance
multi_bot_manager = MultiBotManager()