package cn.ibizlab.core.util.job;

import cn.ibizlab.core.extensions.service.BaseEntityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 清理引擎、报表构建时，产生的全局无效KieContainer
 */
@Slf4j
@Component("CleanExpirationKieContainerJobHandler")
@ConditionalOnExpression("${ibiz.isCleanKC:false}")
public class CleanExpirationKieContainerJobHandler {

    @Autowired
    @Lazy
    private BaseEntityService baseEntityService;
    @Autowired
    @Lazy
    @Qualifier("AnalyseEntityServiceImpl")
    private BaseEntityService analyseEntityServiceImpl;

    /**
     * 默认每天凌晨6点开始执行
     */
    @Scheduled(cron = "${ibiz.cleanCronValue:0 0 6 * * ?}")
    private void cleanExpirationKieContainer(){
        long start = System.currentTimeMillis();
        log.info("--cleanExpirationKieContainer--开始执行");
        try {
            baseEntityService.cleanExpirationKieContainer();
            analyseEntityServiceImpl.cleanExpirationKieContainer();
            log.info("--cleanExpirationKieContainer--执行完成，耗时（毫秒）：" + (System.currentTimeMillis() - start));
        }catch (Exception e){
            e.printStackTrace();
            log.error("--cleanExpirationKieContainer--执行异常，原因：" + e.getMessage());
        }
    }
}
