package tech.easyflow.agent.distributed;

import jakarta.annotation.PostConstruct;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;

/**
 * Agent 运行节点心跳维护器。
 */
@Component
public class AgentRuntimeNodeHeartbeat {

    private static final Duration HEARTBEAT_TTL = Duration.ofSeconds(90);

    private final AgentRuntimeRouteRegistry routeRegistry;

    /**
     * 创建 Agent 运行节点心跳维护器。
     *
     * @param routeRegistry Agent 运行态 Redis 路由注册表
     */
    public AgentRuntimeNodeHeartbeat(AgentRuntimeRouteRegistry routeRegistry) {
        this.routeRegistry = routeRegistry;
    }

    /**
     * 启动时立即写入一次当前节点心跳。
     */
    @PostConstruct
    public void init() {
        refresh();
    }

    /**
     * 定期刷新当前节点心跳。
     */
    @Scheduled(fixedDelayString = "${easyflow.agent.runtime.node-heartbeat-delay:30000}", initialDelay = 30000L)
    public void refresh() {
        routeRegistry.heartbeat(HEARTBEAT_TTL);
    }
}
