Search in sources :

Example 1 with WaitContainerResultCallback

use of com.github.dockerjava.api.command.WaitContainerResultCallback in project node-controller by metersphere.

the class JmeterOperateService method startContainer.

private void startContainer(TestRequest testRequest, DockerClient dockerClient, String testId, String containerImage) {
    // 创建 hostConfig
    HostConfig hostConfig = HostConfig.newHostConfig();
    hostConfig.withNetworkMode("host");
    String[] envs = getEnvs(testRequest);
    String containerId = DockerClientService.createContainers(dockerClient, testId, containerImage, hostConfig, envs).getId();
    DockerClientService.startContainer(dockerClient, containerId);
    LoggerUtil.info("Container create started containerId: " + containerId);
    String topic = testRequest.getEnv().getOrDefault("LOG_TOPIC", "JMETER_LOGS");
    String reportId = testRequest.getEnv().get("REPORT_ID");
    dockerClient.waitContainerCmd(containerId).exec(new WaitContainerResultCallback() {

        @Override
        public void onComplete() {
            // 清理文件夹
            try {
                if (DockerClientService.existContainer(dockerClient, containerId) > 0) {
                    // copyTestResources(dockerClient, containerId, reportId, resourceIndex);
                    DockerClientService.removeContainer(dockerClient, containerId);
                }
                LoggerUtil.info("Remove container completed: " + containerId);
            } catch (Exception e) {
                LoggerUtil.error("Remove container error: ", e);
            }
            LoggerUtil.info("completed....");
        }
    });
    dockerClient.logContainerCmd(containerId).withFollowStream(true).withStdOut(true).withStdErr(true).withTailAll().exec(new InvocationBuilder.AsyncResultCallback<Frame>() {

        @Override
        public void onNext(Frame item) {
            String log = new String(item.getPayload()).trim();
            String oomMessage = "There is insufficient memory for the Java Runtime Environment to continue.";
            if (StringUtils.contains(log, oomMessage)) {
                // oom 退出
                String[] contents = new String[] { reportId, "none", "0", oomMessage };
                String message = StringUtils.join(contents, " ");
                kafkaProducerService.sendMessage(topic, message);
            }
            LoggerUtil.info(log);
        }
    });
}
Also used : Frame(com.github.dockerjava.api.model.Frame) HostConfig(com.github.dockerjava.api.model.HostConfig) WaitContainerResultCallback(com.github.dockerjava.api.command.WaitContainerResultCallback) InvocationBuilder(com.github.dockerjava.core.InvocationBuilder) IOException(java.io.IOException)

Example 2 with WaitContainerResultCallback

use of com.github.dockerjava.api.command.WaitContainerResultCallback in project airavata-data-lake by apache.

the class GenericDataParsingTask method runContainer.

