package cn.sikey.selenium.scheduler;

import cn.sikey.selenium.util.WebDriverContextManagerUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class DeleteSessionScheduler {

    public static final int THREE_MINUTES = 3;

    /**
     * 每1分钟运行删除会话
     */
    @Scheduled(cron = "0 0/1 * * * ?")  // 每1分钟执行一次
    public void run() {
        WebDriverContextManagerUtil.sessionMap.asMap().forEach((k, v) -> {
            if (v.getCreateTime().plusMinutes(THREE_MINUTES).isBefore(LocalDateTime.now())) {
                WebDriverContextManagerUtil.destroySession(k);
            }
        });
    }
}
