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

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import javax.annotation.PostConstruct;

@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "application.scheduler")
public class SchedulingConfig implements SchedulingConfigurer {

    private int poolSize = 0;

    @PostConstruct
    private void init() {
        log.info("[{}] ------------", this.getClass().getSimpleName());
        if (this.poolSize == 0) {
//            log.warn("[{}] poolSize size set as default: {} EA.", this.getClass().getSimpleName(), this.poolSize);
            this.poolSize = 10;
        }
        log.info("[{}] poolCore: {} EA.", this.getClass().getSimpleName(), this.poolSize);
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();

        threadPoolTaskScheduler.setPoolSize(this.poolSize);
        threadPoolTaskScheduler.setThreadNamePrefix("scheduler-");
        threadPoolTaskScheduler.initialize();

        scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
    }
}
