improve: auto-restart bridge when config changes

This commit is contained in:
linuztx 2026-03-26 02:07:45 +08:00
parent a12183ba6e
commit 45b21c093a
2 changed files with 16 additions and 0 deletions

View file

@ -57,6 +57,14 @@ async def _poll_loop() -> None:
allowed_users = config.get("allowed_users") or []
mode = config.get("mode", "bot")
# Detect config changes that require bridge restart
desired = {"port": port, "mode": mode, "allowed_users": sorted(allowed_users)}
running = bridge_manager.get_running_config()
if bridge_started and bridge_manager.is_process_alive() and running and running != desired:
PrintStyle.info(f"WhatsApp: config changed, restarting bridge")
await bridge_manager.stop_bridge()
bridge_started = False
# Start bridge if needed
if not bridge_started or not bridge_manager.is_process_alive():
try:

View file

@ -15,6 +15,7 @@ from helpers.print_style import PrintStyle
_bridge_process: subprocess.Popen | None = None
_bridge_lock = asyncio.Lock()
_bridge_config: dict = {} # config the running bridge was started with
BRIDGE_DIR = str(Path(__file__).parent.parent / "whatsapp-bridge")
BRIDGE_SCRIPT = os.path.join(BRIDGE_DIR, "bridge.js")
@ -57,6 +58,8 @@ async def start_bridge(
cwd=BRIDGE_DIR,
)
_start_log_reader(_bridge_process)
_bridge_config.clear()
_bridge_config.update({"port": port, "mode": mode, "allowed_users": sorted(allowed_users or [])})
# Wait for bridge to become healthy
for _ in range(20):
@ -85,6 +88,7 @@ async def stop_bridge() -> None:
except Exception:
pass
_bridge_process = None
_bridge_config.clear()
PrintStyle.info("WhatsApp: bridge stopped")
@ -148,6 +152,10 @@ def is_process_alive() -> bool:
return _bridge_process is not None and _bridge_process.poll() is None
def get_running_config() -> dict:
return dict(_bridge_config)
# ------------------------------------------------------------------
# Internal
# ------------------------------------------------------------------