Files
sino-mci/channel-service/app/services/heartbeat_service.py
marsal bd0ac464cf feat(channel-service): add Python FastAPI pywechat channel service
- Add FastAPI project under channel-service/
- Implement mock pywechat client adapter (login, send, heartbeat)
- Add /v1/wechat-personal/login/*, /v1/wechat-personal/send/* APIs
- Add /v1/heartbeat and /v1/monitor/status for client health
- Add /monitor HTML page with Web Components
- Add pytest suite and Dockerfile
- Update .gitignore for Python/Node artifacts
2026-07-07 12:36:54 +08:00

33 lines
1.2 KiB
Python

from __future__ import annotations
import time
from app.clients.pywechat_client import ClientRegistry, OnlineStatus, registry
class HeartbeatService:
def __init__(self, registry: ClientRegistry):
self.registry = registry
def heartbeat(self, account_id: str, payload: dict) -> dict:
client = self.registry.get_or_create(account_id, payload.get("account_name"))
client.heartbeat()
if "risk_level" in payload:
client.state.risk_level = payload["risk_level"]
if "sent_count_today" in payload:
client.state.sent_count_today = payload["sent_count_today"]
return {"success": True, "account_id": account_id}
def check_offline_accounts(self, timeout_seconds: int = 120) -> list[dict]:
now = time.time()
offline = []
for client in self.registry.list_all():
last = client.state.last_heartbeat_at
if client.state.online_status == OnlineStatus.ONLINE and (last is None or now - last > timeout_seconds):
client.state.online_status = OnlineStatus.OFFLINE
offline.append({"account_id": client.state.account_id, "last_heartbeat_at": last})
return offline
heartbeat_service = HeartbeatService(registry)