package com.cgi.eoss.ftep.worker;

import com.cgi.eoss.ftep.rpc.CredentialsServiceGrpc;
import com.cgi.eoss.ftep.rpc.ServiceContextFilesServiceGrpc;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.RemoteApiVersion;
import com.google.common.base.Strings;
import io.grpc.ManagedChannelBuilder;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;

@Configuration
@ComponentScan(
        basePackageClasses = WorkerConfig.class,
        excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = FtepWorkerApplication.class)
)
@EnableScheduling
public class WorkerConfig {

    @Bean
    public Integer cacheConcurrencyLevel(@Value("${ftep.worker.cache.concurrency:4}") int concurrencyLevel) {
        return concurrencyLevel;
    }

    @Bean
    public Integer cacheMaxWeight(@Value("${ftep.worker.cache.maxWeight:1024}") int maximumWeight) {
        return maximumWeight;
    }

    @Bean
    public Path cacheRoot(@Value("${ftep.worker.cache.baseDir:/data/cache/dl}") String cacheRoot) {
        return Paths.get(cacheRoot);
    }

    @Bean
    public Path jobEnvironmentRoot(@Value("${ftep.worker.jobEnv.baseDir:/data/cache/jobs}") String jobEnvRoot) {
        return Paths.get(jobEnvRoot);
    }

    @Bean
    public ManagedChannelBuilder channelBuilder(@Value("${ftep.worker.server.grpcHost:f-tep-server}") String host,
                                                @Value("${ftep.worker.server.grpcPort:6565}") Integer port) {
        return ManagedChannelBuilder.forAddress(host, port).usePlaintext(true);
    }

    @Bean
    public CredentialsServiceGrpc.CredentialsServiceBlockingStub credentialsService(ManagedChannelBuilder channelBuilder) {
        return CredentialsServiceGrpc.newBlockingStub(channelBuilder.build());
    }

    @Bean
    public ServiceContextFilesServiceGrpc.ServiceContextFilesServiceBlockingStub serviceContextFilesService(ManagedChannelBuilder channelBuilder) {
        return ServiceContextFilesServiceGrpc.newBlockingStub(channelBuilder.build());
    }

    @Bean
    public OkHttpClient okHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        // If an http_proxy is set in the current environment, add it to the client
        String httpProxy = System.getenv("http_proxy");
        if (!Strings.isNullOrEmpty(httpProxy)) {
            URI proxyUri = URI.create(httpProxy);
            builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort())));
        }

        return builder.build();
    }

    @Bean
    @Primary
    public DockerClient dockerClient(@Value("${ftep.worker.docker.hostUrl:unix:///var/run/docker.sock}") String dockerHostUrl) {
        DockerClientConfig dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withApiVersion(RemoteApiVersion.VERSION_1_19)
                .withDockerHost(dockerHostUrl)
                .build();

        return DockerClientBuilder.getInstance(dockerClientConfig)
                .build();
    }

}

@@ -185,24 +185,87 @@
 ckerClient;%0A
+import com.github.dockerjava.api.command.DockerCmdExecFactory;%0A
 import com.g
@@ -470,24 +470,87 @@
 ApiVersion;%0A
+import com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory;%0A
 import com.g
@@ -3794,65 +3794,310 @@
     
-return DockerClientBuilder.getInstance(dockerClientConfig
+DockerCmdExecFactory dockerCmdExecFactory = new JerseyDockerCmdExecFactory()%0A                .withMaxTotalConnections(100)%0A                .withMaxPerRouteConnections(10);%0A%0A        return DockerClientBuilder.getInstance(dockerClientConfig)%0A                .withDockerCmdExecFactory(dockerCmdExecFactory
 )%0A  

Increase docker client connection pool to avoid blocking jobs