ADK-gateway/backend/app/services/heartbeat.py
2026-08-04 17:20:08 +08:00

24 lines
864 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""心跳保活服务:扫描 Agent 池,剔除超时未心跳节点。"""
import logging
import redis.asyncio as aioredis
from app.config import settings
from app.constants import AgentStatus
from app.repository.agent_repo import AgentRepo
logger = logging.getLogger(__name__)
class HeartbeatService:
def __init__(self, redis: aioredis.Redis, agent_repo: AgentRepo | None = None):
self.redis = redis
self.agent_repo = agent_repo or AgentRepo(redis)
async def scan(self) -> int:
"""扫描并剔除超时 Agent返回剔除数量。"""
stale = await self.agent_repo.stale_agents(settings.agent_heartbeat_timeout)
for aid in stale:
await self.agent_repo.update_status(aid, AgentStatus.OFFLINE)
logger.info("heartbeat: agent offline (stale) agent_id=%s", aid)
return len(stale)