
package cn.orionsec.ops.task.fixed;

import cn.orionsec.ops.constant.common.EnableType;
import cn.orionsec.ops.constant.system.SystemEnvAttr;
import cn.orionsec.ops.handler.terminal.manager.TerminalSessionManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class TerminalHeartbeatPusher {

    @Resource
    private TerminalSessionManager terminalSessionManager;

    @Scheduled(cron = "*/30 * * * * ?")
    private void checkHeartbeat() {
        EnableType type = EnableType.of(SystemEnvAttr.TERMINAL_ACTIVE_PUSH_HEARTBEAT.getValue());
        if (!type.getValue()) {
            return;
        }
        terminalSessionManager.getSessionHolder().forEach((k, v) -> {
            log.info("terminal 开始主动发送心跳 token: {}", k);
            try {
                v.sendHeartbeat();
            } catch (Exception e) {
                log.error("terminal 主动发送心跳失败 token: {} {}", k, e);
            }
        });
    }

}
