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

import com.utic.center.utic.ptis.server.config.ApplicationConfig;
import com.utic.center.utic.ptis.server.controller.UticPtisServerController;
import com.utic.center.utic.ptis.server.service.ProcessStateService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

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

    @Async("scheduleExecutor")
//    @Scheduled(cron = "0 0/5 * * * *")
//    @Scheduled(cron = "0 4,9,14,19,24,29,34,39,44,49,54,59 * * * *")
//    @Scheduled(cron = "${application.scheduling:0 4,9,14,19,24,29,34,39,44,49,54,59 * * * *}")
    @Scheduled(cron = "0 * * * * *")
    public void scheduleProcess() {
        if (this.config.isStartSchedule()) {
            this.controller.run();

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

}
