package com.gameplatform.server.task;

import com.gameplatform.server.service.cooldown.MemoryMachineCooldownService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 机器冷却清理任务
 * 定期清理过期的机器冷却记录以释放内存
 */
@Component
public class MachineCooldownCleanupTask {
    private static final Logger log = LoggerFactory.getLogger(MachineCooldownCleanupTask.class);

    private final MemoryMachineCooldownService machineCooldownService;

    public MachineCooldownCleanupTask(MemoryMachineCooldownService machineCooldownService) {
        this.machineCooldownService = machineCooldownService;
    }

    /**
     * 每30分钟清理一次过期的冷却记录
     */
//    @Scheduled(fixedRate = 30 * 60 * 1000) // 30分钟
    public void cleanupExpiredCooldowns() {
        try {
            int sizeBefore = machineCooldownService.getCooldownQueueSize();
            machineCooldownService.cleanupExpiredCooldowns();
            int sizeAfter = machineCooldownService.getCooldownQueueSize();
            
            if (sizeBefore != sizeAfter) {
                log.info("机器冷却记录清理完成: 清理前{}个，清理后{}个，清理了{}个过期记录", 
                        sizeBefore, sizeAfter, sizeBefore - sizeAfter);
            } else {
                log.debug("机器冷却记录清理完成: 无过期记录需要清理，当前记录数{}", sizeAfter);
            }
        } catch (Exception e) {
            log.error("机器冷却记录清理失败: {}", e.getMessage(), e);
        }
    }
}