package at.procon.dip.clustering.config;

import java.util.concurrent.Executor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableConfigurationProperties(ClusteringExecutionProperties.class)
public class ClusteringExecutionConfiguration {

    @Bean(name = "clusteringRunExecutor")
    public Executor clusteringRunExecutor(ClusteringExecutionProperties properties) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix("clustering-run-");
        executor.setCorePoolSize(properties.resolvedCorePoolSize());
        executor.setMaxPoolSize(Math.max(properties.resolvedCorePoolSize(), properties.resolvedMaxPoolSize()));
        executor.setQueueCapacity(properties.resolvedQueueCapacity());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(30);
        executor.initialize();
        return executor;
    }
}
