package at.procon.dip.embedding.config;

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

@Configuration
public class EmbeddingJobProcessingConfiguration {

    @Bean(name = "embeddingJobProcessingExecutor")
    public Executor embeddingJobProcessingExecutor(EmbeddingProperties properties) {
        int parallelBatchCount = Math.max(1, properties.getJobs().getParallelBatchCount());

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix("embedding-job-");
        executor.setCorePoolSize(parallelBatchCount);
        executor.setMaxPoolSize(parallelBatchCount);
        executor.setQueueCapacity(parallelBatchCount);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(30);
        executor.initialize();
        return executor;
    }
}
