package io.wdd.rpc.scheduler.job;


import io.wdd.common.utils.TimeUtils;
import io.wdd.rpc.scheduler.beans.ScriptSchedulerDTO;
import io.wdd.rpc.scheduler.service.script.AgentApplyScheduledScript;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;

import static io.wdd.rpc.scheduler.service.QuartzSchedulerServiceImpl.SCRIPT_SCHEDULER_MISSION_KEY;

/**
 * 定时脚本任务核心类，Quartz框架定时调用该类
 */
@Slf4j
@Deprecated
public class AgentScriptSchedulerJob extends QuartzJobBean {

    @Resource
    AgentApplyScheduledScript agentApplyScheduledScript;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        // 解析 Scheduler 模块传递过来的参数
        JobDataMap jobDataMap = jobExecutionContext
                .getJobDetail()
                .getJobDataMap();

        // ScriptScheduleDTO
        ScriptSchedulerDTO scriptSchedulerDTO = (ScriptSchedulerDTO) jobDataMap.get(SCRIPT_SCHEDULER_MISSION_KEY);

        log.info("[ 定时脚本任务 ] - 调用时间为: {}; 执行内容为: {}",
                 TimeUtils.currentTimeString(),
                 scriptSchedulerDTO
        );

        // 调用实际任务执行器, 实际执行定时任务的核心业务内容
        agentApplyScheduledScript.apply(scriptSchedulerDTO);

    }
}
