package cn.edu.zjvtit.cloudstorage.apiservice.schedule;

import cn.edu.zjvtit.cloudstorage.apiservice.store.DataServerStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.util.Map;

@Component
public class ScheduledTask {

    @Autowired
    private DataServerStore dataServerStore;
    @Scheduled(fixedRate = 5000)
    public void scheduledTask() {
        // 检查过期的服务器
        // 清除
        long now = System.currentTimeMillis() / 1000;
        Map<String,Long> serverHeart = this.dataServerStore.getServerHeart();
        for (Map.Entry<String,Long> entry : serverHeart.entrySet()) {
            // 清除五秒仍然没有心跳的服务器
            if (entry.getValue() < (now-5)) {
                this.dataServerStore.getServerHeart().remove(entry.getKey());
            }
        }

    }

}

