package com.its.vds.stat.server.scheduler;

import com.its.vds.stat.server.config.ApplicationConfig;
import com.its.vds.stat.server.controller.VdsStatServerController;
import com.its.vds.stat.server.service.UnitSystService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@RequiredArgsConstructor
@EnableScheduling
@Component
public class ApplicationScheduler {


    private final ApplicationConfig config;
    private final UnitSystService unitSystService;
    private final VdsStatServerController controller;

    //    @Async
    @Scheduled(cron = "40 * * * * *")
    public void scheduleProcess() {
        if (this.config.isStartSchedule()) {
            try {
                // 프로세스 상태정보 업데이트
                this.unitSystService.processRunning();
            } catch(Exception e) {
                log.error("ApplicationScheduler.updateProcessState: Exception {}", e.getMessage());
            }

            try {
                if (this.controller.run()) {
                    // 5분 주기 작업이 끝났으면 프로세스 상태정보를 다시 업데이트 한다.
                    this.unitSystService.processRunning();
                }
            } catch (Exception e) {
                log.error("ApplicationScheduler.scheduleProcess: Exception {}", e.getMessage());
            }
        }
    }


}