private void runContainer(DataParser parser, String inputPath, String outputPath, Map<String, String> environmentValues) throws Exception {
    DefaultDockerClientConfig.Builder config = DefaultDockerClientConfig.createDefaultConfigBuilder();
    DockerClient dockerClient = DockerClientBuilder.getInstance(config.build()).build();
    logger.info("Pulling image " + parser.getDockerImage());
    try {
        dockerClient.pullImageCmd(parser.getDockerImage().split(":")[0]).withTag(parser.getDockerImage().split(":")[1]).exec(new PullImageResultCallback()).awaitCompletion();
    } catch (InterruptedException e) {
        logger.error("Interrupted while pulling image", e);
        throw e;
    }
    logger.info("Successfully pulled image " + parser.getDockerImage());
    String containerId = UUID.randomUUID().toString();
    String[] commands = parser.getExecCommand().split(" ");
    CreateContainerResponse containerResponse = dockerClient.createContainerCmd(parser.getDockerImage()).withCmd(commands).withName(containerId).withBinds(Bind.parse(inputPath + ":" + parser.getInputPath()), Bind.parse(outputPath + ":" + parser.getOutputPath())).withTty(true).withAttachStdin(true).withAttachStdout(true).withEnv(environmentValues.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.toList())).exec();
    logger.info("Created the container with id " + containerResponse.getId());
    final StringBuilder dockerLogs = new StringBuilder();
    if (containerResponse.getWarnings() != null && containerResponse.getWarnings().length > 0) {
        StringBuilder warningStr = new StringBuilder();
        for (String w : containerResponse.getWarnings()) {
            warningStr.append(w).append(",");
        }
        logger.warn("Container " + containerResponse.getId() + " warnings : " + warningStr);
    } else {
        logger.info("Starting container with id " + containerResponse.getId());
        dockerClient.startContainerCmd(containerResponse.getId()).exec();
        LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerResponse.getId()).withStdOut(true).withStdErr(true);
        try {
            logContainerCmd.exec(new ResultCallback.Adapter<Frame>() {

                @Override
                public void onNext(Frame item) {
                    logger.info("Got frame: {}", item);
                    ;
                    if (item.getStreamType() == StreamType.STDOUT) {
                        dockerLogs.append(new String(item.getPayload(), StandardCharsets.UTF_8));
                        dockerLogs.append("\n");
                    } else if (item.getStreamType() == StreamType.STDERR) {
                        dockerLogs.append(new String(item.getPayload(), StandardCharsets.UTF_8));
                        dockerLogs.append("\n");
                    }
                    super.onNext(item);
                }

                @Override
                public void onError(Throwable throwable) {
                    logger.error("Errored while running the container {}", containerId, throwable);
                    super.onError(throwable);
                }

                @Override
                public void onComplete() {
                    logger.info("Container {} successfully completed", containerId);
                    super.onComplete();
                }
            }).awaitCompletion();
        } catch (InterruptedException e) {
            logger.error("Interrupted while reading container log" + e.getMessage());
            throw e;
        }
        logger.info("Waiting for the container to stop");
        Integer statusCode = dockerClient.waitContainerCmd(containerResponse.getId()).exec(new WaitContainerResultCallback()).awaitStatusCode();
        logger.info("Container " + containerResponse.getId() + " exited with status code " + statusCode);
        if (statusCode != 0) {
            logger.error("Failing as non zero status code was returned");
            throw new Exception("Failing as non zero status code was returned");
        }
        logger.info("Container logs " + dockerLogs.toString());
    }
    dockerClient.removeContainerCmd(containerResponse.getId()).exec();
    logger.info("Successfully removed container with id " + containerResponse.getId());
}
Also used : TaskResult(org.apache.helix.task.TaskResult) DockerClientBuilder(com.github.dockerjava.core.DockerClientBuilder) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse) Bind(com.github.dockerjava.api.model.Bind) StringMap(org.apache.airavata.datalake.orchestrator.workflow.engine.task.types.StringMap) ManagedChannel(io.grpc.ManagedChannel) LoggerFactory(org.slf4j.LoggerFactory) BlockingTask(org.apache.airavata.datalake.orchestrator.workflow.engine.task.BlockingTask) HashMap(java.util.HashMap) StreamType(com.github.dockerjava.api.model.StreamType) DockerClient(com.github.dockerjava.api.DockerClient) org.apache.airavata.datalake.data.orchestrator.api.stub.parsing(org.apache.airavata.datalake.data.orchestrator.api.stub.parsing) Map(java.util.Map) WaitContainerResultCallback(com.github.dockerjava.api.command.WaitContainerResultCallback) Frame(com.github.dockerjava.api.model.Frame) PullImageResultCallback(com.github.dockerjava.api.command.PullImageResultCallback) DefaultDockerClientConfig(com.github.dockerjava.core.DefaultDockerClientConfig) Logger(org.slf4j.Logger) LogContainerResultCallback(com.github.dockerjava.core.command.LogContainerResultCallback) BlockingTaskDef(org.apache.airavata.datalake.orchestrator.workflow.engine.task.annotation.BlockingTaskDef) Files(java.nio.file.Files) TaskParam(org.apache.airavata.datalake.orchestrator.workflow.engine.task.annotation.TaskParam) IOException(java.io.IOException) UUID(java.util.UUID) LogContainerCmd(com.github.dockerjava.api.command.LogContainerCmd) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) ManagedChannelBuilder(io.grpc.ManagedChannelBuilder) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Paths(java.nio.file.Paths) ResultCallback(com.github.dockerjava.api.async.ResultCallback) Frame(com.github.dockerjava.api.model.Frame) DockerClient(com.github.dockerjava.api.DockerClient) LogContainerCmd(com.github.dockerjava.api.command.LogContainerCmd) IOException(java.io.IOException) WaitContainerResultCallback(com.github.dockerjava.api.command.WaitContainerResultCallback) PullImageResultCallback(com.github.dockerjava.api.command.PullImageResultCallback) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse) DefaultDockerClientConfig(com.github.dockerjava.core.DefaultDockerClientConfig)

Aggregations

WaitContainerResultCallback (com.github.dockerjava.api.command.WaitContainerResultCallback)2 Frame (com.github.dockerjava.api.model.Frame)2 IOException (java.io.IOException)2 DockerClient (com.github.dockerjava.api.DockerClient)1 ResultCallback (com.github.dockerjava.api.async.ResultCallback)1 CreateContainerResponse (com.github.dockerjava.api.command.CreateContainerResponse)1 LogContainerCmd (com.github.dockerjava.api.command.LogContainerCmd)1 PullImageResultCallback (com.github.dockerjava.api.command.PullImageResultCallback)1 Bind (com.github.dockerjava.api.model.Bind)1 HostConfig (com.github.dockerjava.api.model.HostConfig)1 StreamType (com.github.dockerjava.api.model.StreamType)1 DefaultDockerClientConfig (com.github.dockerjava.core.DefaultDockerClientConfig)1 DockerClientBuilder (com.github.dockerjava.core.DockerClientBuilder)1 InvocationBuilder (com.github.dockerjava.core.InvocationBuilder)1 LogContainerResultCallback (com.github.dockerjava.core.command.LogContainerResultCallback)1 ManagedChannel (io.grpc.ManagedChannel)1 ManagedChannelBuilder (io.grpc.ManagedChannelBuilder)1 File (java.io.File)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1