Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
67 lines (53 loc) · 1.95 KB
/
Copy pathserver.py
File metadata and controls
67 lines (53 loc) · 1.95 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
import argparse
import asyncio
import logging
import os
from discord_gateway import run_discord_gateway
from health_server import run_health_server
logger = logging.getLogger(__name__)
async def _serve(mode: str) -> None:
tasks = []
if mode in {"telegram", "all"}:
from bot import main as telegram_main
# Run telegram polling in a worker thread.
tasks.append(asyncio.create_task(asyncio.to_thread(telegram_main)))
if mode == "all":
logger.warning(
"Running multiple gateways in '--mode all'. Note: Telegram runs in a separate "
"thread with its own event loop. Sessions and asyncio locks are NOT shared "
"between gateways."
)
if mode in {"discord", "all"}:
tasks.append(asyncio.create_task(run_discord_gateway()))
if os.getenv("ENABLE_HEALTH_SERVER", "true").lower() == "true":
host = os.getenv("HEALTH_SERVER_HOST", "0.0.0.0")
port = int(os.getenv("HEALTH_SERVER_PORT", "8787"))
tasks.append(
asyncio.create_task(asyncio.to_thread(run_health_server, host, port))
)
if not tasks:
raise RuntimeError("No gateway selected.")
try:
await asyncio.gather(*tasks)
finally:
# Graceful shutdown: cancel background missions and close DB
from tools.background_ops import cancel_all_background_tasks
from tools.database_ops import close_db
from tools.mcp_manager import mcp_manager
await cancel_all_background_tasks()
await mcp_manager.cleanup()
close_db()
def main() -> None:
parser = argparse.ArgumentParser(
description="Yolo gateway server wrapper (Telegram/Discord-ready).",
)
parser.add_argument(
"--mode",
choices=["telegram", "discord", "all"],
default="telegram",
help="Gateway mode to start.",
)
args = parser.parse_args()
asyncio.run(_serve(args.mode))
if __name__ == "__main__":
main()