package com.sonic.frog.job;


import com.sonic.common.client.JobmanClient;
import com.sonic.common.utils.LogUtils;
import com.sonic.frog.service.HomeRecommendV2Service;
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;

/**
 * 更新首页聚合推荐缓存数据
 */
@Slf4j
@Component
public class UpdateHomeRecommendJob {

    @Autowired
    private JobmanClient jobmanClient;
    @Autowired
    private HomeRecommendV2Service homeRecommendV2Service;

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

    protected JobmanClient.JobResult updateHomeRecommendJob(JobmanClient.JobContext jobContext) {
        try {
            LogUtils.setTraceId();
            log.info("===> updateHomeRecommendJob start !!!");
            homeRecommendV2Service.updateCache();
            log.info("===> updateHomeRecommendJob count : {} end !!!", 0);
            return JobmanClient.JobResult.success(0);
        } catch (Exception e) {
            log.error("===> updateHomeRecommendJob error : ", e);
        }finally {
            LogUtils.removeTraceId();
        }
        return JobmanClient.JobResult.success(0);
    }

}