package com.its.bis.scheduler;

import com.its.app.AppUtils;
import com.its.app.utils.Elapsed;
import com.its.bis.config.ApplicationConfig;
import com.its.bis.config.DebugConfig;
import com.its.bis.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

@Slf4j
@AllArgsConstructor
@EnableScheduling
@Component
public class SchedulerTask {

    private final ApplicationConfig config;
    private final UnitSystService unitSystService;

    @PreDestroy
    public void onShutDown() {
         this.unitSystService.updateUnitSystStts(false);
        DebugConfig debug = (DebugConfig) AppUtils.getBean(DebugConfig.class);
        debug.saveDebugInfo();
    }

    /**
     * 프로세스와 제어기 상태정보를 업데이트 한다.
     */
    @Scheduled(cron = "5 * * * * *")  // 1분주기 작업 실행
    public void scheduleUnitSystStts() {
        if (!this.config.isStartSchedule()) {
            return;
        }
        Elapsed elapsed = new Elapsed();
        log.info("scheduleUnitSystStts: start. {}", Thread.currentThread().getName());
        // 1. 프로세스 상태정보 업데이트
        this.unitSystService.updateUnitSystStts(true);
        log.info("scheduleUnitSystStts: ..end. {} ms. {}", elapsed.milliSeconds(), Thread.currentThread().getName());
    }

}
