package com.sonic.cow.job;


import com.sonic.common.client.JobmanClient;
import com.sonic.common.utils.LogUtils;
import com.sonic.cow.service.GenImageTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 生成图片任务前端心跳过期数据扫描
 * @author mzc
 */
@Slf4j
@Component
public class GenImageHeartBeatScanJobHandler {

    @Autowired
    private JobmanClient jobmanClient;
    @Autowired
    private GenImageTaskService genImageTaskService;

    /** 每10秒执行一次 */
    @Scheduled(cron = "0/10 * * * * ? ")
    public void genImageHeartBeatScanSchedule() {
        // 锁 1 分钟过期
        jobmanClient.run("genImageHeartBeatScanJob", TimeUnit.MINUTES.toSeconds(2), this::genImageHeartBeatScanJob);
    }

    protected JobmanClient.JobResult genImageHeartBeatScanJob(JobmanClient.JobContext jobContext) {
        try {
            LogUtils.setTraceId();
            log.info("===> genImageHeartBeatScanJob start !!!");
            int count = genImageTaskService.scanHeartBeatExpTime();
            log.info("===> genImageHeartBeatScanJob count : {} end !!!", count);
            return JobmanClient.JobResult.success(count);
        } finally {
            LogUtils.removeTraceId();
        }
    }
}