package com.utic.center.utic.stat.server.scheduler;

import com.utic.center.utic.stat.server.config.ApplicationConfig;
import com.utic.center.utic.stat.server.controller.UticStatServerController;
import com.utic.center.utic.stat.server.service.ProcessStateService;
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 UticStatServerScheduler {

    private final ApplicationConfig config;
    private final ProcessStateService processStateService;
    private final UticStatServerController controller;

//    @Async
    @Scheduled(cron = "0 * * * * *")
    public void scheduleProcess() {
        if (this.config.isStartSchedule()) {
            try {
                this.controller.run();
            } catch (Exception e) {
                log.error("ApplicationScheduler.scheduleProcess: Exception {}", e.getMessage());
            }

            try {
                // 프로세스 상태정보 업데이트
                this.processStateService.processRunning();
            } catch(Exception e) {
                log.error("ApplicationScheduler.updateProcessState: Exception {}", e.getMessage());
            }
        }
    }

}
